Elasticsearch 中文分词器 IK 配置和使用

来源:互联网 发布:怪物猎人2g多玩数据库 编辑:程序博客网 时间:2024/05/22 13:20

Elasticsearch 内置的分词器对中文不友好,会把中文分成单个字来进行全文检索,不能达到想要的结果
看一个例子

curl -XGET 'http://localhost:9200/_analyze?pretty&analyzer=standard' -d '第二更新'#返回{  "tokens" : [ {    "token" : "第",    "start_offset" : 1,    "end_offset" : 2,    "type" : "<IDEOGRAPHIC>",    "position" : 0  }, {    "token" : "二",    "start_offset" : 2,    "end_offset" : 3,    "type" : "<IDEOGRAPHIC>",    "position" : 1  }, {    "token" : "更",    "start_offset" : 3,    "end_offset" : 4,    "type" : "<IDEOGRAPHIC>",    "position" : 2  }, {    "token" : "新",    "start_offset" : 4,    "end_offset" : 5,    "type" : "<IDEOGRAPHIC>",    "position" : 3  } ]}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

standard 分词器将 “第二更新” 拆分成了四个字,而我们更希望拆分成 “第二”、”更新” 这样有意义词语
IK 分词器插件就是解决这种问题的
使用 IK 测试,将请求链接改为 http://localhost:9200/_analyze?pretty&analyzer=ik

{  "tokens" : [ {    "token" : "第二",    "start_offset" : 1,    "end_offset" : 3,    "type" : "CN_WORD",    "position" : 0  }, {    "token" : "二",    "start_offset" : 2,    "end_offset" : 3,    "type" : "TYPE_CNUM",    "position" : 1  }, {    "token" : "更新",    "start_offset" : 3,    "end_offset" : 5,    "type" : "CN_WORD",    "position" : 2  } ]}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

现在我们就来安装和体验一下它吧

Elasticsearch 版本:2.2.0 CSDN下载
IK 插件版本 :1.8 CSDN下载
服务器系统:CentOS 6.4 (虚拟机)

一、安装

可以到 IK 的 GitHub 上获取对应的版本
Elasticsearch 要使用 ik,就要先构建 ik 的 jar包,这里要用到 maven 包管理工具(CSDN下载)
而 maven 需要Java 环境,既然在使用 Elasticsearch ,那么我就假定你已有 java 环境了
安装 maven

cd /usr/local/src/tar zxvf /usr/local/src/apache-maven-3.3.9-bin.tar.gzcp -r /usr/local/src/apache-maven-3.3.9 /usr/local/maven3.3.9
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

添加 maven 环境变量

vim /etc/profile#在最后面添加MAVEN_HOME=/usr/local/maven3.3.9export MAVEN_HOMEexport PATH=${PATH}:${MAVEN_HOME}/bin# 执行如下代码,使更改生效source /etc/profile
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

保存退出,查看maven 信息

mvn -v# 返回Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)Maven home: /usr/local/maven3.3.9Java version: 1.7.0_79, vendor: Oracle CorporationJava home: /usr/local/java/jreDefault locale: zh_CN, platform encoding: UTF-8OS name: "linux", version: "2.6.32-358.el6.x86_64", arch: "amd64", family: "unix"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

构建 IK 的 jar 包,你也可以下载我构建好的直接使用 CSDN下载

cd /usr/local/src/unzip -x elasticsearch-analysis-ik-1.8.zip cd elasticsearch-analysis-ik-master/
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

执行构建,第一次有点慢(下载依赖包)

mvn package
  • 1
  • 1

完成后,会在当前目录下生成 target 的目录,构建好的安装包在 ./target/releases/elasticsearch-analysis-ik-1.8.0.zip
将安装包解压并移动到 elasticsearch 的插件目录下的ik 文件夹

unzip -d /usr/local/elasticsearch-2.2.0/plugins/ik ./target/releases/elasticsearch-analysis-ik-1.8.0.zip
  • 1
  • 1

重启 elasticsearch,就可以使用 ik 了

二、使用

ik 带有两个分词器
ik_max_word :会将文本做最细粒度的拆分;尽可能多的拆分出词语
ik_smart:会做最粗粒度的拆分;已被分出的词语将不会再次被其它词语占有
例子:

