我正在尝试编写一个 elasticsearch (v5.1) 查询来检查一个字段中的所有标记是否与搜索词中的所有标记匹配,但以任何顺序。
例如,该字段可能是:
full_name: 'Will Smith'
Run Code Online (Sandbox Code Playgroud)
和搜索词Will SmithorSmith Will会匹配。但是搜索Will还是Smith不匹配。
我尝试过使用and运算符进行匹配查询,并使用 进行短语查询slop,但这些都确保了术语搜索都在该字段中,而不是该字段中的所有术语都在搜索中。
我可以用一个新字段来索引,reversed_name但想知道是否有我在某处丢失的查询选项。
您应该使用“minimum_should_match”参数查看 bool 查询。在您的情况下,它看起来像这样:
{
"query":{
"bool" : {
"should" : [
{"term" : { "name" : "Will" }},
{"term" : { "name" : "Smith" }}
],
"minimum_should_match" : 2
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将匹配“Will Smith”和“Smith Will”。如果您只想搜索 Will 或 Smith,则需要将其更改为:
{
"query":{
"bool" : {
"should" : [
{"term" : { "name" : "Will" }}
],
"minimum_should_match" : 1
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这次只会匹配“Will”。(完全符合)
| 归档时间: |
|
| 查看次数: |
4565 次 |
| 最近记录: |