若是直接使用Elasticsearch的朋友在处理中文内容的搜索时,确定会遇到很尴尬的问题——中文词语被分红了一个一个的汉字,当用Kibana做图的时候,按照term来分组,结果一个汉字被分红了一组。html
这是由于使用了Elasticsearch中默认的标准分词器,这个分词器在处理中文的时候会把中文单词切分红一个一个的汉字,所以引入中文的分词器就能解决这个问题。git
本篇文章按照下面的内容进行描述:github
分词顾名思义,就是把一句话分红一个一个的词。这个概念在搜索中很重要,好比 This is a banana.
若是按照普通的空格来分词,分红this
,is
,a
,banana
,的出来的a
其实对咱们并无什么用处。所以须要注意下面的问题:算法
a
,or
,and
这种都属于停顿词)Banana
与banana
)具体的算法能够参考http://tartarus.org/~martin/PorterStemmer/,对照的词语能够参考这里http://snowball.tartarus.org/algorithms/porter/diffs.txtapi
相比中文,就复杂的度了。由于中文不能单纯的依靠空格,标点这种进行分词。就好比中华人民共和国国民
,不能简单的分红一个词,也不能粗暴的分红中华人民共和国
和国民
,人民
、中华
这些也都算一个词!app
所以常见的分词算法就是拿一个标准的词典,关键词都在这个词典里面。而后按照几种规则去查找有没有关键词,好比:curl
IK,elasticsearch-analysis-ik提供了两种方式,ik_smart
就是最少切分,ik_max_word
则为细粒度的切分(多是双向,没看过源码)jvm
了解了分词器的背景后,就能够看一下如何在Elasticsearch重安装分词器了。elasticsearch
在github中下载相应的代码,好比个人最新版本2.4.0就没有对应的ik版本,不用担忧,只须要修改pom.xml就能够了:maven
<properties> <!-- 这里的版本号,修改为你对应的版本就好了。 不过最好不要跨度太大,相近的版本可能没有问题,可是跨度太大的版本,这样作就不保证好使了--> <elasticsearch.version>2.4.0</elasticsearch.version> <maven.compiler.target>1.7</maven.compiler.target> <elasticsearch.assembly.descriptor>${project.basedir}/src/main/assemblies/plugin.xml</elasticsearch.assembly.descriptor> <elasticsearch.plugin.name>analysis-ik</elasticsearch.plugin.name> <elasticsearch.plugin.classname>org.elasticsearch.plugin.analysis.ik.AnalysisIkPlugin</elasticsearch.plugin.classname> <elasticsearch.plugin.jvm>true</elasticsearch.plugin.jvm> <tests.rest.load_packaged>false</tests.rest.load_packaged> <skip.unit.tests>true</skip.unit.tests> <gpg.keyname>4E899B30</gpg.keyname> <gpg.useagent>true</gpg.useagent> </properties>
下载后,执行mvn package
,进行打包:
├─config ├─src └─target ├─archive-tmp ├─classes ├─generated-sources ├─maven-archiver ├─maven-status ├─releases │ └─elasticsearch-analysis-ik-1.9.5.zip └─surefire
编译完成后,能够在target/releases目录下找到对应的zip包。
解压zip包,复制到elasticsearch-root-path/plugins/ik下便可。
[root@hadoop-master ik]# ll total 1428 -rw-r--r-- 1 root root 263965 Sep 26 15:03 commons-codec-1.9.jar -rw-r--r-- 1 root root 61829 Sep 26 15:03 commons-logging-1.2.jar drwxr-xr-x 3 root root 4096 Sep 26 16:11 config -rw-r--r-- 1 root root 56023 Sep 26 15:03 elasticsearch-analysis-ik-1.9.5.jar -rw-r--r-- 1 root root 736658 Sep 26 15:03 httpclient-4.5.2.jar -rw-r--r-- 1 root root 326724 Sep 26 15:03 httpcore-4.4.4.jar -rw-r--r-- 1 root root 2666 Sep 26 15:03 plugin-descriptor.properties [root@hadoop-master ik]# pwd /usr/elk/elasticsearch-2.4.0/plugins/ik
拷贝后,重启elasticsearch就能够使用分词器了。
这里使用_analyze api对中文段落进行分词,测试一下:
GET _analyze { "analyzer":"ik_max_word", "text":"中华人民共和国国歌" }
能够看到ik尽量多的切分的单词:
{ "tokens": [ { "token": "中华人民共和国", "start_offset": 0, "end_offset": 7, "type": "CN_WORD", "position": 0 }, { "token": "中华人民", "start_offset": 0, "end_offset": 4, "type": "CN_WORD", "position": 1 }, { "token": "中华", "start_offset": 0, "end_offset": 2, "type": "CN_WORD", "position": 2 }, { "token": "华人", "start_offset": 1, "end_offset": 3, "type": "CN_WORD", "position": 3 }, { "token": "人民共和国", "start_offset": 2, "end_offset": 7, "type": "CN_WORD", "position": 4 }, { "token": "人民", "start_offset": 2, "end_offset": 4, "type": "CN_WORD", "position": 5 }, { "token": "共和国", "start_offset": 4, "end_offset": 7, "type": "CN_WORD", "position": 6 }, { "token": "共和", "start_offset": 4, "end_offset": 6, "type": "CN_WORD", "position": 7 }, { "token": "国", "start_offset": 6, "end_offset": 7, "type": "CN_CHAR", "position": 8 }, { "token": "国歌", "start_offset": 7, "end_offset": 9, "type": "CN_WORD", "position": 9 } ] }
若是使用ik_smart,则会尽量少的返回词语:
{ "tokens": [ { "token": "中华人民共和国", "start_offset": 0, "end_offset": 7, "type": "CN_WORD", "position": 0 }, { "token": "国歌", "start_offset": 7, "end_offset": 9, "type": "CN_WORD", "position": 1 } ] }
我这里直接在elastic Sense中进行测试的(强烈推荐这个插件,很是好用,不过输入中文的时候,有点BUG)
PUT test { }
若是你用的是curl,能够执行curl -XPUT localhost:9200/test
POST test/test/_mapping { "test": { "_all": { "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "term_vector": "no", "store": "false" }, "properties": { "content": { "type": "string", "store": "no", "term_vector": "with_positions_offsets", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "include_in_all": "true", "boost": 8 } } } }
上面的命令,是定义test索引下test类型的映射。其中定义了_all字段的分析方法,以及content属性的分析方法。
这里介绍下什么是_all字段,其实_all字段是为了在不知道搜索哪一个字段时,使用的。es会把全部的字段(除非你手动设置成false),都放在_all中,而后经过分词器去解析。当你使用query_string的时候,默认就在这个_all字段上去作查询,而不须要挨个字段遍历,节省了时间。
properties
中定义了特定字段的分析方式。在上面的例子中,仅仅设置了content的分析方法。
POST test/test/1 { "test":"美国留给伊拉克的是个烂摊子吗" } POST test/test/2 { "content":"公安部:各地校车将享最高路权吗" } POST test/test/3 { "content":"中韩渔警冲突调查:韩警平均天天扣1艘中国渔船" } POST test/test/4 { "content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首" }
GET test/_search { "query" : { "term" : { "content" : "中国" }}, "highlight" : { "pre_tags" : ["<tag1>", "<tag2>"], "post_tags" : ["</tag1>", "</tag2>"], "fields" : { "content" : {} } } }
获得返回结果:
{ "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 1.5, "hits": [ { "_index": "test", "_type": "test", "_id": "4", "_score": 1.5, "_source": { "content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首" }, "highlight": { "content": [ "<tag1>中国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首" ] } }, { "_index": "test", "_type": "test", "_id": "3", "_score": 0.53699243, "_source": { "content": "中韩渔警冲突调查:韩警平均天天扣1艘中国渔船" }, "highlight": { "content": [ "中韩渔警冲突调查:韩警平均天天扣1艘<tag1>中国</tag1>渔船" ] } } ] } }
pinyin分词器可让用户输入拼音,就能查找到相关的关键词。好比在某个商城搜索中,输入shuihu
,就能匹配到水壶
。这样的体验仍是很是好的。
pinyin分词器的安装与IK是同样的,这里就省略掉了。下载的地址参考github.
这个分词器在1.8版本中,提供了两种分词规则:
pinyin
,就是普通的把汉字转换成拼音;pinyin_first_letter
,提取汉字的拼音首字母首先建立索引,并建立分词器:
PUT medcl { "index" : { "analysis" : { "analyzer" : { "pinyin_analyzer" : { "tokenizer" : "my_pinyin", "filter" : "word_delimiter" } }, "tokenizer" : { "my_pinyin" : { "type" : "pinyin", "first_letter" : "none", "padding_char" : " " } } } } }
而后使用analyze api,进行测试
GET medcl/_analyze { "text":"刘德华", "analyzer":"pinyin_analyzer" }
能够获得结果:
{ "tokens": [ { "token": "liu", "start_offset": 0, "end_offset": 3, "type": "word", "position": 0 }, { "token": "de", "start_offset": 0, "end_offset": 3, "type": "word", "position": 1 }, { "token": "hua", "start_offset": 0, "end_offset": 3, "type": "word", "position": 2 } ] }
若是分词器设置为pinyin_first_letter,则分析的结果为:
{ "tokens": [ { "token": "ldh", "start_offset": 0, "end_offset": 3, "type": "word", "position": 0 } ] }
POST medcl/_close { }
PUT medcl/_settings { "index" : { "analysis" : { "analyzer" : { "pinyin_analyzer" : { "tokenizer" : "my_pinyin", "filter" : ["word_delimiter","nGram"] } }, "tokenizer" : { "my_pinyin" : { "type" : "pinyin", "first_letter" : "prefix", "padding_char" : " " } } } } }
POST medcl/_open { }
POST medcl/folks/_mapping { "folks": { "properties": { "name": { "type": "multi_field", "fields": { "name": { "type": "string", "store": "no", "term_vector": "with_positions_offsets", "analyzer": "pinyin_analyzer", "boost": 10 }, "primitive": { "type": "string", "store": "yes", "analyzer": "keyword" } } } } } }
POST medcl/folks/1 { "name":"刘德华" }
GET medcl/folks/_search { "query": {"match": { "name": "l d hua" }} }
这里搜liu de hua
,ldh
,l de hua
都能匹配到,仍是很强大滴。
{ "took": 7, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 7.408082, "hits": [ { "_index": "medcl", "_type": "folks", "_id": "1", "_score": 7.408082, "_source": { "name": "刘德华" } } ] } }