起因

需要在ES中使用聚合进行统计分析,但是聚合字段值为中文,ES的默认分词器对于中文支持非常不友好:会把完整的中文词语拆分为一系列独立的汉字进行聚合,显然这并不是我的初衷。我们来看个实例:

POST http://192.168.80.133:9200/my_index_name/my_type_name/_search
{
"size": 0,
"query" : {
"range" : {
"time": {
"gte": 1513778040000,
"lte": 1513848720000
}
}
},
"aggs": {
"keywords": {
"terms": {"field": "keywords"},
"aggs": {
"emotions": {
"terms": {"field": "emotion"}
}
}
}
}
}

输出结果:

{
"took": 22,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 32,
"max_score": 0.0,
"hits": []
},
"aggregations": {
"keywords": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "力", # 完整的词被拆分为独立的汉字
"doc_count": 2,
"emotions": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": -1,
"doc_count": 1
},
{
"key": 0,
"doc_count": 1
}
]
}
},
{
"key": "动",
"doc_count": 2,
"emotions": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": -1,
"doc_count": 1
},
{
"key": 0,
"doc_count": 1
}
]
}
}
]
}
}
}

既然ES的默认分词器对于中文支持非常不友好,那么有没有可以支持中文的分词器呢?如果有,该如何使用呢?

第一个问题,万能的谷歌告诉了我结果,已经有了支持中文的分词器,而且是开源实现:IK Analysis for Elasticsearch,详见:https://github.com/medcl/elasticsearch-analysis-ik。

秉着“拿来主义”不重复造轮子的指导思想,直接先拿过来使用一下,看看效果怎么样。那么,如何使用IK分词器呢?其实这是一个ES插件,直接安装并对ES进行相应的配置即可。

安装IK分词器

我的ES版本为2.4.1,需要下载的IK版本为:1.10.1(注意:必须下载与ES版本对应的IK,否则不能使用)。

1.下载,编译IK

wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v1.10.1/elasticsearch-analysis-ik-1.10.1.zip
unzip elasticsearch-analysis-ik-1.10.1.zip
cd elasticsearch-analysis-ik-1.10.1
mvn clean package

在elasticsearch-analysis-ik-1.10.1\target\releases目录下生成打包文件:elasticsearch-analysis-ik-1.10.1.zip。

2.在ES中安装IK插件

将上述打包好的IK插件:elasticsearch-analysis-ik-1.10.1.zip拷贝到ES/plugins目录下,执行解压。

unzip elasticsearch-analysis-ik-1.10.1.zip
rm -rf elasticsearch-analysis-ik-1.10.1.zip # 解压完之后一定要删除这个zip包,否则在启动ES时报错

重启ES。

使用IK分词器

安装IK分词器完毕之后,就可以在ES使用了。

第一步:新建index

PUT http://192.168.80.133:9200/my_index_name

第二步:给将来要使用的doc字段添加mapping

在这里我在ES中存储的doc格式如下:

{
"nagtive_kw": []
"is_all": false,
"emotion": 0,
"focuce": false,
"keywords": ["动力","外观","油耗"], // 在keywords字段上进行聚合分析
"source": "汽车之家",
"time": -1,
"machine_emotion": 0,
"title": "no title",
"spider": "qczj_index",
"content": {},
"url": "http://xxx",
"brand": "宝马",
"series": "宝马1系",
"model": "2017款"
}

需要在keywords字段上进行聚合分析,所以给keywords字段添加mapping设置:

POST http://192.168.80.133:9200/my_index_name/my_type_name/_mapping
{
"properties": {
"keywords": { # 设置keywords字段使用ik分词器
"type": "string",
"store": "no",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart",
"boost": 8
}
}
}

注意: 在设置mapping时有一个小插曲,我根据IK的官网设置“keywords”的type为“text”时报错:

