elasticsearch同义词及动态更新

时间:2022-09-03 10:44:13

第一种:
参考地址:http://dev.paperlesspost.com/setting-up-elasticsearch-synonyms/27
1、Add a synonyms file.
2、Create the index with setting and mappings to support synonyms.
Creating a synonyms file
config/ynonyms.txt

# synonyms.txt
sea cow => manatee
cat, feline, lolcat

Setting up index settings and mappings

POST http://localhost:9200/my_index/
{
"settings": {
"index": {
"analysis": {
"analyzer": {
"synonym": {
"tokenizer": "whitespace",
"filter": ["synonym"]
}
},
"filter": {
"synonym": {
"type": "synonym",
"synonyms_path": "synonyms.txt",
"ignore_case": true
}
}
}
}
},
"mappings": {
"animal": {
"properties": {
"name": {
"type": "String",
"analyzer": "synonym"
},
"type": {
"type": "String",
"analyzer": "synonym"
}
}
}
}
}

Bonus: Refreshing synonyms file

curl -XPOST 'localhost:9200/my_index/_close'
curl -XPUT 'localhost:9200/my_index/_settings' -d '
{
"index" : {
"analysis.filter.synonym.synonyms_path" : "synonyms.txt"
}
}'
curl -XPOST 'localhost:9200/my_index/_open'

第二种(支持动态更新):

elasticsearch.yml最后添加以下内容:

index.analysis.analyzer.default.type: ik #默认分词用ik
index:
analysis:
filter:
local_synonym:
type: dynamic_synonym
synonyms_path: dynamic-synonym/synonym.txt
interval:
remote_synonym:
type: dynamic_synonym
synonyms_path: http://127.0.0.1:8082/remote_ext_synonym.txt
interval:
includeIndexs: [test,music,uzai]
excludeIndexs: [authors]
blankSynonymWord: --b,--b
analyzer:
ik:
alias: [ik_analyzer]
type: ik
#type: org.elasticsearch.index.analysis.IkAnalyzerProvider
ik_max_word:
type: ik
use_smart: false
ik_smart:
type: ik
use_smart: true
ik_syno:
type: custom
tokenizer: ik
filter: [local_synonym,remote_synonym]
ik_syno_smart:
type: custom
tokenizer: ik
filter: [local_synonym,remote_synonym]
use_smart: true
standard_syno:
type: custom
filter: [local_synonym,remote_synonym]
tokenizer: standard

下载插件(支持动态更新):

https://github.com/bells/elasticsearch-analysis-dynamic-synonym

1、mvn package
2、copy and unzip target/releases/elasticsearch-analysis-dynamic-synonym-{version}.zip to your-es-root/plugins/dynamic-synonym
注意
1、我用的是elasticsearch-2.4.1,插件版本要对应上
2、dynamic-synonym和ik中的某些jar重复,这样会报错,解决方式:删除重复的jar

参考:

http://www.cnblogs.com/yjf512/p/4789239.html

http://blog.csdn.net/tianzhaixing2013/article/details/51506496

http://blog.csdn.net/yusewuhen/article/details/50685685

http://11670039.blog.51cto.com/11660039/1825728