Warm tip: This article is reproduced from serverfault.com, please click

elasticsearch-当搜索词的单词多于索引时,如何匹配?

(elasticsearch - How to match when search term has more words than index?)

发布于 2020-11-30 01:56:20

我的索引是2-4个字符,没有空格,但是用户经常搜索“我没有索引”的“完整术语”,但是在空格后有3个额外的字符。例如:我索引“ A1”或“ A1B”或“ A1B2”,“完整术语”类似于“ A1 11A”或“ A1B ABA”或“ A1B2 2C8”。

这是当前映射:

"code": {
    "type": "text"
},

如果他搜索“ A1”,则会带入所有它们都是正确的,如果他键入“ A1B”,我只带最后两个,如果他搜索“ A1B2 2C8”,我只带最后一个。

那可能吗?如果是这样,最好的搜索/索引策略是什么?

Questioner
Murilo
Viewed
11
ESCoder 2020-11-30 10:12:50

索引映射:

{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 10
        }
      },
      "analyzer": {
        "autocomplete": { 
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "code": {
        "type": "text",
        "analyzer": "autocomplete", 
        "search_analyzer": "standard" 
      }
    }
  }
}

索引数据:

{
  "code": "A1"
}
{
  "code": "A1B"
}
{
  "code": "A1B2"
}

搜索查询:

{
    "query": {
        "match": {
            "code": {
                "query": "A1B2 2C8"
            }
        }
    }
}

搜索结果:

 "hits": [
      {
        "_index": "65067196",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.3486402,
        "_source": {
          "code": "A1B2"
        }
      }
    ]