ElasticSearch配置ik中文分词器

一、Versions

ES Version: 2.1.1

IK Version: 1.6.2

说明:ES和IK的版本一定要相互兼容,具体兼容性可以查看下面的git说明

 

二、Install

  1. IK的下载:https://github.com/medcl/elasticsearch-analysis-ik
    里面附有安装步骤可以参考,本步骤是略作补充和说明
  2. compile
    mvn package
    说明:这个使用mvn命令时很可能会出问题,建议使用工具如IDEA等打包

    复制 target/releases/elasticsearch-analysis-ik-{version}.zip to your-es-root/plugins/ik,将zip包解压
    说明:仅将zip包里的文件放到ik文件夹中
  3. 配置config文件
    将项目中config文件下的ik文件夹复制到your-es-root/config
  4. 配置elasticsearch.yml
    在elasticsearch.yml文件的最后添加配置(最简单的配置):

    index :
         analysis :
             analyzer :
                 ik :
                     type: custom
                     tokenizer: ik_max_word
  5. restart ES

三、Quick Example

(以下大部分摘抄github中的步骤,不同的地方会做标红)可以使用chrome的插件postman,非常好用

  1. 创建index

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

     

     

  2. 创建mapping(此处与github上不同)

    curl -XPOST http: //localhost:9200/index/fulltext/_mapping -d'
    {
         "fulltext" : {
                  "_all" : {
                 "analyzer" "ik" ,
                 "search_analyzer" "ik_max_word" ,
                 "term_vector" "no" ,
                 "store" "false"
             },
             "properties" : {
                 "content" : {
                     "type" "string" ,
                     "store" "no" ,
                     "term_vector" "with_positions_offsets" ,
                     "analyzer" "ik" ,
                     "search_analyzer" "ik_max_word" ,
                     "include_in_all" "true" ,
                     "boost" 8
                 }
             }
         }
    }
  3. 添加文档

    curl -XPOST http: //localhost:9200/index/fulltext/1 -d'
    { "content" : "美国留给伊拉克的是个烂摊子吗" }
    '
    curl -XPOST http: //localhost:9200/index/fulltext/2 -d'
    { "content" : "公安部:各地校车将享最高路权" }
    '
    curl -XPOST http: //localhost:9200/index/fulltext/3 -d'
    { "content" : "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船" }
    '
    curl -XPOST http: //localhost:9200/index/fulltext/4 -d'
    { "content" : "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首" }
    '
  4. 查询

    curl -XPOST http: //localhost:9200/index/fulltext/_search  -d'
    {
         "query"  : {  "term"  : {  "content"  "中国"  }},
         "highlight"  : {
             "pre_tags"  : [ "<tag1>" "<tag2>" ],
             "post_tags"  : [ "</tag1>" "</tag2>" ],
             "fields"  : {
                 "content"  : {}
             }
         }
    }
    '
  5. 结果

四、其他

参考github上的相关说明:https://github.com/medcl/elasticsearch-analysis-ik

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值