嘿~ 今天天气不错嘛

同义词偏序/扩展和收缩

Elasticsearch | 作者 zz_hello | 发布于2018年10月08日 | 阅读数:2675

版本es6.4,分词器IK
问题描述:
现在的需求是在搜索比如‘水果’的时候能查询出‘水果’,‘苹果’,‘香蕉’,‘西瓜’等等,而在单独搜索‘苹果’的时候不会出现‘水果’。
”水果=>苹果,香蕉,西瓜“
做法及结果
PUT /fruit
{
"settings": {
"index": {
"analysis": {
"filter": {
"my_synonym_filter": {
"type": "synonym",
"synonyms": [
"水果=>苹果,香蕉,西瓜"
]
}
},
"analyzer": {
"my_synonyms": {
"tokenizer": "ik_smart",
"filter": [
"lowercase",
"my_synonym_filter"
]
}
}
}
}
},
"mappings": {
"products": {
"properties": {
"price": {
"type": "long"
},
"productID": {
"type": "text",
"analyzer": "my_synonyms"
}
}
}
}
}
POST /fruit/products/_bulk
{"index": {"_id": 1}}
{ "price" : 10, "productID" : "水果" }
{ "index": { "_id": 2 }}
{ "price" : 20, "productID" : "苹果" }
{ "index": { "_id": 3 }}
{ "price" : 30, "productID" : "香蕉" }
{ "index": { "_id": 4 }}
{ "price" : 30, "productID" : "西瓜" }

GET /fruit/products/_search
{
"query": {
"match": {
"productID": {
"analyzer": "my_synonyms",
"query": "西瓜"
}
}
}
}
这样查询出来的结果有西瓜和水果。不满足需求。

捕获2.PNG

 
但是用英文,并且在建立索引阶段不加mapping,而在查询阶段加入analyzer时是有效果的。如下
PUT /fruit1
{
"settings": {
"index": {
"analysis": {
"filter": {
"my_synonym_filter": {
"type": "synonym",
"synonyms": [
"fruit=>apple,banana,watermelon"
]
}
},
"analyzer": {
"my_synonyms": {
"tokenizer": "ik_smart",
"filter": [
"lowercase",
"my_synonym_filter"
]
}
}
}
}
}
}
POST /fruit1/products/_bulk
{"index": {"_id": 1}}
{ "price" : 10, "productID" : "fruit" }
{ "index": { "_id": 2 }}
{ "price" : 20, "productID" : "apple" }
{ "index": { "_id": 3 }}
{ "price" : 30, "productID" : "banana" }
{ "index": { "_id": 4 }}
{ "price" : 30, "productID" : "watermelon" }

GET /fruit1/products/_search
{
"query": {
"match": {
"productID": {
"analyzer": "my_synonyms",
"query": "watermelon"
}
}
}
}

捕获1.PNG

 
此时的
GET /fruit1/_mapping/products

得到的结果是没有分析器的,如图

捕获3.PNG

 
但是用了中文分词器之后,如果不在建立索引的时候加入mapping,
GET /fruit/_mapping/products是和上面一样,没有分析器。而且在查询的时候加入分析器是没用的。。。
"analyzer": "my_synonyms"这句话没有用,而且查询没结果。
 
这个文档https://www.elastic.co/guide/c ... .html
最后的类型扩展中提到的就是上面的做法,在索引时不使用类型扩展,而在查询时使用,但是用了中文分词之后没有用啊。。
 
所以请教给位大神同义词偏序这个怎么做呢
 
 
 
 
 
已邀请:

zz_hello

赞同来自: zyb1994111 XZ

"mappings": {
"products": {
"properties": {
"price": {
"type": "long"
},
"productID": {
"type": "text",
"analyzer":"ik_smart",
"search_analyzer": "my_synonyms"
}
}
}
}
在mapping里面这样设置就可以了

rochy - rochy_he

赞同来自:

你的第一种方法是可以的
请仔细理解业务的需求和实现的效果

要回复问题请先登录注册