由于elasticsearch 更新实在太快,配置同义词的资料层次不齐,费尽千辛万苦终于找到了。本文通过一个同义词搜索的简单实例来说明ik同义词的配置。

环境介绍

这点很重要,本文是基于elasticsearch7.1.1,ik7.1.1。

配置同义词文件

上传文件的路径位于 es 安装路径的 config 下,新建 analysis 文件夹,并新建 synonym.txt 文件。

此处我是通过配置docker-compose 完成文件挂载,可以简单参考。

version: '3'
services:
es:
build: .
image: es
container_name: master
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
volumes:
- ./elasticsearch/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./elasticsearch/config/analysis/synonym.txt:/usr/share/elasticsearch/config/analysis/synonym.txt
- esdata:/usr/share/elasticsearch/data
- eslogs:/usr/share/elasticsearch/logs
ports:
- 9200:9200
- 9300:9300

synonym.txt

西红柿,番茄

创建index

在kibana下输入

PUT /goods
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0,
"analysis": {
"filter": {
"word_sync": {
"type": "synonym",
"synonyms_path": "analysis/synonym.txt"
}
},
"analyzer": {
"ik_sync_smart": {
"filter": [
"word_sync"
],
"type": "custom",
"tokenizer": "ik_smart"
}
}
}
},
"mappings": { "properties": {
"goodsName": {
"type": "text",
"analyzer": "ik_sync_smart",
"search_analyzer": "ik_sync_smart"
},
"goodsContent": {
"type": "text",
"analyzer": "ik_sync_smart",
"search_analyzer": "ik_sync_smart"
}
}
}
}

插入数据

POST /goods/_doc/1
{
"goodsName": "西红柿",
"goodsContent": "新疆的西红柿"
}

查询测试

POST /goods/_doc/_search
{
"query": {
"match": {
"goodsContent": "番茄"
}
}
}

返回西红柿结果。

实际应用场景下仅需修改index 即可。

05-24 09:43