简体   繁体   English

对ElasticSearch 5.x的条件查询(elasticsearch-rails / elasticsearch-model)

[英]Conditional queries for ElasticSearch 5.x (elasticsearch-rails/elasticsearch-model)

New to ElasticSearch and I was wondering if there is a way to construct conditional queries/filters. ElasticSearch的新手,我想知道是否有一种构造条件查询/过滤器的方法。 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! 我正在与Rails合作,所以我认为它必须处于特定级别,因为我找不到指向ES级条件查询的任何内容,而且我敢肯定,只是假设而已!

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). 它保存在数据库中的2个独立的布尔属性中(男性/女性)。 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! 谢谢!

Are you familiar with elasticsearch search templates ? 您对elasticsearch search templates熟悉吗?
Using search templates you can have conditional and dynamic queries. 使用搜索模板,您可以进行conditional查询和dynamic查询。 for example you can have a list of fields and values to do terms filter and pass it to search template as a parameter. 例如,您可以具有要进行术语过滤的字段和值的列表,并将其作为参数传递给搜索模板。

As suggested by Mohammad - in the end, I pursued a solution using ES search templates which made my life a lot easier. 正如Mohammad所建议-最终,我使用ES搜索模板寻求一种解决方案,这使我的生活变得更加轻松。 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. JBuilder,ElasticSearch-DSL和其他解决方案的问题在于它们似乎无法与最新的ES一起使用,随后,我不确定对gem或版本的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. 我使用JBuilder和ES-DSL设置的版本无法正常工作,因为它们的输出充其量是随机的。

Search Templates -> More Information 搜索模板-> 更多信息

JBuilder -> More Information JBuilder-> 更多信息

ElasticSearch-DSL -> More Information ElasticSearch-DSL-> 更多信息

There are other solutions that I haven't tried, but with search templates, I didn't see any need for that. 我还没有尝试过其他解决方案,但是对于搜索模板,我认为没有任何需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM