简体   繁体   English

在Elasticsearch中使用3的edge-ngram时如何匹配1-2个字符的完全匹配

[英]How to match 1-2 character exact matches while using edge-ngram of 3 in elasticsearch

I am using elasticsearch to query an index with fuzzy matches. 我正在使用Elasticsearch查询具有模糊匹配的索引。 I am using an edge-ngram tokenizer with a min_gram length of 3. 我正在使用min_gram长度为3的edge-ngram标记器。

However, this returns nothing for queries that only include 1 or 2 characters. 但是,对于仅包含1个或2个字符的查询,此操作不返回任何内容。 Is it possible to match on exact matches for just those 1 or 2 characters, but use the edge-ngram for queries with three characters or more? 是否可以仅匹配那些1或2个字符的完全匹配项,但是将edge-ngram用于具有三个或更多字符的查询?

This is my current elasticsearch index mappings: 这是我当前的Elasticsearch索引映射:

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

To query this index, a request like: 要查询该索引,请执行以下请求:

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

Yields plenty of results, however a request like 产生大量结果,但是像

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

gives an empty set. 给出一个空集。 Ideally, the second request would return some result if there is someone in the index named Tim. 理想情况下,如果索引中有某人名为Tim,则第二个请求将返回一些结果。

I am not sure If this will satisfy all your requirements. 我不确定这是否可以满足您的所有要求。 You could check if the length of user input is less than 3 then fire the below query. 您可以检查用户输入的长度是否小于3,然后触发以下查询。

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

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

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