我需要query_string只匹配完全相同。
根据弹性文档 在查询字符串查询时:
空格不被视为运算符,这意味着纽约市将"按原样"传递给为字段配置的分析器。如果字段是关键字字段,则分析器将创建一个术语纽约市,查询生成器将在查询中使用此术语。如果要单独查询每个术语,则需要在术语周围添加显式运算符(例如,new AND york AND city)。
我创建了一个索引测试索引并添加了随机数据:
- 版纳 AF
- 光盘测试自动对焦
- 测试光盘
- AF电视
- 测试阿布
发布:
POST testingindex/_doc/5
{
"name":"banna af"
}
搜索:
GET testingindex/_search?explain
{
"size": 10,
"query": {
"bool": {
"must": [
{
"query_string": {
"fuzziness": 0,
"phrase_slop": 0,
"default_operator": "OR",
"minimum_should_match": "99%",
"fields": [
"name"
],
"query":"(testing af) OR (banna af)"
}
}
]
}
}
}
结果:
"hits" : [
{
"_index" : "testingindex",
"_type" : "_doc",
"_id" : "6",
"_score" : 2.0794415,
"_source" : {
"name" : "banna af"
}
},
{
"_index" : "testingindex",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.8630463,
"_source" : {
"name" : "cd testing af"
}
},
{
"_index" : "testingindex",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.6931472,
"_source" : {
"name" : "testing cd"
}
},
{
"_index" : "testingindex",
"_type" : "_doc",
"_id" : "5",
"_score" : 0.5753642,
"_source" : {
"name" : "af television"
}
},
{
"_index" : "testingindex",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "testing ab"
}
}
]
如果我将运算符更改为:
"default_operator": "AND",
我得到了正确的结果。
但是如果我将查询更改为:
"query":"(testing af) OR (banna af) OR (badfadfaf)"
我没有得到任何结果,我仍然需要匹配的结果回来。
如何让cd 测试af 和banna af成为唯一返回的结果?
只需将术语本身括在双引号中(您必须转义)即可获得完全匹配并删除minimum_should_match
属性 - 简化的查询如下所示:
GET testingindex/_search
{
"query": {
"bool": {
"must": [
{
"query_string": {
"fields": [
"name"
],
"query":"("testing af") OR ("banna af") OR ("badfadfaf")"
}
}
]
}
}
}
屈服:
"hits" : {
"total" : 2,
"max_score" : 1.3862944,
"hits" : [
{
"_index" : "testingindex",
"_type" : "_doc",
"_id" : "qmD-EWoBqkB-aMRpwfuE",
"_score" : 1.3862944,
"_source" : {
"name" : "banna af"
}
},
{
"_index" : "testingindex",
"_type" : "_doc",
"_id" : "q2D_EWoBqkB-aMRpFPtX",
"_score" : 0.5753642,
"_source" : {
"name" : "cd testing af"
}
}
]
}