1 |
https://github.com/medcl/elasticsearch-analysis-ik/ |
插入测试数据
1 2 3 4 5 6 7 8 |
POST /index/text/1 {"content":"美国留给伊拉克的是个烂摊子吗"} POST /index/text/2 {"content":"公安部:各地校车将享最高路权"} POST /index/text/3 {"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"} POST /index/text/4 {"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"} |
检测数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
POST /index/_search { "query" : { "match" : { "content" : "中国" }}, "highlight" : { "pre_tags" : ["<tag1>", "<tag2>"], "post_tags" : ["</tag1>", "</tag2>"], "fields" : { "content" : {} } } } #查询时分词有问题,中国被拆成两个字进行建立索引 |
配置中文分词器
1)上传插件的安装(集群中所有机器都执行)
1 2 3 4 |
[root@es01 ~]# rz [root@es01 ~]# ll -rw-r--r-- 1 root root 4504556 2020-05-19 00:22 elasticsearch-analysis-ik-6.6.0.zip |
2)解压
1 2 3 |
[root@es01 ~]# mkdir /usr/share/elasticsearch/plugins/ik -p [root@es01 ~]# unzip elasticsearch-analysis-ik-6.6.0.zip -d /usr/share/elasticsearch/plugins/ik |
3)编辑配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[root@es03 ~]# vim /usr/share/elasticsearch/plugins/ik/config/IKAnalyzer.cfg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>IK Analyzer 扩展配置</comment> <!--用户可以在这里配置自己的扩展字典 --> <entry key="ext_dict">/etc/elasticsearch/config/my.dic</entry> <!--用户可以在这里配置自己的扩展停止词字典--> <entry key="ext_stopwords"></entry> <!--用户可以在这里配置远程扩展字典 --> <!-- <entry key="remote_ext_dict">words_location</entry> --> <!--用户可以在这里配置远程扩展停止词字典--> <!-- <entry key="remote_ext_stopwords">words_location</entry> --> </properties> |
4)编辑分词文件
1 2 3 |
[root@es03 ~]# cat /etc/elasticsearch/config/my.dic 中国 |
5)重启服务
1 2 |
[root@es01 ~]# systemctl restart elasticsearch.service |
6)重新插入数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
1.建立索引 PUT /news 3.创建mapping POST /news/text/_mapping { "properties": { "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" } } } 3.插入数据 POST /news/text/1 {"content":"美国留给伊拉克的是个烂摊子吗"} POST /news/text/2 {"content":"公安部:各地校车将享最高路权"} POST /news/text/3 {"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"} POST /news/text/4 {"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"} |
7)再次查询关键字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
POST /news/_search { "query" : { "match" : { "content" : "中国" }}, "highlight" : { "pre_tags" : ["<tag1>", "<tag2>"], "post_tags" : ["</tag1>", "</tag2>"], "fields" : { "content" : {} } } } #分词正确 |