1

是否可以对术语查询使用模糊而不匹配?让我解释:

假设我们有 4 个文档

{ "index": { "_id": 1 }}
{ "text": "I play football!"}

{ "index": { "_id": 2 }}
{ "text": "I love playing"}

{ "index": { "_id": 3 }}
{ "text": "X is the best player"}

{ "index": { "_id": 4 }}
{ "text": "plyaer"}

使用时:

GET /index/my_type/_search
{

"query": {
    "fuzzy": {
      "value": "player",
      "fuzziness": 1 
    }
  }
}

我得到:

{ "index": { "_id": 3 }}
{ "text": "X is the best player"}

{ "index": { "_id": 4 }}
{ "text": "plyaer"}

但我只想要一个 plyaer 的结果,它对应于 fuzziness=1 的“精确”匹配(“术语”)

4

1 回答 1

0

每当您进行完全匹配时,您都需要具有关键字类型的字段,因为与文本类型不同,它不会经过分析阶段

我创建了下面的示例映射,其中字段myfield是一个多字段,如下面的映射所示。

映射

{  
   "myfield":{  
      "type":"text",
      "fields":{  
         "keyword":{  
            "type":"keyword",
            "ignore_above":256
         }
      }
   }
}

keyword然后,您可以对 type 字段而不是type执行模糊搜索text

myfield.keyword 上的模糊查询

POST <your_index_name>/_search
{
  "query": {
    "fuzzy": {
      "myfield.keyword": {
        "value": "player",
        "fuzziness": 2
      }
    }
  }
}

或者,您可以为这两种类型构建模糊查询,关键字类型具有更高的提升,这样完全匹配的结果就会出现在顶部。

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "fuzzy": {
            "myfield.keyword": {
              "value": "player",
              "fuzziness": 2,
              "boost": 10
            }
          }
        },
        {
          "fuzzy": {
            "myfield": {
              "value": "player",
              "fuzziness": 2,
              "boost": 2
            }
          }
        }
      ]
    }
  }
}

希望这可以帮助。

于 2018-12-10T12:03:28.953 回答