1

我的弹性搜索文档有一个字段Name,其中包含以下条目:

Samsung Galaxy S3
Samsung Galaxy Ace Duos 3
Samsung Galaxy Duos 3
Samsung Galaxy S2
Samsung Galaxy S (I9000)

在使用以下查询查询此字段时(注意“s”和“3”之间的空格)

{
  "query": {
    "match": {
      "Name": {
        "query": "galaxy s 3",
        "fuzziness": 2,
        "prefix_length": 1
      }
    }
  }
}

"Samsung Galaxy Duos 3"作为相关结果返回,而不是"Samsung Galaxy S3".

我注意到此类任务的模式是忽略任何数字和任何单个字母字符之间的空格,并进行查询。例如 then ,"I-phone 5s"也应该由"I-phone 5 s".

有没有很好的方法来实现这一点?

4

1 回答 1

2

您需要更改分析器以将字符串从文本更改为数字 - 使用正则表达式会有所帮助(这是基于camelcase 分析器):

curl -XPUT 'localhost:9200/myindex/' -d '
     {
         "settings":{
             "analysis": {
                 "analyzer": {
                     "mynewanalyser":{
                         "type": "pattern",
                         "pattern":"([^\\p{L}\\d]+)|(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)"
                     }
                 }
             }
         }
     }'

用你的字符串测试新的分析器:

-XGET 'localhost:9200/myindex/_analyze?analyzer=mynewanalyser&pretty' -d 'Samsung Galaxy S3'
{
  "tokens" : [ {
    "token" : "samsung",
    "start_offset" : 0,
    "end_offset" : 7,
    "type" : "word",
    "position" : 1
  }, {
    "token" : "galaxy",
    "start_offset" : 8,
    "end_offset" : 14,
    "type" : "word",
    "position" : 2
  }, {
    "token" : "s",
    "start_offset" : 15,
    "end_offset" : 16,
    "type" : "word",
    "position" : 3
  }, {
    "token" : "3",
    "start_offset" : 16,
    "end_offset" : 17,
    "type" : "word",
    "position" : 4
  } ]
}
于 2015-01-23T15:57:14.957 回答