注: elasticsearch 版本6.2.2html
域名 ip master 192.168.0.120 slave1 192.168.0.121 slave2 192.168.0.122
在github中搜索ik,找到"medcl/elasticsearch-analysis-ik",并找到https://github.com/medcl/elasticsearch-analysis-ik/releases,选择本身须要的版本:java
或者git
如上图所示,选择和elsticsearch 匹配的版本,并下载zip包。github
①解压elasticsearch-analysis-ik-6.2.2.zipjson
②打开eclispe导入 maven项目服务器
下一步微信
③导入后,使用maven build...编译jar包app
弹出编辑框:eclipse
点击“Run”执行curl
完成后,在target文件夹上右键 选择 Refresh,如图所示:
把编译好的jar包上传到master服务器上,
执行命令安装:
[spark@master ~]$ cd /opt/ [spark@master opt]$ unzip elasticsearch-analysis-ik-6.2.2.zip -d /opt/elasticsearch-6.2.2/plugins/ Archive: elasticsearch-analysis-ik-6.2.2.zip creating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/ inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/plugin-descriptor.properties creating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/config/ inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/config/extra_main.dic inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/config/extra_single_word.dic inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/config/extra_single_word_full.dic inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/config/extra_single_word_low_freq.dic inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/config/extra_stopword.dic inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/config/IKAnalyzer.cfg.xml inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/config/main.dic inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/config/preposition.dic inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/config/quantifier.dic inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/config/stopword.dic inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/config/suffix.dic inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/config/surname.dic inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/elasticsearch-analysis-ik-6.2.2.jar inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/httpclient-4.5.2.jar inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/httpcore-4.4.4.jar inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/commons-logging-1.2.jar inflating: /opt/elasticsearch-6.2.2/plugins/elasticsearch/commons-codec-1.9.jar [spark@master opt]$ cd /opt/elasticsearch-6.2.2/plugins/
[spark@master plugins]$ mv elasticsearch/ ik/
slave1,slave2一样安装,这里省略。。
master,slave1,slave2三台服务器安装完成后,重启elasticsearch 便可加载ik分词器。
1) 删除、建立索引:
curl -Xdelete "http://192.168.0.120:9200/index" curl -Xput "http://192.168.0.120:9200/index"
2)使用index索引建立mapping(对字段‘content’进行中文分词):
curl -XPOST "http://192.168.0.120:9200/index/fulltext/_mapping" -H 'Content-Type: application/json' -d' { "properties": { "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" } } }'
3)先添加4条记录:
curl -XPOST "http://192.168.0.120:9200/index/fulltext/1" -H 'Content-Type: application/json' -d' {"content":"美国留给伊拉克的是个烂摊子吗"}' curl -XPOST "http://192.168.0.120:9200/index/fulltext/2" -H 'Content-Type: application/json' -d' {"content":"公安部:各地校车将享最高路权"}' curl -XPOST "http://192.168.0.120:9200/index/fulltext/3" -H 'Content-Type: application/json' -d' {"content":"中韩渔警冲突调查:韩警平均天天扣1艘中国渔船"}' curl -XPOST "http://192.168.0.120:9200/index/fulltext/4" -H 'Content-Type: application/json' -d' {"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}'
4)执行统计:
curl -XPOST "http://192.168.0.120:9200/index/fulltext/_search" -H 'Content-Type: application/json' -d' { "query" : { "match" : { "content" : "中国" }}, "highlight" : { "pre_tags" : ["<tag1>", "<tag2>"], "post_tags" : ["</tag1>", "</tag2>"], "fields" : { "content" : {} } } }'
返回结果:
{ "took": 133, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.6489038, "hits": [ { "_index": "index", "_type": "fulltext", "_id": "4", "_score": 0.6489038, "_source": { "content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首" }, "highlight": { "content": [ "<tag1>中国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首" ] } }, { "_index": "index", "_type": "fulltext", "_id": "3", "_score": 0.2876821, "_source": { "content": "中韩渔警冲突调查:韩警平均天天扣1艘中国渔船" }, "highlight": { "content": [ "中韩渔警冲突调查:韩警平均天天扣1艘<tag1>中国</tag1>渔船" ] } } ] } }
5)再添加3条记录:
curl -XPOST "http://192.168.0.120:9200/index/fulltext/5" -H 'Content-Type: application/json' -d' {"content":"俄侦委:俄一辆卡车渡河时翻车 致2名中国游客遇难"}' curl -XPOST "http://192.168.0.120:9200/index/fulltext/6" -H 'Content-Type: application/json' -d' {"content":"韩国银行面向中国留学生推出微信支付服务"}' curl -XPOST "http://192.168.0.120:9200/index/fulltext/7" -H 'Content-Type: application/json' -d' {"content":"印媒:中国东北“锈带”在困境中反击"}'
6)从新执行统计:
curl -XPOST "http://192.168.0.120:9200/index/fulltext/_search" -H 'Content-Type: application/json' -d' { "query" : { "match" : { "content" : "中国" }}, "highlight" : { "pre_tags" : ["<tag1>", "<tag2>"], "post_tags" : ["</tag1>", "</tag2>"], "fields" : { "content" : {} } } }'
返回结果:
{ "took": 41, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 5, "max_score": 0.6785375, "hits": [ { "_index": "index", "_type": "fulltext", "_id": "7", "_score": 0.6785375, "_source": { "content": "印媒:中国东北“锈带”在困境中反击" }, "highlight": { "content": [ "印媒:<tag1>中国</tag1>东北“锈带”在困境中反击" ] } }, { "_index": "index", "_type": "fulltext", "_id": "6", "_score": 0.47000363, "_source": { "content": "韩国银行面向中国留学生推出微信支付服务" }, "highlight": { "content": [ "韩国银行面向<tag1>中国</tag1>留学生推出微信支付服务" ] } }, { "_index": "index", "_type": "fulltext", "_id": "4", "_score": 0.44000342, "_source": { "content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首" }, "highlight": { "content": [ "<tag1>中国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首" ] } }, { "_index": "index", "_type": "fulltext", "_id": "5", "_score": 0.2876821, "_source": { "content": "俄侦委:俄一辆卡车渡河时翻车 致2名中国游客遇难" }, "highlight": { "content": [ "俄侦委:俄一辆卡车渡河时翻车 致2名<tag1>中国</tag1>游客遇难" ] } }, { "_index": "index", "_type": "fulltext", "_id": "3", "_score": 0.2876821, "_source": { "content": "中韩渔警冲突调查:韩警平均天天扣1艘中国渔船" }, "highlight": { "content": [ "中韩渔警冲突调查:韩警平均天天扣1艘<tag1>中国</tag1>渔船" ] } } ] } }
IK支持自定义配置词库,配置文件在config文件夹下的analysis-ik/IKAnalyzer.cfg.xml,字典文件也在同级目录下,能够支持多个选项的配置,ext_dict-自定义词库,ext_stopwords-屏蔽词库。
同时还支持热更新配置,配置remote_ext_dict为http地址,输入一行一个词语,注意文档格式要为UTF8无BOM格式,若是词库发生更新,只须要更新response header中任意一个字段Last-Modified或ETag便可。
[spark@master config]$ pwd /opt/elasticsearch-6.2.2/plugins/ik/config [spark@master config]$ more 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"></entry> <!--用户能够在这里配置本身的扩展中止词字典--> <entry key="ext_stopwords"></entry> <!--用户能够在这里配置远程扩展字典 --> <!-- <entry key="remote_ext_dict">words_location</entry> --> <!--用户能够在这里配置远程扩展中止词字典--> <!-- <entry key="remote_ext_stopwords">words_location</entry> --> </properties> [spark@master config]$
参考:
《https://blog.csdn.net/moxiong3212/article/details/79338586》
《https://www.cnblogs.com/gaoxu387/p/7889626.html》