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

标签 php search elasticsearch

我有一个名为 search_suggest 的字段,其内容如下

search_suggest: {
   type: "completion",
   analyzer: "simple",
   payloads: true,
   preserve_separators: false,
   preserve_position_increments: false,
  max_input_length: 50
}

它的值索引为

{
  input: [
   "apple iphone 6"
  ],
  output: "apple iphone 6",
  weight: 5,
  payload: {
   category: "mobiles"
  }
}

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

completion suggester 有什么办法可以做到这一点吗?。 我必须将输入索引为

  • 苹果手机 6
  • iPhone 6
  • 6

我知道 edge-ngram suggester。但缺点是它也会提示重复项。

请帮忙。

最佳答案

如果有人还在寻找答案,

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

例如:

{
  "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 }
  }
}

如您所见,无论您从“慕尼黑市万怡酒店”开始,您都会得到结果。 (除了可能用于“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"
          ]
        }
      }
    }
  }
}

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

Then if I suggest for "apple ip", It won't give result. How about iphone 6?

{
  "text_suggest" : {
    "input": [
      "apple iphone 6",
      "iphone 6"
    ],
    "output" : "apple iphone 6",
    "weight" : 11
  }
}

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

关于php - 完成建议elasticsearch中从中间部分匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38178565/

相关文章:

php - 如何将 PDT 时间格式转换为这种格式 `2016-09-13 08:56:55 +0000` - 使用 PHP?

search - gae 可搜索型号信息

jquery - 找到一个刚刚由 jQuery 添加的数据属性

elasticsearch - 仅返回对象中包含特定值的数组元素

python - Elasticsearch 采样聚合未知 key

php - MySQL无法识别韩文字符

php - 在 CodeIgniter 中使用 this->db->e​​scape_str() 函数时出现语法错误

php - 使用 PHP 显示 SQL 数据库中存储的 URL 中的图像

javascript - 如何使用JS搜索表并仅返回匹配的行?

elasticsearch - 如何在 Elasticsearch (aws)中存储日期范围数据并搜索范围?