跳至主要內容

Elasticsearch之IK(ES中文分词插件)安装教程

zhengcog...大约 2 分钟博文搜索引擎Elasticsearch

IK Analyzer 介绍

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

当安装完Elasticsearch之后,默认已经含有一个分词法,就是standard,这个分词法对英文的支持还可以,但是对中文的支持非常差劲,如图所示:

图1
图1

开始安装

本教程基于elasticsearch6.2.4安装,操作系统:Mac OS

1. 使用 elasticsearch-plugin 工具安装

./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.4/elasticsearch-analysis-ik-6.2.4.zip

2. 重启elasticsearch

举个栗子

1. 添加一个索引

curl -X PUT http://localhost:9200/index

2. create a 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"
          }
      }
}'

3. 添加几个测试文档

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

4. 搜索,关键字高亮显示

curl -XPOST http://localhost: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": 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>渔船 "
                  ]
              }
          }
      ]
  }
}
上次编辑于:
贡献者: Hyman
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.5