POST http://192.168.80.133:9200/my_index_name/my_type_name/_mapping
{
"properties": {
"keywords": {
"type": "text", # text类型在2.4.1版本中不支持
"store": "no",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart",
"boost": 8
}
}
}

报错:

{
"error": {
"root_cause": [
{
"type": "mapper_parsing_exception",
"reason": "No handler for type [text] declared on field [keywords]"
}
],
"type": "mapper_parsing_exception",
"reason": "No handler for type [text] declared on field [keywords]"
},
"status": 400
}

这是因为我使用的ES版本比较低:2.4.1,而text类型是ES5.0之后才添加的类型,所以不支持。在ES2.4.1版本中需要使用string类型。

第三步:添加doc对象

POST http://192.168.80.133:9200/my_index_name/my_type_name/
{
"nagtive_kw": ["动力","外观","油耗"]
"is_all": false,
"emotion": 0,
"focuce": false,
"keywords": ["动力","外观","油耗"], // 在keywords字段上进行聚合分析
"source": "汽车之家",
"time": -1,
"machine_emotion": 0,
"title": "从动次打次吃大餐",
"spider": "qczj_index",
"content": {},
"url": "http://xxx",
"brand": "宝马",
"series": "宝马1系",
"model": "2017款"
}

第四步:聚合分析

POST http://192.168.80.133:9200/my_index_name/my_type_name/_search
{
"size": 0,
"query" : {
"range" : {
"time": {
"gte": 1513778040000,
"lte": 1513848720000
}
}
},
"aggs": {
"keywords": {
"terms": {"field": "keywords"},
"aggs": {
"emotions": {
"terms": {"field": "emotion"}
}
}
}
}
}

输出结果:

{
"took": 22,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 32,
"max_score": 0.0,
"hits": []
},
"aggregations": {
"keywords": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "动力", # 完整的词没有被拆分为独立的汉字
"doc_count": 2,
"emotions": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": -1,
"doc_count": 1
},
{
"key": 0,
"doc_count": 1
}
]
}
}
]
}
}
}

【参考】

http://www.cnblogs.com/xing901022/p/5910139.html 如何在Elasticsearch中安装中文分词器(IK+pinyin)

https://elasticsearch.cn/question/47 关于聚合(aggs)的问题

https://github.com/medcl/elasticsearch-analysis-ik/issues/276 create map时出现No handler for type [text] declared on field [content] #276

http://blog.csdn.net/guo_jia_liang/article/details/52980716 Elasticsearch2.4学习(三)------Elasticsearch2.4插件安装详解

Elasticsearch入门之从零开始安装ik分词器的更多相关文章

  1. Linux下,非Docker启动Elasticsearch 6.3.0,安装ik分词器插件,以及使用Kibana测试Elasticsearch,

    Linux下,非Docker启动Elasticsearch 6.3.0 查看java版本,需要1.8版本 java -version yum -y install java 创建用户,因为elasti ...

  2. Linux使用Docker启动Elasticsearch并配合Kibana使用,安装ik分词器

    注意事项 这里我的Linux虚拟机的IP地址是192.168.1.3 Docker运行Elasticsearch容器之后不会立即有反应,要等一会,等待容器内部启动Elasticsearch,才可以访问 ...

  3. 【ELK】【docker】【elasticsearch】1. 使用Docker和Elasticsearch+ kibana 5.6.9 搭建全文本搜索引擎应用 集群,安装ik分词器

    系列文章:[建议从第二章开始] [ELK][docker][elasticsearch]1. 使用Docker和Elasticsearch+ kibana 5.6.9 搭建全文本搜索引擎应用 集群,安 ...

  4. Elasticsearch下安装ik分词器

    安装ik分词器(必须安装maven) 上传相应jar包 解压到相应目录 unzip elasticsearch-analysis-ik-master.zip(zip包) cp -r elasticse ...

  5. ElasticSearch(六):IK分词器的安装与使用IK分词器创建索引

    之前我们创建索引,查询数据,都是使用的默认的分词器,分词效果不太理想,会把text的字段分成一个一个汉字,然后搜索的时候也会把搜索的句子进行分词,所以这里就需要更加智能的分词器IK分词器了. 1. i ...

  6. windows下elasticsearch安装ik分词器后无法启动

    windows下elasticsearch安装ik分词器后启动报如下图错误: 然后百度说是elasticsearch路径有空格,一看果然我的路径有空格,然后重新换个路径就好了.

  7. 如何开发自己的搜索帝国之安装ik分词器

     Elasticsearch默认提供的分词器,会把每个汉字分开,而不是我们想要的根据关键词来分词,我是中国人 不能简单的分成一个个字,我们更希望 “中国人”,“中国”,“我”这样的分词,这样我们就需要 ...

  8. Elastic Stack 笔记(二)Elasticsearch5.6 安装 IK 分词器和 Head 插件

    博客地址:http://www.moonxy.com 一.前言 Elasticsearch 作为开源搜索引擎服务器,其核心功能在于索引和搜索数据.索引是把文档写入 Elasticsearch 的过程, ...

  9. ElasticSearch5.3安装IK分词器并验证

    ElasticSearch5.3安装IK分词器 之前使用Elasticsearch安装head插件成功了,但是安装IK分词器却失败了.貌似是ElasticSearch5.0以后就不支持直接在elast ...

