Elasticsearch 内置的分词器对中文不友好,会把中文分成单个字来进行全文检索,不能达到想要的结果,在全文检索及新词发展如此快的互联网时代,IK可以进行友好的分词及自定义分词。

IK Analyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包。从2006年12月推出1.0版,目前支持最新版本的ES6.X版本。

ik 带有两个分词器

ik_max_word :会将文本做最细粒度的拆分;尽可能多的拆分出词语

ik_smart:会做最粗粒度的拆分;已被分出的词语将不会再次被其它词语占有

1. 安装插件

如果是集群模式,则每个节点都需要安装ik分词,安装插件完毕后需要重启服务,创建mapping前如果有机器未安装分词,则可能该索引可能为RED,需要删除后重建。

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

2. 创建索引

可以使用CURL命令,6.0版本+也可以在Kibana插件x-pack的DevTools中进行调试API

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

3. 创建mappiing

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

4. 创建文档

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":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
'

5. 查询文档

curl -XPOST http://localhost:9200/index/fulltext/_search  -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>渔船 "
]
}
}
]
}
}

IK支持自定义配置词库,配置文件在config文件夹下的analysis-ik/IKAnalyzer.cfg.xml,字典文件也在同级目录下,可以支持多个选项的配置,ext_dict-自定义词库,ext_stopwords-屏蔽词库。

同时还支持热更新配置,配置remote_ext_dict为http地址,输入一行一个词语,注意文档格式要为UTF8无BOM格式,如果词库发生更新,只需要更新response header中任意一个字段Last-Modified或ETag即可。

<?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>

github地址:

https://github.com/medcl/elasticsearch-analysis-ik

拼音分词

https://github.com/medcl/elasticsearch-analysis-pinyin

Elasticsearch6.0 IKAnalysis分词使用的更多相关文章

  1. Mac环境下 elasticsearch-6.0.1 和 elasticsearch-head 完整安装过程

     安装步骤: 安装java jdk 安装elasticsearch-6.0.1 及中文分词 anslysis-ik-6.0.1 安装elasticsearch-head 下载jdk https://w ...

  2. Elasticsearch6.0及其head插件安装

    Elasticsearch6.0及其head插件安装 1.下载并解压elasticsearch 2.修改elasticsearch.yml文件 # 集群的名字 cluster.name: my-app ...

  3. (新)elasticsearch6.0版本安装head插件

    ES6.0版本安装head插件 1.1 前言 不知道是我电脑问题还是最近的开源软件都比较**,mysql和elasticsearch新版本变动都比较大. elasticsearch6.0貌似已经不支持 ...

  4. ELK(Elasticsearch6.0以上版本head插件安装)

    参考:https://www.cnblogs.com/Onlywjy/p/Elasticsearch.html Elasticsearch6.0不能使用命令直接安装head插件 修改配置文件/etc/ ...

  5. elasticsearch6.0.0源码导入到idea总结

    由于elasticsearch使用了gradle管理依赖,进行构建,再加上对gradle没有接触过. 因此,导入到idea中遇到问题非常多.这里大致罗列下并说明解决方法. 环境背景 gradle版本: ...

  6. elasticsearch6.0版本安装head插件

    ES6.0版本安装head插件 1.1 前言 不知道是我电脑问题还是最近的开源软件都比较**,mysql和elasticsearch新版本变动都比较大. elasticsearch6.0貌似已经不支持 ...

  7. centos6安装elasticsearch6.0

    环境准备 1台centos6操作系统主机,关闭selinux及iptables官方下载elasticsearch6.0软件包:https://artifacts.elastic.co/...官方下载j ...

  8. ElasticSearch6.0 高级应用之 多字段聚合Aggregation(二)

    ElasticSearch6.0 多字段聚合网上完整的资料很少 ,所以作者经过查阅资料,编写了聚合高级使用例子 例子是根据电商搜索实际场景模拟出来的 希望给大家带来帮助! 下面我们开始吧! 1. 创建 ...

  9. ElasticSearch6.0 Java API 使用 排序,分组 ,创建索引,添加索引数据,打分等(一)

    ElasticSearch6.0  Java API  使用     排序,分组 ,创建索引,添加索引数据,打分等 如果此文章对你有帮助,请关注一下哦 1.1 搭建maven 工程  创建web工程 ...

随机推荐

  1. vim常用命令—撤销与反撤销

    命令模式下(即按ESC后的模式) u 撤销 Ctrl r (组合键) 反撤销<后悔撤销>

  2. 十四:Using CGroups with YARN

        Cgroups可以控制linux 上应用程序的资源(内存.CPU)使用,yarn可以使用Cgroups来CPU使用.Cgroups的配置,在yarn-site.xml中设置: 1)启用Cgro ...

  3. HADOOP docker(六):hive简易使用指南

    前言1.hive简介1.1 hive组件与相应功能:1.2 hive的表类型1.3 分区表1.3 分隔符1.4 hive的数据存储2.数据类型2.1 基本数据类型2.1 复杂数据类型2.3 NULL3 ...

  4. [离散化]人潮最多的時段( Interval Partitioning Problem )

    範例:人潮最多的時段( Interval Partitioning Problem ) 一群訪客參加宴會,我們詢問到每一位訪客的進場時刻與出場時刻,請問宴會現場擠進最多人的時段. 換個角度想,想像會場 ...

  5. iframe 随内容自适应高度

    兼容性好的 html代码: <iframe src="enterprise/enter_edit.aspx" id="mainframe" framebo ...

  6. 在 Range 对象中,Min (14)必须小于或等于 max (-1)。

    DataTable dt = ds.Tables[]; DataRow[] drs = dt.Select("Id=" + categoryID ); 解决方法:将参数用单引号阔起 ...

  7. ZOJ 1711 H-Sum It Up

    https://vjudge.net/contest/67836#problem/H Given a specified total t and a list of n integers, find ...

  8. Expected Conditions的常用函数

    Expected Conditions的使用场景有两种  1.直接在断言中使用  2.与WebDriverWait配合使用,动态等待页面上元素出现或者消失 1. title_is: 判断当前页面的ti ...

  9. redis——持久化方式RDB与AOF分析

    https://blog.csdn.net/u014229282/article/details/81121214 redis两种持久化的方式 RDB持久化可以在指定的时间间隔内生成数据集的时间点快照 ...

  10. mybatis小结-001

    代码不能直接copy运行,有些内容是为了说明知识点 1,sqlMqpConfig.xml <?xml version="1.0" encoding="UTF-8&q ...