ElasticSearch中基于词编辑距离的模糊语句搜索

tzdcorbm  于 2022-10-06  发布在  ElasticSearch
关注(0)|答案(1)|浏览(165)

对于给定的索引,我添加了如下文档:

[
{"expression": "tell me something about elasticsearch"},
{"expression": "this is a new feature for elasticsearch"},
{"expression": "tell me something about kibana"},

# ... and so on

]

现在,我想查询ElasticSearch,对于给定的输入表达式:"tell me something on elasticsearch"。它必须给出:

{"expression": "tell me something about elasticsearch"},
{"expression": "tell me something about kibana"}

由于是这种情况,因此编辑距离w.r.t.在这种情况下,to words(非字符级)较小。

我们可以在ElasticSearch上执行这样的查询吗?

xesrikrc

xesrikrc1#

根据我的理解,模糊性不允许输入短语/匹配短语。但让我为您分享一些用例,并尝试这些用例是否有帮助。

1.如果您要忽略丢失的单词执行搜索,请使用slopWITH MATCH_PASSION而不是FUZZY(这可能对您有效)

GET demo_index/_search
{
    "query": {
        "match_phrase": {
            "field1": {
                "query": "tell me something elasticsearch",
                "slop": 1                                     -----> you can increase it as per your requirement
            }
        }
    }
}
2. Secondly if you want to perform search on character level changes you can use below queries with fuzziness

 - Single search on different fields
GET index_name/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "enginere",                   -----> Wrong spelling but still we will get result wherever query match**engineer**keyword. Again you can increase fuzziness.
            "fields": [
              "field_name1",
              "field_name2",
              ...
            ],
            "fuzziness": 1,
            "slop": 1                              -----> not compulsory 
          }
        }
      ]
    }
  }
}
- Multi search in different fields
GET index_name/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "field1": {
              "query": "text1",
              "fuzziness": 1
            }
          }
        },
        {
          "match": {
            "field2": {
              "query": "text2",
              "fuzziness": 2
            }
          }
        }
      ],
      "filter": [
        {
          "match": {
            "field3": "text3"
          }
        }
      ]
    }
  }
}

相关问题