我在以前的文章中介绍过 Elasticsearch的安装和使用,这里咱们使用Kibina做为工具来操做es,可使用es的_analyze来分析分词器的分词结果。git
ES默认的分词器为英文分词器,对英文句子能作到比较好的分词,咱们看一个例子。当输入如下请求时,对"What's your name"句子进行分词,能看到将几个词都分了出来。github
POST _analyze
{
"tokenizer": "standard",
"text": "What's your name"
} 复制代码
{
"tokens" : [
{
"token" : "What's",
"start_offset" : 0,
"end_offset" : 6,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "your",
"start_offset" : 7,
"end_offset" : 11,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "name",
"start_offset" : 12,
"end_offset" : 16,
"type" : "<ALPHANUM>",
"position" : 2
}
]
}复制代码
当输入中文"你叫什么名字"时,能够看到标准分词器将句子分红了一个一个的字,这显然在咱们实际使用的过程当中是没办法接受的。bash
POST _analyze
{
"tokenizer": "standard",
"text": "你叫什么名字"
}复制代码
{
"tokens" : [
{
"token" : "你",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0
},
{
"token" : "叫",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
},
{
"token" : "什",
"start_offset" : 2,
"end_offset" : 3,
"type" : "<IDEOGRAPHIC>",
"position" : 2
},
{
"token" : "么",
"start_offset" : 3,
"end_offset" : 4,
"type" : "<IDEOGRAPHIC>",
"position" : 3
},
{
"token" : "名",
"start_offset" : 4,
"end_offset" : 5,
"type" : "<IDEOGRAPHIC>",
"position" : 4
},
{
"token" : "字",
"start_offset" : 5,
"end_offset" : 6,
"type" : "<IDEOGRAPHIC>",
"position" : 5
}
]
}
复制代码
因为英文句子都是使用空{ "tokens" : [ { "token" : "你", "start_offset" : 0, "end_offset" : 1, "type" : "CN_CHAR", "position" : 0 }, { "token" : "叫什么", "start_offset" : 1, "end_offset" : 4, "type" : "CN_WORD", "position" : 1 }, { "token" : "名字", "start_offset" : 4, "end_offset" : 6, "type" : "CN_WORD", "position" : 2 } ] } 格进行分隔,所以在分词比较明确,可是中文因为语言特性,分词比较难分,也容易产生分词歧义,若是本身开发分词器,成本会比较大,因此通常在使用过程当中都会用一些分词器,比较著名的有Jieba分词器,hanlp等,咱们这里介绍一个es的插件分词器,ik分词器。能够从github下载分词器的压缩包,下载地址: github.com/medcl/elast… ,在es的plugins目录下建立一个ik的目录,把解压后的文件放到ik目录下,而后重启Elasticsearch。app
这时,咱们把以前的分词器换成ik_smart,再来看效果。能够看到用ik_smart已经可以将中文进行分词。elasticsearch
POST _analyze
{
"tokenizer": "ik_smart",
"text": "你叫什么名字"
}复制代码
{
"tokens" : [
{
"token" : "你",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_CHAR",
"position" : 0
},
{
"token" : "叫什么",
"start_offset" : 1,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "名字",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 2
}
]
}
复制代码
除了ik_smart以外,还有一个ik_max_wrod分词器。工具
{
"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
}
]
}
复制代码
这两种分词器在应对具体的场景时,须要选择合适的分词器进行使用。post
通常状况下,为了提升搜索的效果,须要这两种分词器配合使用。既索引时用ik_max_word尽量多的分词,而搜索时用ik_smart尽量提升匹配准度,让用户的搜索尽量的准确。好比一个常见的场景,就是搜索"进口红酒"的时候,尽量的不要出现口红相关商品或者让口红不要排在前面。优化
咱们首先在Elasticsearch内建立一个叫goods的索引,其中名字的分词器用的是ik_max_word。spa
PUT /goods
{
"mappings":{
"goods": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"analyzer": "ik_max_word",
"type": "text"
}
}
}
},
"settings":{
"index": {
"refresh_interval": "1s",
"number_of_shards": 5,
"max_result_window": "10000000",
"mapper": {
"dynamic": "false"
},
"number_of_replicas": 0
}
}
}
复制代码
而后咱们经过POST请求,往里面添加一些数据。插件
POST /goods/goods
{
"id":"1",
"name":"美丽粉色口红明星"
}
POST /goods/goods
{
"id":"2",
"name":"好喝的进口红酒"
}
POST /goods/goods
{
"id":"3",
"name":"进口红酒真好喝"
}
复制代码
最后,在查询的时候,咱们指定查询分词器为ik_smart。
GET /goods/goods/_search
{
"query":{
"match": {
"name": {
"query": "进口红酒",
"analyzer": "ik_smart"
}
}
}
}复制代码
能够看到两条进口红酒相关的记录被搜了出来,可是口红没有被搜出来
{
"took" : 28,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.36464313,
"hits" : [
{
"_index" : "goods",
"_type" : "goods",
"_id" : "cdLk1WoBvRMfJWIKVfOP",
"_score" : 0.36464313,
"_source" : {
"id" : "3",
"name" : "进口红酒真好喝"
}
},
{
"_index" : "goods",
"_type" : "goods",
"_id" : "ctLk1WoBvRMfJWIKX_O6",
"_score" : 0.36464313,
"_source" : {
"id" : "2",
"name" : "好喝的进口红酒"
}
}
]
}
}
复制代码
分词器是Elasticsearch中很重要的一部分,网上也有不少开源的分词器,对于通常的应用这些开源分词器也许足够用了,可是在某些特定的场景下,可能须要对分词器作一些优化,甚至须要自研一些分词器。