随机推荐

  1. Summary (7Road)

    今天有幸参加了一次考试,碰到的一些考题如下: 很多东西,明明都是会的,可是就是写不出来.可见自己的Linux基础还是很薄弱,懒得去记这些有用的东西.既然自己不是老板,不是项目经理,就该努力记住这些基础 ...

  2. 【题解】 bzoj2462: [BeiJing2011]矩阵模板

    题面戳我 Solution 二维矩阵\(hash\),判断即可 自己YY了一个方法,\(bzoj\)T到飞,(一开始还用的三\(hash\)),交到luogu貌似跑的不慢啊qwq (我是不会告诉你全输 ...

  3. Building Microservices with Spring Boot and Apache Thrift. Part 1 with servlet

    https://dzone.com/articles/building-microservices-spring In the modern world of microservices it's i ...

  4. 阻止 form 回车 自动提交

    问题:当form表单中只有一个input时,在input中按回车键会自动提交. 解决方案: 1.form元素上加onsubmit="return false"(推荐) 2.多个in ...

  5. 洛谷P4242 树上的毒瘤

    解:首先有个套路是一条边的权值是[两端点颜色不同].这个用树剖直接维护,支持修改. 每次询问建虚树,查询虚树上每条边的权值.然后树形DP,用开店的方法,每个点链加链查. #include <bi ...

  6. Django session相关操作

    Django session 是存储在数据库中的所以要先跟数据库建立连接 本连接有Django跟数据库建立连接的操作:https://www.cnblogs.com/Niuxingyu/p/10296 ...

  7. #ifndef HeaderName_h #define HeaderName_h #endif 使用详解(转)

    原文:#ifndef HeaderName_h #define HeaderName_h #endif 使用详解 想必很多人都看到过头文件中写有:#ifndef HeaderName_h       ...

  8. python赋值和生成器

    在python赋值过程中,对单个变量的赋值,在所有语言中都是通用的,如果是对两个变量同时进行赋值,这个时候,就会出现一点点小的差异.例如在下面的一两行代码中. a , b = b , a+b 这是同时 ...

  9. P3358 最长k可重区间集问题

    题目链接 \(Click\) \(Here\) 这题的写法非常巧妙. 每个位置的点向它的下一个位置连一个容量为\(INF\)的边,从区间的左端点往右端点拉一条容量为\(1\),费用为区间长度的边,从起 ...

  10. Vue(基础七)_webpack打包工具(续)

    ---恢复内容开始--- 一.前言               1.webpack-dev-server               2.es6的解析               3.单文件引入 二. ...