添加近义词
echo '凯悦,锡伯,红桃' > vim synonyms.txt
cp synonyms.txt /opt/elasticsearch8.0.0/node2/config/analysis-ik/ cp synonyms.txt /opt/elasticsearch8.0.0/node3/config/analysis-ik/
要点:
增加analysis,处定义分词器修改name的分词器
DELETE /shop PUT /shop { "settings": { "number_of_shards": 1, "number_of_replicas": 1, "analysis": { "filter": { "my_synonym_filter": { "type": "synonym", "synonyms_path": "analysis-ik/synonyms.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_word","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"} } } }
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 } ] }
如果结构之前就存在,要让同义词起作用,需要先删除原索引再重建结构(如上)如果是新索引,则不需要行该步骤 .
以下会查询相同的内容
GET /shop/_search { "query": { "match": { "name": "锡伯" } } } GET /shop/_search { "query": { "match": { "name": "凯悦" } } } GET /shop/_search { "query": { "match": { "name": "红桃" } } }