【问题标题】:How to match 1-2 character exact matches while using edge-ngram of 3 in elasticsearch在elasticsearch中使用3的edge-ngram时如何匹配1-2个字符的精确匹配
【发布时间】:2015-11-12 16:45:52
【问题描述】:

我正在使用弹性搜索来查询具有模糊匹配的索引。我正在使用 min_gram 长度为 3 的 edge-ngram 分词器。

但是,对于仅包含 1 或 2 个字符的查询,这不会返回任何内容。是否可以仅匹配 1 或 2 个字符的完全匹配,但使用 edge-ngram 进行具有三个或更多字符的查询?

这是我当前的弹性搜索索引映射:

curl -XPUT 'http://localhost:9200/person' -d '{
"settings": {
    "number_of_shards": 1,
    "analysis": {
        "filter": {
            "autocomplete_filter": {
                "type":     "edge_ngram",
                "min_gram": 3,
                "max_gram": 20
            }
        },
        "analyzer": {
            "default": {
                "type":      "custom",
                "tokenizer": "standard",
                "filter": [
                    "lowercase",
                    "autocomplete_filter"
                ]
            }
        }
    }
}
}'

要查询这个索引,请求如下:

curl -XPOST 'localhost:9200/person/type/_search' -d '{
    "query": {
        "match": {
            "_all": "Tim”
        }
    }
}'

产生大量结果,但是像这样的请求

curl -XPOST 'localhost:9200/person/type/_search' -d '{
    "query": {
        "match": {
            "_all": "Ti”
        }
    }
}'

给出一个空集。理想情况下,如果索引中有名为 Tim 的人,则第二个请求会返回一些结果。

【问题讨论】:

    标签: elasticsearch n-gram


    【解决方案1】:

    我不确定这是否能满足您的所有要求。您可以检查用户输入的长度是否小于 3,然后触发以下查询。

    {
      "query": {
        "match_phrase_prefix": {
          "_all": "ti"
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多