# ik_max_wordcurl -XGET 'http://localhost:9200/_analyze?pretty&analyzer=ik_max_word' -d '联想是全球最大的笔记本厂商'#返回{  "tokens" : [ {    "token" : "联想",    "start_offset" : 0,    "end_offset" : 2,    "type" : "CN_WORD",    "position" : 0  }, {    "token" : "全球",    "start_offset" : 3,    "end_offset" : 5,    "type" : "CN_WORD",    "position" : 1  }, {    "token" : "最大",    "start_offset" : 5,    "end_offset" : 7,    "type" : "CN_WORD",    "position" : 2  }, {    "token" : "笔记本",    "start_offset" : 8,    "end_offset" : 11,    "type" : "CN_WORD",    "position" : 3  }, {    "token" : "笔记",    "start_offset" : 8,    "end_offset" : 10,    "type" : "CN_WORD",    "position" : 4  }, {    "token" : "笔",    "start_offset" : 8,    "end_offset" : 9,    "type" : "CN_WORD",    "position" : 5  }, {    "token" : "记",    "start_offset" : 9,    "end_offset" : 10,    "type" : "CN_CHAR",    "position" : 6  }, {    "token" : "本厂",    "start_offset" : 10,    "end_offset" : 12,    "type" : "CN_WORD",    "position" : 7  }, {    "token" : "厂商",    "start_offset" : 11,    "end_offset" : 13,    "type" : "CN_WORD",    "position" : 8  } ]}# ik_smartcurl -XGET 'http://localhost:9200/_analyze?pretty&analyzer=ik_smart' -d '联想是全球最大的笔记本厂商'# 返回{  "tokens" : [ {    "token" : "联想",    "start_offset" : 0,    "end_offset" : 2,    "type" : "CN_WORD",    "position" : 0  }, {    "token" : "全球",    "start_offset" : 3,    "end_offset" : 5,    "type" : "CN_WORD",    "position" : 1  }, {    "token" : "最大",    "start_offset" : 5,    "end_offset" : 7,    "type" : "CN_WORD",    "position" : 2  }, {    "token" : "笔记本",    "start_offset" : 8,    "end_offset" : 11,    "type" : "CN_WORD",    "position" : 3  }, {    "token" : "厂商",    "start_offset" : 11,    "end_offset" : 13,    "type" : "CN_WORD",    "position" : 4  } ]}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96

看出区别了吧

下面我们来创建一个索引,使用 ik
创建一个名叫 iktest 的索引,设置它的分析器用 ik ,分词器用 ik_max_word,并创建一个 article 的类型,里面有一个 subject 的字段,指定其使用 ik_max_word 分词器

curl -XPUT 'http://localhost:9200/iktest?pretty' -d '{    "settings" : {        "analysis" : {            "analyzer" : {                "ik" : {                    "tokenizer" : "ik_max_word"                }            }        }    },    "mappings" : {        "article" : {            "dynamic" : true,            "properties" : {                "subject" : {                    "type" : "string",                    "analyzer" : "ik_max_word"                }            }        }    }}'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

批量添加几条数据,这里我指定元数据 _id 方便查看,subject 内容为我随便找的几条新闻的标题

curl -XPOST http://localhost:9200/iktest/article/_bulk?pretty -d '{ "index" : { "_id" : "1" } }{"subject" : ""闺蜜"崔顺实被韩检方传唤 韩总统府促彻查真相" }{ "index" : { "_id" : "2" } }{"subject" : "韩举行"护国训练" 青瓦台:决不许国家安全出问题" }{ "index" : { "_id" : "3" } }{"subject" : "媒体称FBI已经取得搜查令 检视希拉里电邮" }{ "index" : { "_id" : "4" } }{"subject" : "村上春树获安徒生奖 演讲中谈及欧洲排外问题" }{ "index" : { "_id" : "5" } }{"subject" : "希拉里团队炮轰FBI 参院民主党领袖批其“违法”" }'

查询 “希拉里和韩国”

