Elasticsearch---中文分词器IK

中文分词器IK:

Analyzer: ik_smart , ik_max_word ,

Tokenizer: ik_smart , ik_max_word

一、IK分词器的安装:

1.下载安装:(下载的版本与ES版本对应)

方式一、下载源代码、编译:(略)

方式二、在线安装命令安装:(替换版本号)

在线安装 ik es插件 命令:

# /elasticsearch-6.2.2/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.2/elasticsearch-analysis-ik-6.2.2.zip

安装结果: /plugin/analysis-ik  

 查看插件安装列表: # sudo /elasticsearch-6.2.0/bin/elasticsearch-plugin list

2. 重启ES:  加载成功  loaded plugin[analysis-ik]

[2019-08-01T23:39:35,470][INFO ][o.e.p.PluginsService     ] [kii7EyF] loaded plugin [analysis-ik]

二. IK分词器的使用

1.创建index:  PUT http://localhost:9200/test_index 

请求:PUT http://localhost:9200/test_index 
结果:
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "test1_index"
}

2. 创建Mapping: 

POST http://localhost:9200/test_index/test_type/_mapping

{
        "properties": {
            "content": {  //test_type里的的Feild
                "type": "text",
                "analyzer": "ik_max_word",  //指定索引时的分析器
                "search_analyzer": "ik_max_word" //指定查询时的分析器
            }
        }

}

3. 添加测试数据:

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

4. 查询(带高亮)

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

三、配置:

1.IKAnalyzer.cfg.xml 配置文件位置:

在 {conf}/analysis-ik/config/IKAnalyzer.cfg.xml 

或在 {plugins}/elasticsearch-analysis-ik-*/config/IKAnalyzer.cfg.xml

2.配置字典和停止库:

IKAnalyzer.cfg.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<comment>IK Analyzer 扩展配置</comment>
	<!--用户可以在这里配置自己的扩展字典 -->
	<entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic</entry>
	 <!--用户可以在这里配置自己的扩展停止词字典-->
	<entry key="ext_stopwords">custom/ext_stopword.dic</entry>
 	<!--用户可以在这里配置远程扩展字典 -->
	<entry key="remote_ext_dict">location</entry>
 	<!--用户可以在这里配置远程扩展停止词字典-->
	<entry key="remote_ext_stopwords">http://xxx.com/xxx.dic</entry>
</properties>

my_stopword.dic文件格式:

3.热更新 IK 分词使用方法:

(1) 配置字典、停止词url地址:

 	<!--用户可以在这里配置远程扩展字典 -->
	<entry key="remote_ext_dict">location</entry>
 	<!--用户可以在这里配置远程扩展停止词字典-->
	<entry key="remote_ext_stopwords">location</entry>

其中 location 是指一个 url,比如 http://yoursite.com/getCustomDict

(2)触发分词热更新:

  1. 该 http 请求需要返回两个头部(header),一个是 Last-Modified,一个是 ETag,这两者都是字符串类型,只要有一个发生变化,该插件就会去抓取新的分词进而更新词库。

  2. 该 http 请求返回的内容格式是一行一个分词,换行符用 \n 即可。

发布了18 篇原创文章 · 获赞 1 · 访问量 3910

猜你喜欢

转载自blog.csdn.net/silmeweed/article/details/98116583