我需要使用match_phrase和模糊匹配的查询。但是我找不到任何文档来构造这样的查询。另外,当我尝试合并查询(一个在另一个)时,它会引发错误。可以构造这样的查询吗?
答案 0 :(得分:1)
您将需要使用Span Queries。
以下查询将对champions league
进行词组匹配+模糊查询,例如在name
类型的示例字段text
上
如果您想要多个字段,请添加另一个must
子句。
通知我已经提到slop:0
和in_order:true
可以进行精确的词组匹配,而您在fuzzy
查询中使用match
查询实现模糊行为。
POST span-index/mydocs/1
{
"name": "chmpions leage"
}
POST span-index/mydocs/2
{
"name": "champions league"
}
POST span-index/mydocs/3
{
"name": "chompions leugue"
}
POST span-index/_search
{
"query":{
"bool":{
"must":[
{
"span_near":{
"clauses":[
{
"span_multi":{
"match":{
"fuzzy":{
"testField":"champions"
}
}
}
},
{
"span_multi":{
"match":{
"fuzzy":{
"testField":"league"
}
}
}
}
],
"slop":0,
"in_order":true
}
}
]
}
}
}
{
"took": 19,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.5753642,
"hits": [
{
"_index": "span-index",
"_type": "mydocs",
"_id": "2",
"_score": 0.5753642,
"_source": {
"name": "champions league"
}
},
{
"_index": "span-index",
"_type": "mydocs",
"_id": "1",
"_score": 0.5753642,
"_source": {
"name": "chmpions leage"
}
},
{
"_index": "span-index",
"_type": "mydocs",
"_id": "3",
"_score": 0.5753642,
"_source": {
"name": "chompions leugue"
}
}
]
}
}
让我知道这是否有帮助!