自带组件
比较简单,但是修改同义词库需要重启es。
1、同义词词库
准备一个同义词词库,每行一个同义词词组,例子 elasticsearch/config/analysis/syno.dic
西红柿,番茄,tomato
马铃薯,土豆
2、配置分词器
PUT /people_v3
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0,
"analysis": {
"filter": {
"my_synonym_filter": {
"type": "synonym",
"synonyms_path" : "analysis/syno.dic"
}
},
"analyzer": {
"my_synonyms": {
"tokenizer": "ik_max_word",
"filter": [
"lowercase",
"my_synonym_filter"
]
}
}
}
}
}
效果:
GET people_v3/_analyze
{
"text": "我爱土豆",
"analyzer": "my_synonyms"
}
{
"tokens": [
{
"token": "我",
"start_offset": 0,
"end_offset": 1,
"type": "CN_CHAR",
"position": 0
},
{
"token": "爱",
"start_offset": 1,
"end_offset": 2,
"type": "CN_CHAR",
"position": 1
},
{
"token": "土豆",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 2
},
{
"token": "马铃薯",
"start_offset": 2,
"end_offset": 4,
"type": "SYNONYM",
"position": 2
}
]
}