Elasticsearch中文分词插件IK Analyer安装

来自CloudWiki
跳转至: 导航搜索

下载源码包

IK与ES的版本对应关系:

IK version 	ES version
master 	5.x -> master
5.6.9 	5.6.9
5.6.4 	5.6.4
5.5.3 	5.5.3
5.4.3 	5.4.3
5.3.3 	5.3.3
5.2.2 	5.2.2

执行命令:

wget https://github.com/medcl/elasticsearch-analysis-ik/archive/v5.5.1.zip

下载后随便找一目录解压即可。

如何下载其他版本的ik?打开网址:https://github.com/medcl/elasticsearch-analysis-ik,按下图①②③④⑤操作。

Bd3-21.png

编译源码包

下面进行编译,没安装Maven的请先安装Maven:

//进入一个目录
cd /home/
//创建一个文件夹
mkdir maven
//下载maven的tar包
wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz
//解压tar包
tar -xvf apache-maven-3.5.2-bin.tar.gz
配置环境变量:export M2_HOME=/opt/usr/local/maven/

export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$M2_HOME/bin:$PATH
 
再配置好环境变量后:source /etc/profile
查看是否安装成功:mvn --verison

安装完maven后到ik目录下执行:

mvn clean
mvn compile
mvn package

编译成功后可以看到多了一个target目录:

[root@localhost elasticsearch-analysis-ik-5.5.1]# ls
config  licenses  LICENSE.txt  pom.xml  README.md  src  target

再进入到 /target/releases/ 目录下会看到有elasticsearch-analysis-ik-5.5.1.zip生成

[root@localhost elasticsearch-analysis-ik-5.5.1]# cd target/releases/
[root@localhost releases]# ls
elasticsearch-analysis-ik-5.5.1.zip
[root@localhost releases]#

在es主目录下plugins目录下创建ik目录:

[root@localhost esu]# cd /home/esu/elasticsearch-5.5.1/plugins
[root@localhost plugins]# mkdir ik

再把releases下的elasticsearch-analysis-ik-5.5.1.zip解压到ik目录:

unzip elasticsearch-analysis-ik-5.5.1.zip -d /home/esu/elasticsearch-5.5.1/plugins/ik

此时的ik目录:

[root@localhost ik]# pwd
/home/esu/elasticsearch-5.5.1/plugins/ik
[root@localhost ik]# ls
commons-codec-1.9.jar                httpclient-4.5.2.jar
commons-logging-1.2.jar              httpcore-4.4.4.jar
config                               plugin-descriptor.properties
elasticsearch-analysis-ik-5.5.1.jar

重启ES服务,如果不提示错误,即告安装成功!

验证

创建一个索引

curl -XPUT http://localhost:9200/index

创建一个映射mapping

curl -XPOST http://localhost:9200/index/fulltext/_mapping -H 'Content-Type:application/json' -d'
{
        "properties": {
            "content": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word"
            }
        }
}'

索引一些文档

curl -XPOST http://localhost:9200/index/fulltext/1 -H 'Content-Type:application/json' -d' {"content":"美国留给伊拉克的是个烂摊子吗"}'

{"_index":"index","_type":"fulltext","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"created":true}

curl -XPOST http://localhost:9200/index/fulltext/3 -H 'Content-Type:application/json' -d' {"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}'

curl -XPOST http://localhost:9200/index/fulltext/3 -H 'Content-Type:application/json' -d' {"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}'

高亮查询

curl -XPOST http://localhost:9200/index/_search  -H 'Content-Type:application/json' -d'
{
    "query" : { "match" : { "content" : "中国" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
    }
}
'

结果:

{
    "took": 14,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 2,
        "hits": [
            {
                "_index": "index",
                "_type": "fulltext",
                "_id": "4",
                "_score": 2,
                "_source": {
                    "content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
                },
                "highlight": {
                    "content": [
                        "<tag1>中国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 "
                    ]
                }
            },
            {
                "_index": "index",
                "_type": "fulltext",
                "_id": "3",
                "_score": 2,
                "_source": {
                    "content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
                },
                "highlight": {
                    "content": [
                        "均每天扣1艘<tag1>中国</tag1>渔船 "
                    ]
                }
            }
        ]
    }
}


参考文档:

[1] https://blog.csdn.net/laotoumo/article/details/54899085

[2] https://blog.csdn.net/Abysscarry/article/details/79913521