【问题标题】:ElasticSearch - Match all terms in any orderElasticSearch - 以任何顺序匹配所有术语
【发布时间】:2019-02-11 12:02:09
【问题描述】:

我正在尝试编写一个 elasticsearch (v5.1) 查询来检查字段中的所有标记是否匹配搜索词中的所有标记,但是否以任何顺序.

例如,该字段可以是:

full_name: 'Will Smith'

搜索词 Will SmithSmith Will 将匹配。但是搜索 WillSmith 将不匹配。

我尝试了使用and 运算符的匹配查询和使用slop 的短语查询,但这些都确保了术语搜索都在字段中,而不是字段中的所有术语都在搜索中。

我可以使用reversed_name 之类的新字段进行索引,但想知道是否有我在某处遗漏的查询选项。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您应该查看带有“minimum_should_match”参数的布尔查询。在您的情况下,它看起来像这样:

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

    这将匹配“Will Smith”和“Smith Will”。如果您只想搜索 Will 或 Smith,则需要将其更改为:

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

    这次只会匹配“Will”。 (完全匹配)

    【讨论】:

    • 如果它是关于匹配所有术语,那么我建议不要使用should 指定“minimum_should_match”,而是使用“必须”子句。这将使查询更加简单易读。
    • 如果文档看起来像这样:“Will Smith Michael”怎么办?那么 minimum_should_match 就不行了?
    猜你喜欢
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    相关资源
    最近更新 更多