0

New to ElasticSearch and I was wondering if there is a way to construct conditional queries/filters. I am working with Rails, so I suppose it has to be on that particular level, as I couldn't find anything that points to conditional queries at ES-Level and I am pretty sure it was silly just to assume!

So here is the (working) query I have:

search_definition = {
      query: {
          bool: {
              must: [
                  {
                      more_like_this: {
                          fields: tag_types,
                          docs: [
                              {
                                  _index: self.class.index_name,
                                  _type: self.class.document_type,
                                  _id: id
                              }
                          ],
                          min_term_freq: 1
                      }
                  }
              ],
              should: [
                  range: {
                      age: {
                          gte: min_age,
                          lte: max_age,
                          boost: 4.0
                      }
                  }
              ],
              filter: {
                  bool: {
                      must: [
                          term: {
                              active: true
                          }
                      ],
                      must: [
                          geo_distance: {
                              distance: xdistance,
                              unit: "km",
                              location: {
                                  lat: xlat,
                                  lon: xlng
                              },
                              boost: 5.0
                          }
                      ]
                  }
              }
          }
      },
      size: how_many
  }

And it works perfectly fine. Now let's assume I'd like to apply additional filters, in this particular example I need to verify when the user who is searching, that the users on the other end are, in fact, looking for a person of gender for whoever is searching. This is held in 2 separate boolean attributes in the database (male/female). I thought it would be simple enough to prepare two similar filters - however, there are a few more conditional filters that run into the queries, and I would eventually end up with more than ten pre-prepared filters. There must be a more elegant way! Thank you!

Georg Keferböck
  • 1,967
  • 26
  • 43

2 Answers2

1

Are you familiar with elasticsearch search templates?
Using search templates you can have conditional and dynamic queries. for example you can have a list of fields and values to do terms filter and pass it to search template as a parameter.

Mohammad Mazraeh
  • 1,044
  • 7
  • 12
  • This indeed looks interesting. I started yesterday setting up stuff with JBuilder in Rails, and it took me ages to get it somewhat working - so I perhaps stick with it for the time being and keep exploring templates the coming days :) For anyone else may reading this - when things get complicated JBuilder is not necessarily a pleasant tool to work with! – Georg Keferböck May 14 '17 at 10:53
1

As suggested by Mohammad - in the end, I pursued a solution using ES search templates which made my life a lot easier. The problem with JBuilder, ElasticSearch-DSL and other solutions is that they appear not to be working with the latest ES, and subsequently, I am not sure where I end up should there me ever any changes to gems or version of ES. So cutting the middle man out and taking full control with templates that are in fact super easy to create made a lot of sense to me. The versions I set up with JBuilder and ES-DSL never worked correctly as their output was random at best.

Search Templates -> More Information

JBuilder -> More Information

ElasticSearch-DSL -> More Information

There are other solutions that I haven't tried, but with search templates, I didn't see any need for that.

Georg Keferböck
  • 1,967
  • 26
  • 43