Elasticsearch 将字符串匹配到具有模糊性的字段

Tim*_*aas 4 elasticsearch

我正在尝试将字符串与字段匹配,并且只想应用模糊性。

例如,使用这些文件:

{ title: "replace oilfilter" }, { title: "replace motoroil" }
Run Code Online (Sandbox Code Playgroud)

以下查询应仅匹配第一个文档:

"Replace oilfilter", "Replace oilsfilter", "Replaze oilfilter"
Run Code Online (Sandbox Code Playgroud)

下面的查询应该匹配的任何文件:

"replace", "oilfilter", "motoroil"
Run Code Online (Sandbox Code Playgroud)

到目前为止我得到的是以下内容:

指数

我正在使用关键字分析器,因此它将(潜在)短语视为单个单词,这样它在搜索“replace”时与文档不匹配,但在搜索确切术语“Replace oilfilter”时确实找到了一个文档.

    "mappings": {
        "blacklist": {
            "properties": {
                "title": {
                    "type": "text",
                    "analyzer": "keyword"
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

搜索

我尝试了多个查询来搜索文档。我接近以下查询:

    "query": {
        "query_string": {
            "default_field": "title",
            "fuzziness": "3",
            "query": query
        }
    }
Run Code Online (Sandbox Code Playgroud)

结果

通过此查询,结果如下:

> "Replace oilfilter" (exact words)
< doc: { title: "replace oilfilter" }, score: 0.5753..
< doc: { title: "replace motoroil" }, score: 0.2876..

> "Replace iolfilter" (typo)
< doc: { title: "replace oilfilter" }, score: 0.2876..

> "oilfilter" (other term)
< doc: { title: "replace oilfilter" }, score: 0.2876..
Run Code Online (Sandbox Code Playgroud)

问题

结果还不错,但我需要分数更准确。只有简单错字的第二个查询应该比第一个查询中的第二个结果和第三个查询中的唯一结果获得更高的分数。

我想要实现的是它将整个查询与文档中的整个字段相匹配,这就是我使用关键字分析器的原因。最重要的是,我只想应用一些模糊性。

希望有人可以对这个问题有所了解。

谢谢!

Las*_*sus 9

以下搜索应该可以实现您想要的:

{
  "query": {
      "bool": {
        "must": {
          "multi_match": {
            "query": "replace oilfliter",
            "fuzziness": "3",
            "fields": [
              "title"
            ],
            "minimum_should_match": "75%",
            "type": "most_fields"
          }
        }
      }
  }
}
Run Code Online (Sandbox Code Playgroud)

minimum_should_match如果您希望无论查询字符串有多长,都需要对所有查询词进行匹配,则可以将增加到 100%。