Elasticsearch“ match_phrase”查询和“ fuzzy”查询-可以结合使用

时间:2018-11-29 14:18:32

标签: elasticsearch elastic-stack elasticsearch-6

我需要使用match_phrase和模糊匹配的查询。但是我找不到任何文档来构造这样的查询。另外,当我尝试合并查询(一个在另一个)时,它会引发错误。可以构造这样的查询吗?

1 个答案:

答案 0 :(得分:1)

您将需要使用Span Queries

以下查询将对champions league进行词组匹配+模糊查询,例如在name类型的示例字段text

如果您想要多个字段,请添加另一个must子句。

通知我已经提到slop:0in_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"
        }
      }
    ]
  }
}

让我知道这是否有帮助!