首页 > 解决方案 > 具有相邻单词的 Elastic Search 自动完成分析器

问题描述

在我的 ES 7.14 索引中,我在一些文本字段上设置了自定义分析器,如下所示

{
    "aliases": {},
    "mappings": {
        "properties": {
            "text": {
                "type": "text",
                "analyzer": "autocomplete"
            }
        }
    },
    "settings": {
        "analysis": {
            "filter": {
                "autocomplete_filter": {
                    "type": "ngram",
                    "min_gram": 1,
                    "max_gram": 60
                }
            },
            "analyzer": {
                "autocomplete": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "lowercase",
                        "autocomplete_filter"
                    ]
                }
            }
        },
        "index.max_ngram_diff": 60
    }
}

sayt此解决方案对于( search-as-you-type ) 之类的功能很有用,但在某些情况下,例如搜索相邻单词,这可能是个问题。事实上,当使用通配符时,*antonio* *banderas*它可以按预期工作,但是当使用它时antonio banderas,就像这里一样:

{
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "text": "antonio"
          }
        },
        {
          "wildcard": {
            "text": "banderas"
          }
        }
      ]
    }
  }
}

由于自定义分析器autocomplete绑定到字段text,它将搜索包含antonioplus的任何字符串(子标记) banderas。另一个例子:搜索marq会找到gabriel garcia marquez,因为marqin marquezmarq使用确切的通配符查询或*marq没有尾随的.时不应发生这种情况*。那么在这种情况下如何覆盖自动完成自定义分析器的行为来搜索相邻的单词呢?

标签: elasticsearchelasticsearch-7

解决方案


推荐阅读