首页 文章

弹性搜索中的术语模糊性

提问于
浏览
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相对应的结果,对应于模糊= 1的“精确”匹配(“术语”)

1 回答

  • 0

    每当你进行完全匹配时你需要有Keyword类型的字段,与Text类型不同,它不会经过Analysis阶段

    我创建了下面的示例映射,其中field myfieldmulti-field,如下面的映射所示 .

    Mapping

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

    然后,您可以在 keyword 类型的字段上执行模糊搜索,而不是 text 类型 .

    Fuzzy Query on 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
                }
              }
            }
          ]
        }
      }
    }
    

    希望这可以帮助 .

相关问题