我正在尝试编写一个 elasticsearch (v5.1( 查询来检查字段中的所有标记是否与搜索词中的所有标记匹配,但顺序不限。
例如,该字段可以是:
full_name: 'Will Smith'
并且Will Smith
或Smith Will
的搜索词将匹配。但是,搜索Will
或Smith
不匹配。
我尝试过使用and
运算符匹配查询和使用slop
进行短语查询,但这些都确保术语搜索都在字段中,而不是字段中的所有术语都在搜索中。
我可以使用像reversed_name
这样的新字段进行索引,但想知道是否有我在某处缺少的查询选项。
您应该查看带有"minimum_should_match"参数的布尔查询。在您的情况下,它看起来像这样:
{
"query":{
"bool" : {
"should" : [
{"term" : { "name" : "Will" }},
{"term" : { "name" : "Smith" }}
],
"minimum_should_match" : 2
}
}
}
}
这将匹配"威尔史密斯"和"史密斯威尔"。如果您只想搜索威尔或史密斯,则需要将其更改为:
{
"query":{
"bool" : {
"should" : [
{"term" : { "name" : "Will" }}
],
"minimum_should_match" : 1
}
}
}
}
这次只会匹配"将"。(完全匹配(