如何在Elasticsearch中键入字段时添加模糊性



当您在Elasticsearch上键入字段类型时,我一直试图给搜索添加一些模糊性,但始终没有得到所需的查询。有人想实施这个吗?

模糊查询返回包含与搜索术语相似的术语的文档,这些术语由Levenstein编辑距离测量。

模糊性参数可以指定为:

AUTO--它根据术语的长度生成编辑距离。长度:

0..2--必须与完全匹配

3..5--允许一次编辑大于5--允许两次编辑

添加具有索引数据和搜索查询的工作示例。

指数数据:

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

搜索查询:

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

搜索结果:

"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"
}
}
]

参考这些博客来获得关于模糊查询的详细解释

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

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

更新1:请参阅ES官方文档

模糊性、prefix_length、max_expansions、rewrite和fuzzy_transpositions参数支持以下项用于构造术语查询,但对根据最终术语构造的前缀查询。

有一些悬而未决的问题,并讨论了一些链接,这些链接指出-模糊性不适用于bool_prefix multi_match(键入时搜索(

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

我知道这个问题很久以前就被问过了,但我认为这对我有效。

由于Elasticsearch允许用多个数据类型声明单个字段,因此我的映射如下所示。

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

在向索引中添加一些数据后,我像这样获取了这些数据。

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
}
}
]
}
}
}

最新更新