简体   繁体   English

如何在 Elasticsearch 中输入字段时添加模糊搜索?

[英]How to add fuzziness to search as you type field in Elasticsearch?

I've been trying to add some fuzziness to my search as you type field type on Elasticsearch, but never got the needed query.当您在 Elasticsearch 上键入字段类型时,我一直在尝试为我的搜索添加一些模糊性,但从未获得所需的查询。 Anyone have any idea to implement this?任何人有任何想法来实现这个?

I know this question is asked long ago but I think this worked for me.我知道很久以前就有人问过这个问题,但我认为这对我有用。

Since Elasticsearch allows a single field to be declared with multiple data types, my mapping is like below.由于 Elasticsearch 允许使用多种数据类型声明单个字段,因此我的映射如下所示。

PUT products
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "fields": {
          "product_type": {
            "type": "search_as_you_type"
          }
        }
      }
    }
  }
}

After adding some data to the index I fetched like this.将一些数据添加到索引后,我像这样获取。

GET products/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "prodc",
            "type": "bool_prefix",
            "fields": [
              "title.product_type",
              "title.product_type._2gram",
              "title.product_type._3gram"
            ]
          }
        },
        {
          "multi_match": {
            "query": "prodc",
            "fuzziness": 2
          }
        }
      ]
    }
  }
}

Fuzzy Query returns documents that contain terms similar to the search term, as measured by a Levenshtein edit distance. Fuzzy Query返回包含与搜索词相似的词的文档,由 Levenshtein 编辑距离衡量。

The fuzziness parameter can be specified as: fuzziness 参数可以指定为:

AUTO -- It generates an edit distance based on the length of the term.自动——它根据术语的长度生成编辑距离。 For lengths:对于长度:

0..2 -- must match exactly 0..2 -- 必须完全匹配

3..5 -- one edit allowed Greater than 5 -- two edits allowed 3..5 -- 允许一次编辑 大于 5 -- 允许两次编辑

Adding working example with index data and search query.添加带有索引数据和搜索查询的工作示例。

Index Data:指数数据:

{
  "title":"product"
}
{
  "title":"prodct"
}

Search Query:搜索查询:

    {
    "query": {
        "fuzzy": {
            "title": {
                "value": "prodc",
                "fuzziness":2,
                "transpositions":true,
                 "boost": 5
            }
        }
    }
}

Search Result:搜索结果:

"hits": [
  {
    "_index": "test",
    "_type": "_doc",
    "_id": "1",
    "_score": 2.0794415,
    "_source": {
      "title": "product"
    }
  },
  {
    "_index": "test",
    "_type": "_doc",
    "_id": "2",
    "_score": 2.0794415,
    "_source": {
      "title": "produt"
    }
  }
]

Refer these blogs to get a detailed explaination on fuzzy query请参阅这些博客以获取有关模糊查询的详细说明

https://www.elastic.co/blog/found-fuzzy-search https://www.elastic.co/blog/found-fuzzy-search

https://qbox.io/blog/elasticsearch-optimization-fuzziness-performance https://qbox.io/blog/elasticsearch-optimization-fuzziness-performance

Update 1: Refer this ES official documentation更新 1:参考这个ES 官方文档

The fuzziness , prefix_length , max_expansions , rewrite , and fuzzy_transpositions parameters are supported for the terms that are used to construct term queries, but do not have an effect on the prefix query constructed from the final term.用于构建术语查询的术语支持 fuzziness 、 prefix_length 、 max_expansions 、 rewrite 和 Fuzzy_transpositions 参数,但对从最终术语构建的前缀查询没有影响。

There are some open issues and discuss links that states that - Fuzziness not work with bool_prefix multi_match (search-as-you-type)有一些悬而未决的问题和讨论链接指出 -模糊性不适用于 bool_prefix multi_match (search-as-you-type)

https://github.com/elastic/elasticsearch/issues/56229 https://github.com/elastic/elasticsearch/issues/56229

https://discuss.elastic.co/t/fuzziness-not-work-with-bool-prefix-multi-match-search-as-you-type/229602/3 https://discuss.elastic.co/t/fuzziness-not-work-with-bool-prefix-multi-match-search-as-you-type/229602/3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM