ElasticSearch 中文同义词实现

https://blog.csdn.net/xsdxs/article/details/52806499

参考以下两个网址,但运行报错,以下是我自己改进方式和使用总结。
https://github.com/medcl/elasticsearch-analysis-ik/issues/93
http://elasticsearch.cn/?/question/29
本文前提默认已经装好ES和IK插件。

1:elasticserach.yml 最后一行添加如下内容(该文件位于elasticsearch-x.x.x/config目录下):
index.analysis.analyzer.default.type: ik

2:在elasticsearch-x.x.x/config目录下新建同义词文件synonyms.txt。
其中,synonyms.txt 编码格式为’utf-8’,内容建议为空。

3:创建索引

curl -XPUT localhost:9200/test -d'
{
"settings": {
"index": {
"analysis": {
"analyzer": {
"jt_cn": {
"type": "custom",
"use_smart": "true",
"tokenizer": "ik_smart",
"filter": ["jt_tfr","jt_sfr"],
"char_filter": ["jt_cfr"]
},
"ik_smart": {
"type": "ik",
"use_smart": "true"
},
"ik_max_word": {
"type": "ik",
"use_smart": "false"
}
},
"filter": {
"jt_tfr": {
"type": "stop",
"stopwords": [" "]
},
"jt_sfr": {
"type": "synonym",
"synonyms_path": "synonyms.txt"
}
},
"char_filter": {
"jt_cfr": {
"type": "mapping",
"mappings": [
"| => \|"
]
}
}
}
}
}
}'
4:创建映射

curl -X PUT localhost:9200/test/haizhi/_mapping -d '{
"haizhi": {
"properties": {
"title": {
"include_in_all": true,
"analyzer": "jt_cn",
"term_vector": "with_positions_offsets",
"boost": 8,
"store": true,
"type": "string"
}
}
}
}'

5:插入数据

curl -XPUT localhost:9200/test/haizhi/1 -d '{
"title": "番茄"
}'
curl -XPUT localhost:9200/test/haizhi/2 -d '{
"title": "西红柿"
}'
curl -XPUT localhost:9200/test/haizhi/3 -d '{
"title": "我是西红柿"
}'
curl -XPUT localhost:9200/test/haizhi/4 -d '{
"title": "我是番茄"
}'
curl -XPUT localhost:9200/test/haizhi/5 -d '{
"title": "土豆"
}'
curl -XPUT localhost:9200/test/haizhi/6 -d '{
"title": "aa"
}'
6:查询1

curl -XPOST 'localhost:9200/test/haizhi/_search?pretty' -d '
{
"query": {
"match_phrase": {
"title": {
"query": "西红柿",
"analyzer": "jt_cn"
}
}
},
"highlight": {
"pre_tags": [
"",
""
],
"post_tags": [
"",
""
],
"fields": {
"title": {}
}
}
}'
结果如下
这里写图片描述
7:查询2
我们知道“西红柿”和“番茄”是同义词,我们在同义词词典(synonyms.txt)中添加如下内容,并重启ES,再用第6步的查询。

Example:

西红柿, 番茄

结果如下,成功匹配同义词
这里写图片描述
8:查询3
修改同义词词典(synonyms.txt)为如下内容,重启ES。

Example:

西红柿, 番茄
超级土豆, 土豆

查询如下:

curl -XPOST 'localhost:9200/test/haizhi/_search?pretty' -d '
{
"query": {
"match_phrase": {
"title": {
"query": "超级土豆",
"analyzer": "jt_cn"
}
}
}
}'

结果如下,查不到结果
这里写图片描述
9:查询4
在{plugins}/elasticsearch-analysis-ik-*/config/custom/mydict.dic词典中新加“超级土豆”一词,并且重启ES。

  • 结果如下,成功用“超级土豆”搜索到“土豆”一词
    这里写图片描述

10:小结

同义词字典或是IK用户自定义词典更新,必须每次重启elasticsearch才有效。
同义词词对是必须能被完成切分的词语。
比如在synonyms.txt 文件中增加同义词对: ‘超级土豆’ – ‘土豆’ 。但在实际的搜索中用“超级土豆”是搜不到“土豆”的。因为“超级土豆”会被切分为多个词语。必须在{plugins}/elasticsearch-analysis-ik-*/config/custom/mydict.dic词典中新加“超级土豆”一词,才能用“超级土豆”一词搜出“土豆”。

最新文章

  1. Win7安装MySQL-5.7.16过程
  2. jQuery:实现两个<select>控件的互移操作
  3. C#_闭包陷阱
  4. gitlab安装过程总结
  5. JavaScript 基础第二天
  6. ACM数学问题分类(汇总帖)
  7. 转】Nginx+tomcat配置集群负载均衡
  8. 循环json里面的数据
  9. Win7下通过easyBCD引导安装Ubuntu14.04
  10. Unhandled Exxception “Unhandled exception type IOException”?
  11. VC获取精确时间的做法
  12. C++模板编程
  13. CareerCup它1.8 串移包括问题
  14. WGCNA算法研究笔记
  15. Remove Element leetcode
  16. ReactiveCocoa应用篇(一)
  17. Java工程师可以从事的几大职业
  18. 《剑指Offer》第20题(Java实现):定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
  19. Cylinder Candy(积分)
  20. 基于Docker的redis集群搭建

热门文章

  1. NC使用技巧
  2. Java : java基础(4) 线程
  3. pycharm中每次创建py文件时就自动生成代码头,以及出现SyntaxError:Non-ASCII 。。。问题
  4. 《PHP内核探索系列文章》系列分享专栏
  5. C语言程序设计·谭浩强(第四版)第二章课后习题的答案,算法——程序的灵魂
  6. logger模块的使用
  7. linux文件操作篇 (二) 打开和关闭文件
  8. 【转】mysql索引最左匹配原则的理解
  9. 如何删除TFS项目
  10. react实现页面切换动画效果