我定义了以下自定义分析器:
{
"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
,就会有结果,但Mi
或Mi3
不会让我任意。为什么会发生这种情况,任何人都可以帮助实现这个目标吗?
答案 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
} ]
}