同义词扩展
2020-06-12 本文已影响0人
乌鲁木齐001号程序员
导论
有些时候,需要给一些词配置同义词,比如用户搜“累了”,什么结果都搜不到,如果给“累了”配置了同义词“酒店”,其搜索结果和搜索“酒店”的结果一样。
创建 synomyms.txt
- 创建目录为:
/home/lixinlei/application/elasticsearch/7.3.0/elasticsearch-node1/plugins/ik/config
; - 把互为同义词的一组词写在一行里;
凯悦,锡箔,红桃
拷贝 synomyms.txt 到其他节点
cp synomyms.txt /home/lixinlei/application/elasticsearch/7.3.0/elasticsearch-node2/plugins/ik/config
cp synomyms.txt /home/lixinlei/application/elasticsearch/7.3.0/elasticsearch-node2/plugins/ik/config
重新定义索引 shop
- 要先删除 shop,再创建 shop,再索引数据到 shop;
- 注意
synonyms_path
后面跟的路径是相对路径,相对于 ES_HOME/config;
# 删除索引 shop
DELETE /shop
# 重建索引 shop,使用自定义的,使用同义词过滤器的分词器
PUT /shop
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1,
"analysis": {
"filter": {
"my_synonym_filter": {
"type": "synonym",
"synonyms_path": "../plugins/ik/config/synomyms.txt"
}
},
"analyzer": {
"ik_syno":{
"type":"custom",
"tokenizer":"ik_smart",
"filter":["my_synonym_filter"]
},
"ik_syno_max":{
"type":"custom",
"tokenizer":"ik_max_word",
"filter":["my_synonym_filter"]
}
}
}
},
"mappings": {
"properties": {
"id":{"type": "integer"},
"name":{"type": "text", "analyzer": "ik_syno_max", "search_analyzer": "ik_syno"},
"tags":{"type": "text", "analyzer": "whitespace", "fielddata": true},
"location":{"type": "geo_point"},
"remark_score":{"type": "double"},
"price_per_man":{"type": "integer"},
"category_id":{"type": "integer"},
"category_name":{"type":"keyword"},
"seller_id":{"type": "integer"},
"seller_remark_score":{"type":"double"},
"seller_disabled_flag":{"type":"integer"}
}
}
}
重新分词 “凯悦”
- 得到“凯悦”是因为自定义词库中有这个词;
- 剩下俩词是因为它们和凯悦一起写进了 synomyms.txt 中;
GET /shop/_analyze
{
"field": "name",
"text": "凯悦"
}
---
{
"tokens" : [
{
"token" : "凯悦",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "锡箔",
"start_offset" : 0,
"end_offset" : 2,
"type" : "SYNONYM",
"position" : 0
},
{
"token" : "红桃",
"start_offset" : 0,
"end_offset" : 2,
"type" : "SYNONYM",
"position" : 0
}
]
}
使用“凯悦”的同义词搜索,得到和搜“凯悦”一样的结果
- BINGO!
GET /shop/_search
{
"query": {
"match": {
"name": "锡箔"
}
}
}