首页 > 解决方案 > Elasticsearch - 在 query_string 中传递模糊参数

问题描述

我有一个自定义AUTO:10,20 模糊值的模糊查询。

{
"query": {
 "match": {
   "name": {
     "query": "nike",
     "fuzziness": "AUTO:10,20"
   }
 }
}
}

如何将其转换为query_string查询?我试过nike~AUTO:10,20了,但它不工作。

标签: elasticsearch

解决方案


也有可能query_strng,让我使用与 OP 提供的相同示例来展示,两者match_query都由 OP 匹配提供,并query_string使用相同的score.

根据这个这个ES 文档,Elasticsearch 支持AUTO:10,20格式,这也在我的示例中显示。

索引映射

{
    "mappings": {
        "properties": {
            "name": {
                "type": "text"
            }
        }
    }
}

索引一些文档

{
   "name" : "nike"
}

使用模糊匹配搜索查询

{
"query": {
 "match": {
   "name": {
     "query": "nike",
     "fuzziness": "AUTO:10,20"
   }
 }
}
}

结果

"hits": [
         {
            "_index": "so-query",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.9808292,
            "_source": {
               "name": "nike"
            }
         }
      ]

具有模糊性的查询字符串

{
    "query": {
        "query_string": {
            "fields": ["name"],
            "query": "nike",
            "fuzziness": "AUTO:10,20"
        }
    }
}

结果

 "hits": [
         {
            "_index": "so-query",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.9808292,
            "_source": {
               "name": "nike"
            }
         }
      ]

推荐阅读