同义词分析器不给出结果

时间:2015-10-30 03:59:24

标签: elasticsearch

我定义了以下自定义分析器:

{
  "analysis": {
    "analyzer": {
      "products-alike": {
        "filter": [
          "lowercase",
          "product-db"
        ],
        "tokenizer": "standard"
      }
    },
    "filter": {
      "product-db": {
        "type": "synonym",
        "synonyms": [
          "Xiaomi,Mi,Mi3,Mi4,Redmi",
          "OnePlus,OnePlusOne,OnePlus1,OnePlus2"
        ]
      }
    }
  }
}

现在我已将其映射到必填字段并完成查询。但是只有完全匹配的结果,例如,如果我查询Xiaomi,就会有结果,但MiMi3不会让我任意。为什么会发生这种情况,任何人都可以帮助实现这个目标吗?

1 个答案:

答案 0 :(得分:2)

您只需要用小写而不是CamelCase编写所有同义词,如下所示:

{
  "analysis": {
    "analyzer": {
      "products-alike": {
        "filter": [
          "lowercase",
          "product-db"
        ],
        "tokenizer": "standard"
      }
    },
    "filter": {
      "product-db": {
        "type": "synonym",
        "synonyms": [
          "xiaomi,mi,mi3,mi4,redmi",
          "oneplus,oneplusone,oneplus1,oneplus2"
        ]
      }
    }
  }
}

在此之后,它会起作用,即如果您查询Mi3,则您将匹配所有同义词令牌:

curl -XGET 'localhost:9200/your_index/_analyze?analyzer=products-alike&pretty' -d 'Mi3'

结果:

{
  "tokens" : [ {
    "token" : "xiaomi",
    "start_offset" : 0,
    "end_offset" : 3,
    "type" : "SYNONYM",
    "position" : 1
  }, {
    "token" : "mi",
    "start_offset" : 0,
    "end_offset" : 3,
    "type" : "SYNONYM",
    "position" : 1
  }, {
    "token" : "mi3",
    "start_offset" : 0,
    "end_offset" : 3,
    "type" : "SYNONYM",
    "position" : 1
  }, {
    "token" : "mi4",
    "start_offset" : 0,
    "end_offset" : 3,
    "type" : "SYNONYM",
    "position" : 1
  }, {
    "token" : "redmi",
    "start_offset" : 0,
    "end_offset" : 3,
    "type" : "SYNONYM",
    "position" : 1
  } ]
}