ES安装中文IK分词器

之前自己一个人折腾ES的时候,安装IK分词器都会失败,以为没有8.x的支持,只有7.x的版本。其实不是,这里将步骤记录下来。

1,访问IK分词器项目地址

查看相应的Readme内容,当前已经支持到ES 8.4.1版本,也就是说一直在更新。

readme

2, 安装方式选择

有两种,安装都比较简单,可以任意选择一种。 

ES安装中文IK分词器_第1张图片

3, 选择elasticsearch plug命令的方式安装。

查看具体ES版本对应的IK分词器链接地址。通过这个link查看:

ES安装中文IK分词器_第2张图片

例如我现在需要安装针对ES8.1.0的

ES安装中文IK分词器_第3张图片

拷贝下link地址以备后用。

ES安装中文IK分词器_第4张图片

登录ES服务器,使用elasticsearch-plugin命令安装:

[es@localhost ~]$ cd /app/es/bin/
[es@localhost bin]$ ./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.1.0/elasticsearch-analysis-ik-8.1.0.zip
-> Installing https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.1.0/elasticsearch-analysis-ik-8.1.0.zip
-> Downloading https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.1.0/elasticsearch-analysis-ik-8.1.0.zip
[=================================================] 100%   
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@     WARNING: plugin requires additional permissions     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.net.SocketPermission * connect,resolve
See https://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y
-> Installed analysis-ik
-> Please restart Elasticsearch to activate any plugins installed

重启ES
安装完成。

4,使用IK分词器。

自 v5.0.0 起移除名为 ik 的analyzer和tokenizer,请分别使用 ik_smart 和 ik_max_word

下面我使用官方的实例进行测试。

1.create a index

[es@localhost bin]$ curl -H "Content-Type:application/json" --cacert /app/es/config/certs/http_ca.crt -u elastic:elastic123 -XPUT "https://192.168.88.8:9200/index" 
{"acknowledged":true,"shards_acknowledged":true,"index":"index"}

2.create a mapping

curl -H "Content-Type:application/json" --cacert /app/es/config/certs/http_ca.crt -u elastic:elastic123 -XPOST "https://192.168.88.8:9200/index/_mapping" -d'
{
        "properties": {
            "content": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_smart"
            }
        }

}'

ES安装中文IK分词器_第5张图片

 如果有"analyzer [ik_smart] has not been configured in mappings"的报错,要注意,如果是ES集群,需要每个集群都安装IK分词器。我只在一个节点运行了,因此我在节点2上也运行ik分词器的安装:

ES安装中文IK分词器_第6张图片

 最后,需要重启ES才能将安装的插件生效。

3.index some docs

curl -H "Content-Type:application/json" --cacert /app/es/config/certs/http_ca.crt -u elastic:elastic123 -XPOST https://192.168.88.8:9200/index/_create/1 -d'
{"content":"内容参考github上的原文"}
'
curl -H "Content-Type:application/json" --cacert /app/es/config/certs/http_ca.crt -u elastic:elastic123 -XPOST https://192.168.88.8:9200/index/_create/2 -d'
{"content":"内容参考github上的原文"}
'
curl -H "Content-Type:application/json" --cacert /app/es/config/certs/http_ca.crt -u elastic:elastic123 -XPOST https://192.168.88.8:9200/index/_create/3 -d'
{"content":"内容参考github上的原文"}
'
curl -H "Content-Type:application/json" --cacert /app/es/config/certs/http_ca.crt -u elastic:elastic123 -XPOST https://192.168.88.8:9200/index/_create/4 -d'
{"content":"中内容参考github上的原文"}
'

4.query with highlighting

curl -H "Content-Type:application/json" --cacert /app/es/config/certs/http_ca.crt -u elastic:elastic123 -XPOST https://192.168.88.8:9200/index/_search -d'
{
    "query" : { "match" : { "content" : "内容参考github上的原文" }},
    "highlight" : {
        "pre_tags" : ["", ""],
        "post_tags" : ["", ""],
        "fields" : {
            "content" : {}
        }
    }
}
'

输出结果:
 如果嫌上面的输出格式不清楚,可以使用Kibana的Dev Tools进行查询:

ES安装中文IK分词器_第7张图片

 


 

你可能感兴趣的:(ELK,es,elasticsearch,ik分词器)