ElasticSearch安装ik分词插件

一、IK简介

IK Analyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包。最初,它是以开源项目Luence为应用主体的,结合词典分词和文法分析算法的中文分词组件。从3.0版本开 始,IK发展为面向Java的公用分词组件,独立于Lucene项目,同时提供了对Lucene的默认优化实现。在2012版本中,IK实现了简单的分词 歧义排除算法,标志着IK分词器从单纯的词典分词向模拟语义分词衍化。

二、安装IK分词插件

1、获取分词的依赖包

通过git clone https://github.com/medcl/elasticsearch-analysis-ik,下载分词器源码,然后进入下载目录:

执行命令:mvn clean package,打包生成elasticsearch-analysis-ik-1.2.5.jar

将这个jar拷贝到ES_HOME/plugins/analysis-ik目录下面,如果没有该目录,则先创建该目录。

2、ik目录拷贝

将下载目录中的ik目录拷贝到ES_HOME/config目录下面。

3、分词器配置

打开ES_HOME/config/elasticsearch.yml文件,在文件最后加入如下内容:

index:
  analysis:                   
    analyzer:      
      ik:
          alias: [ik_analyzer]
          type: org.elasticsearch.index.analysis.IkAnalyzerProvider
      ik_max_word:
          type: ik
          use_smart: false
      ik_smart:
          type: ik
          use_smart: true
或:
index.analysis.analyzer.default.type: ik

ok!插件安装已经完成,请重新启动ES,接下来测试ik分词效果啦!

三、ik分词测试

1、创建一个索引,名为index

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

如果Elasticsearch 安装完IK分词插件后使用curl命令创建索引出错如下:

nested: ClassNotFoundException[org.apache.http.client.ClientProtocolException]; ","status":500}

解决方法:

Elasticsearch lib 文件夹中 引入:httpclient-4.3.5.jar和httpcore-4.3.2.jar。

下载地址:http://hc.apache.org/httpcomponents-client-4.4.x/download.html

2、为索引index创建mapping

curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
{
    "fulltext": {
             "_all": {
            "analyzer": "ik"
        },
        "properties": {
            "content": {
                "type" : "string",
                "boost" : 8.0,
                "term_vector" : "with_positions_offsets",
                "analyzer" : "ik",
                "include_in_all" : true
            }
        }
    }
}'

3、测试

curl 'http://localhost:9200/index/_analyze?analyzer=ik&pretty=true' -d '
{
"text":"世界如此之大"
}'

显示结果如下:

{
  "tokens" : [ {
    "token" : "text",
    "start_offset" : 4,
    "end_offset" : 8,
    "type" : "ENGLISH",
    "position" : 1
  }, {
    "token" : "世界",
    "start_offset" : 11,
    "end_offset" : 13,
    "type" : "CN_WORD",
    "position" : 2
  }, {
    "token" : "如此",
    "start_offset" : 13,
    "end_offset" : 15,
    "type" : "CN_WORD",
    "position" : 3
  }, {
    "token" : "之大",
    "start_offset" : 15,
    "end_offset" : 17,
    "type" : "CN_WORD",
    "position" : 4
  } ]
}

官方资料: https://github.com/medcl/elasticsearch-analysis-ik

原文地址:https://www.cnblogs.com/hunttown/p/5450635.html