curl -XPOST http://localhost:9200/iktest/article/_search?pretty  -d'{    "query" : { "match" : { "subject" : "希拉里和韩国" }},    "highlight" : {        "pre_tags" : ["<font color='red'>"],        "post_tags" : ["</font>"],        "fields" : {            "subject" : {}        }    }}'#返回{  "took" : 113,  "timed_out" : false,  "_shards" : {    "total" : 5,    "successful" : 5,    "failed" : 0  },  "hits" : {    "total" : 4,    "max_score" : 0.034062363,    "hits" : [ {      "_index" : "iktest",      "_type" : "article",      "_id" : "2",      "_score" : 0.034062363,      "_source" : {        "subject" : "韩举行"护国训练" 青瓦台:决不许国家安全出问题"      },      "highlight" : {        "subject" : [ "<font color=red>韩</font>举行"护<font color=red>国</font>训练" 青瓦台:决不许国家安全出问题" ]      }    }, {      "_index" : "iktest",      "_type" : "article",      "_id" : "3",      "_score" : 0.0076681254,      "_source" : {        "subject" : "媒体称FBI已经取得搜查令 检视希拉里电邮"      },      "highlight" : {        "subject" : [ "媒体称FBI已经取得搜查令 检视<font color=red>希拉里</font>电邮" ]      }    }, {      "_index" : "iktest",      "_type" : "article",      "_id" : "5",      "_score" : 0.006709609,      "_source" : {        "subject" : "希拉里团队炮轰FBI 参院民主党领袖批其“违法”"      },      "highlight" : {        "subject" : [ "<font color=red>希拉里</font>团队炮轰FBI 参院民主党领袖批其“违法”" ]      }    }, {      "_index" : "iktest",      "_type" : "article",      "_id" : "1",      "_score" : 0.0021509775,      "_source" : {        "subject" : ""闺蜜"崔顺实被韩检方传唤 韩总统府促彻查真相"      },      "highlight" : {        "subject" : [ ""闺蜜"崔顺实被<font color=red>韩</font>检方传唤 <font color=red>韩</font>总统府促彻查真相" ]      }    } ]  }}

这里用了高亮属性 highlight,直接显示到 html 中,被匹配到的字或词将以红色突出显示。若要用过滤搜索,直接将 match 改为 term 即可

三、热词更新配置

网络词语日新月异,如何让新出的网络热词(或特定的词语)实时的更新到我们的搜索当中呢
先用 ik 测试一下 :

curl -XGET 'http://localhost:9200/_analyze?pretty&analyzer=ik_max_word' -d '成龙原名陈港生'#返回{  "tokens" : [ {    "token" : "成龙",    "start_offset" : 1,    "end_offset" : 3,    "type" : "CN_WORD",    "position" : 0  }, {    "token" : "原名",    "start_offset" : 3,    "end_offset" : 5,    "type" : "CN_WORD",    "position" : 1  }, {    "token" : "陈",    "start_offset" : 5,    "end_offset" : 6,    "type" : "CN_CHAR",    "position" : 2  }, {    "token" : "港",    "start_offset" : 6,    "end_offset" : 7,    "type" : "CN_WORD",    "position" : 3  }, {    "token" : "生",    "start_offset" : 7,    "end_offset" : 8,    "type" : "CN_CHAR",    "position" : 4  } ]}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

ik 的主词典中没有”陈港生” 这个词,所以被拆分了。
现在我们来配置一下
修改 IK 的配置文件 :ES 目录/plugins/ik/config/ik/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">http://192.168.1.136/hotWords.php</entry>    <!--用户可以在这里配置远程扩展停止词字典-->    <!-- <entry key="remote_ext_stopwords">words_location</entry> --></properties>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

这里我是用的是远程扩展字典,因为可以使用其他程序调用更新,且不用重启 ES,很方便;当然使用自定义的 mydict.dic 字典也是很方便的,一行一个词,自己加就可以了
既然是远程词典,那么就要是一个可访问的链接,可以是一个页面,也可以是一个txt的文档,但要保证输出的内容是 utf-8 的格式
hotWords.PHP 的内容

$s = <<<'EOF'陈港生元楼蓝瘦EOF;header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT', true, 200);header('ETag: "5816f349-19"');echo $s;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

ik 接收两个返回的头部属性 Last-Modified 和 ETag,只要其中一个有变化,就会触发更新,ik 会每分钟获取一次
重启 Elasticsearch ,查看启动记录,看到了三个词已被加载进来

[2016-10-31 15:08:57,749][INFO ][ik-analyzer              ] 陈港生[2016-10-31 15:08:57,749][INFO ][ik-analyzer              ] 元楼[2016-10-31 15:08:57,749][INFO ][ik-analyzer              ] 蓝瘦
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

现在我们来测试一下,再次执行上面的请求,返回

...  }, {    "token" : "陈港生",    "start_offset" : 5,    "end_offset" : 8,    "type" : "CN_WORD",    "position" : 2  }, {...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

可以看到 ik 分词器已经匹配到了 “陈港生” 这个词