Elasticsearch入门之从零开始安装ik分词器

起因

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

1POST http://192.168.80.133:9200/my_index_name/my_type_name/_search 2{ 3 "size": 0, 4 "query" : { 5 "range" : { 6 "time": { 7 "gte": 1513778040000, 8 "lte": 1513848720000 9 } 10 } 11 }, 12 "aggs": { 13 "keywords": { 14 "terms": {"field": "keywords"}, 15 "aggs": { 16 "emotions": { 17 "terms": {"field": "emotion"} 18 } 19 } 20 } 21 } 22}

输出结果:

1{ 2 "took": 22, 3 "timed_out": false, 4 "_shards": { 5 "total": 5, 6 "successful": 5, 7 "failed": 0 8 }, 9 "hits": { 10 "total": 32, 11 "max_score": 0.0, 12 "hits": [] 13 }, 14 "aggregations": { 15 "keywords": { 16 "doc_count_error_upper_bound": 0, 17 "sum_other_doc_count": 0, 18 "buckets": [ 19 { 20 "key": "力", # 完整的词被拆分为独立的汉字 21 "doc_count": 2, 22 "emotions": { 23 "doc_count_error_upper_bound": 0, 24 "sum_other_doc_count": 0, 25 "buckets": [ 26 { 27 "key": -1, 28 "doc_count": 1 29 }, 30 { 31 "key": 0, 32 "doc_count": 1 33 } 34 ] 35 } 36 }, 37 { 38 "key": "动", 39 "doc_count": 2, 40 "emotions": { 41 "doc_count_error_upper_bound": 0, 42 "sum_other_doc_count": 0, 43 "buckets": [ 44 { 45 "key": -1, 46 "doc_count": 1 47 }, 48 { 49 "key": 0, 50 "doc_count": 1 51 } 52 ] 53 } 54 } 55 ] 56 } 57 } 58}

既然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

1wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v1.10.1/elasticsearch-analysis-ik-1.10.1.zip 2unzip elasticsearch-analysis-ik-1.10.1.zip 3cd elasticsearch-analysis-ik-1.10.1 4mvn 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目录下,执行解压。

1unzip elasticsearch-analysis-ik-1.10.1.zip 2rm -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格式如下:

1{ 2 "nagtive_kw": [] 3 "is_all": false, 4 "emotion": 0, 5 "focuce": false, 6 "keywords": ["动力","外观","油耗"], // 在keywords字段上进行聚合分析 7 "source": "汽车之家", 8 "time": -1, 9 "machine_emotion": 0, 10 "title": "no title", 11 "spider": "qczj_index", 12 "content": {}, 13 "url": "http://xxx", 14 "brand": "宝马", 15 "series": "宝马1系", 16 "model": "2017款" 17}

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

1POST http://192.168.80.133:9200/my_index_name/my_type_name/_mapping 2{ 3 "properties": { 4 "keywords": { # 设置keywords字段使用ik分词器 5 "type": "string", 6 "store": "no", 7 "analyzer": "ik_smart", 8 "search_analyzer": "ik_smart", 9 "boost": 8 10 } 11 } 12}

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

1POST http://192.168.80.133:9200/my_index_name/my_type_name/_mapping 2{ 3 "properties": { 4 "keywords": { 5 "type": "text", # text类型在2.4.1版本中不支持 6 "store": "no", 7 "analyzer": "ik_smart", 8 "search_analyzer": "ik_smart", 9 "boost": 8 10 } 11 } 12}

报错:

1{ 2 "error": { 3 "root_cause": [ 4 { 5 "type": "mapper_parsing_exception", 6 "reason": "No handler for type [text] declared on field [keywords]" 7 } 8 ], 9 "type": "mapper_parsing_exception", 10 "reason": "No handler for type [text] declared on field [keywords]" 11 }, 12 "status": 400 13}

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

第三步:添加doc对象

1POST http://192.168.80.133:9200/my_index_name/my_type_name/ 2{ 3 "nagtive_kw": ["动力","外观","油耗"] 4 "is_all": false, 5 "emotion": 0, 6 "focuce": false, 7 "keywords": ["动力","外观","油耗"], // 在keywords字段上进行聚合分析 8 "source": "汽车之家", 9 "time": -1, 10 "machine_emotion": 0, 11 "title": "从动次打次吃大餐", 12 "spider": "qczj_index", 13 "content": {}, 14 "url": "http://xxx", 15 "brand": "宝马", 16 "series": "宝马1系", 17 "model": "2017款" 18}

第四步:聚合分析

1POST http://192.168.80.133:9200/my_index_name/my_type_name/_search 2{ 3 "size": 0, 4 "query" : { 5 "range" : { 6 "time": { 7 "gte": 1513778040000, 8 "lte": 1513848720000 9 } 10 } 11 }, 12 "aggs": { 13 "keywords": { 14 "terms": {"field": "keywords"}, 15 "aggs": { 16 "emotions": { 17 "terms": {"field": "emotion"} 18 } 19 } 20 } 21 } 22}

输出结果:

1{ 2 "took": 22, 3 "timed_out": false, 4 "_shards": { 5 "total": 5, 6 "successful": 5, 7 "failed": 0 8 }, 9 "hits": { 10 "total": 32, 11 "max_score": 0.0, 12 "hits": [] 13 }, 14 "aggregations": { 15 "keywords": { 16 "doc_count_error_upper_bound": 0, 17 "sum_other_doc_count": 0, 18 "buckets": [ 19 { 20 "key": "动力", # 完整的词没有被拆分为独立的汉字 21 "doc_count": 2, 22 "emotions": { 23 "doc_count_error_upper_bound": 0, 24 "sum_other_doc_count": 0, 25 "buckets": [ 26 { 27 "key": -1, 28 "doc_count": 1 29 }, 30 { 31 "key": 0, 32 "doc_count": 1 33 } 34 ] 35 } 36 } 37 ] 38 } 39 } 40}

【参考】 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插件安装详解

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )