返回从 ElasticSearch 中的字段派生的关键字集



我有点新,我需要帮助,我在网上查找找不到我正在寻找的任何答案。基本上,im 尝试做的是基于从某些文本字段派生的关键字进行自动完成

举一个我的索引示例:

"name": "One liter of Chocolate Milk"
"name": "Milo Milk 250g"
"name": "HiLow low fat milk"
"name": "Yoghurt strawberry"
"name": "Milk Nutrisoy"

因此,当我输入"mi"时,我希望得到这样的结果:

"milk"
"milo"
"milo milk"
"chocolate milk" 
etc

很好的例子是这个 aliexpress.com 自动完成

提前致谢

这似乎是shingle令牌过滤器的一个很好的用例

curl -XPUT localhost:9200/your_index -d '{
  "settings": {
      "analysis": {
        "analyzer": {
          "my_shingles": {
            "tokenizer": "standard",
            "filter": [
              "lowercase",
              "shingles"
            ]
          }
        },
        "filter": {
          "shingles": {
            "type": "shingle",
            "min_shingle_size": 2,
            "max_shingle_size": 2,
            "output_unigrams": true
          }
        }
      }
  },
  "mappings": {
    "your_type": {
      "properties": {
        "field": {
          "type": "string",
          "analyzer": "my_shingles"
        }
      }
    }
  }
}'

如果使用此分析器分析Milo Milk 250g,则会获得以下令牌:

curl -XGET 'localhost:9200/your_index/_analyze?analyzer=my_shingles&pretty' -d 'Milo Milk 250g'
{
  "tokens" : [ {
    "token" : "milo",
    "start_offset" : 0,
    "end_offset" : 4,
    "type" : "<ALPHANUM>",
    "position" : 0
  }, {
    "token" : "milo milk",
    "start_offset" : 0,
    "end_offset" : 9,
    "type" : "shingle",
    "position" : 0
  }, {
    "token" : "milk",
    "start_offset" : 5,
    "end_offset" : 9,
    "type" : "<ALPHANUM>",
    "position" : 1
  }, {
    "token" : "milk 250g",
    "start_offset" : 5,
    "end_offset" : 14,
    "type" : "shingle",
    "position" : 1
  }, {
    "token" : "250g",
    "start_offset" : 10,
    "end_offset" : 14,
    "type" : "<ALPHANUM>",
    "position" : 2
  } ]
}

因此,在搜索 mi 时,您将获得以下令牌:

  • 麦洛
  • 美禄牛奶
  • 牛奶
  • 牛奶 250g

最新更新