从中间部分匹配完成建议 elasticsearch

Lij*_*ham 3 php search elasticsearch

我有一个名为search_suggest的字段,其中包含以下内容

search_suggest: {
   type: "completion",
   analyzer: "simple",
   payloads: true,
   preserve_separators: false,
   preserve_position_increments: false,
  max_input_length: 50
}
Run Code Online (Sandbox Code Playgroud)

它的值索引为

{
  input: [
   "apple iphone 6"
  ],
  output: "apple iphone 6",
  weight: 5,
  payload: {
   category: "mobiles"
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我搜索苹果,它会给我结果。但是,如果我搜索 iphone,它不会给我任何结果。

完成建议有什么方法可以做到这一点?我是否必须将输入索引为

  • 苹果 iphone 6
  • iPhone 6
  • 6

我知道 edge-ngram 建议者。但缺点是它也会建议重复。

请帮忙。

Zob*_*san 7

如果有人还在寻找答案,

完成建议器适用于前缀匹配。因此,在输入中,您可以提供短语的可能后缀。这将允许您进行前缀搜索,即使您从中间开始,也就是子字符串搜索。

例如:

{
  "text" : "Courtyard by Marriot Munich City",
  "text_suggest" : {
    "input": [
      "Courtyard by Marriot Munich City",
      "by Marriot Munich City",
      "Marriot Munich City",
      "Munich City",
      "City"
    ],
    "output" : "Courtyard by Marriot Munich City",
    "weight" : 11,
    "payload": { "id" : 314159 }
  }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,无论您从“慕尼黑万豪万怡酒店”开始,您都会获得结果。(除了可能用于“by”,因为在大多数情况下它会作为停用词被丢弃)。

对于一般搜索字符串,最多 4-5 个步骤就足够了。此外,如果您使用过滤器处理停用词,则无需担心输入中的停用词。

样品指数分析仪

{
  "settings" : {
    "analysis" : {
      "filter" : {
        "suggester_stop" : {
          "type" : "stop",
          "stopwords" : "_english_",
          "remove_trailing" : false,
          "ignore_case" : true
        },
        "suggester_stemmer" : {
          "type" : "stemmer",
          "name" : "light_english"
        }
      },
      "analyzer" : {
        "suggester_analyzer" : {
          "type" : "custom",
          "tokenizer" : "standard",
          "char_filter" : ["html_strip"],
          "filter" : [
            "standard",
            "lowercase",
            "suggester_stop",
            "suggester_stemmer"
          ]
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这将解决您在评论之一中提到的问题:

然后,如果我建议使用“apple ip”,则不会给出结果。iphone 6怎么样?

{
  "text_suggest" : {
    "input": [
      "apple iphone 6",
      "iphone 6"
    ],
    "output" : "apple iphone 6",
    "weight" : 11
  }
}
Run Code Online (Sandbox Code Playgroud)

您将获得“apple ip”、“iphone 6”等的搜索结果。但是,您不会获得“apple 6”的结果,这对于人们来说并不常见。