简体   繁体   English

ElasticSearch-以任何顺序匹配所有术语

[英]ElasticSearch - Match all terms in any order

I'm trying to write an elasticsearch (v5.1) query to check whether all tokens in a field match all tokens in a search term , but in any order . 我正在尝试编写Elasticsearch(v5.1)查询,以检查字段中的所有标记是否与搜索项中的所有标记匹配,但顺序是否一致

For example the field could be: 例如,该字段可以是:

full_name: 'Will Smith'

And the search terms Will Smith or Smith Will would match. 搜索条件将匹配Will SmithSmith Will However searching Will or Smith would not match. 但是,搜索WillSmith不会匹配。

I've tried match queries with the and operator and phrase queries with slop , but these all ensured that the terms search were all in the field, not that all terms in the field were in the search. 我尝试过使用and运算符进行匹配查询, and使用slop短语查询,但是这些都确保了术语搜索全部在字段中,而不是确保字段中的所有术语都在搜索中。

I could index with a new field like reversed_name but was wondering if there was a query option I was missing somewhere. 我可以使用诸如reversed_name类的新字段建立索引,但我想知道是否某个地方缺少查询选项。

You should take a look at the bool query with "minimum_should_match" parameter. 您应该看看带有“ minimum_should_match”参数的布尔查询。 It would look something like this in your case: 在您的情况下,看起来像这样:

 {   
    "query":{
        "bool" : {
            "should" : [
               {"term" : { "name" : "Will" }},
               {"term" : { "name" : "Smith" }}
             ],
             "minimum_should_match" : 2
          }
       }
    }
}

This would match "Will Smith" and "Smith Will". 这将匹配“威尔·史密斯”和“史密斯·威尔”。 If you would like to search only Will or Smith then you would need to change it to this: 如果您只想搜索Will或Smith,则需要将其更改为:

 {   
    "query":{
        "bool" : {
            "should" : [
               {"term" : { "name" : "Will" }}
             ],
             "minimum_should_match" : 1
          }
       }
    }
}

Only "Will" will be matched this time. 这次只会匹配“ Will”。 (exact match) (完全符合)

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

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