分詞是Elasticsearch使用過程當中一個很重要的功能,得益於Elasticsearch的插件機制,網上有不少開源的Elasticsearch分詞器,能夠基於Elasticsearch插件的形式進行安裝。git
本文基於Elasticsearch6.5.4版本進行拼音分詞器的安裝和使用,Elasticsearch的安裝和ik分詞器的使用能夠參考:github
拼音分詞器的下載地址爲:github.com/medcl/elast…app
下載對應的代碼到本地,注意分支只有6.x,咱們Elasticsearch的版本爲6.5.4,能夠在tag裏找到對應的版本v6.5.4(注:通常github代碼並不會每一個版本對應一個分支,可是版本會打上對應的tag,從tag下載就能夠)。框架
下載完成後,用maven命令mvn package
打包,在/target/releases 目錄下會生成一個elasticsearch-analysis-pinyin-6.5.4.zip文件。在Elasticsearch的plugins目錄下,新建一個pinyin文件夾,將zip內的文件解壓到pinyin文件夾內。elasticsearch
解壓成功後,重啓ES便可,這時發現重啓失敗。原來v6.5.4tag下載下來的pom.xml文件Elasticsearch版本仍是6.3.0,替換成6.5.4以後,重啓成功maven
<elasticsearch.version>6.3.0</elasticsearch.version>
複製代碼
重啓ES成功之後,咱們能夠用Kibina來使用一下拼音分詞器。首先咱們使用ES的analyze功能,看一下拼音分詞的效果post
GET _analyze
{
"analyzer": "pinyin",
"text": ["咱們是一家人"]
}
複製代碼
分析的結果爲:spa
{
"tokens" : [
{
"token" : "wo",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 0
},
{
"token" : "men",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 1
},
{
"token" : "shi",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 2
},
{
"token" : "yi",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 3
},
{
"token" : "jia",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 4
},
{
"token" : "ren",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 5
},
{
"token" : "wmsyjr",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 5
}
]
}
複製代碼
而後咱們新建一個索引,有一個字段name,name.ik使用ik分詞器,name.pinyin使用拼音分詞器。
PUT /pinyin_test
{
"mappings":{
"doc": {
"properties": {
"id": {
"type": "integer"
},
"name": {
"fields": {
"ik": {
"type": "text",
"analyzer": "ik_max_word"
},
"pinyin":{
"type": "text",
"analyzer": "pinyin"
}
},
"type": "text"
}
}
}
},
"settings":{
"index": {
"refresh_interval": "1s",
"number_of_shards": 3,
"max_result_window": "10000000",
"number_of_replicas": 0
}
}
}
複製代碼
新建索引以後,咱們利用_bulk功能,批量插入一些數據
POST _bulk
{ "index" : { "_index" : "pinyin_test", "_type" : "doc" } }
{ "name": "啤酒"}
{ "index" : { "_index" : "pinyin_test", "_type" : "doc" } }
{ "name": "壁虎"}
{ "index" : { "_index" : "pinyin_test", "_type" : "doc" } }
{ "name": "閉戶"}
{ "index" : { "_index" : "pinyin_test", "_type" : "doc" } }
{ "name": "幣戶"}
{ "index" : { "_index" : "pinyin_test", "_type" : "doc" } }
{ "name": "啤酒杯"}
{ "index" : { "_index" : "pinyin_test", "_type" : "doc" } }
{ "name": "喝花酒"}
{ "index" : { "_index" : "pinyin_test", "_type" : "doc" } }
複製代碼
對索引進行搜索的時候,用ik或者默認分詞器搜索pj都沒有結果。
GET pinyin_test/_search
{
"query": {"match": {
"name.ik": "pj"
}}
}
複製代碼
只有使用pinyin分詞的器,搜索pj的時候能搜到啤酒
GET pinyin_test/_search
{
"query": {"match": {
"name.pinyin": "pj"
}}
}
複製代碼
結果爲:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.4599355,
"hits" : [
{
"_index" : "pinyin_test",
"_type" : "doc",
"_id" : "w2u-tW0BCKQ-TN47xQMp",
"_score" : 1.4599355,
"_source" : {
"name" : "啤酒"
}
}
]
}
}
複製代碼
Elasticsearch的插件功能異常的強大,有不少值得分析和研究的地方。而分詞對於搜索來講也是很是重要的功能。在一些特殊的場景下,咱們可能須要利用ES的插件框架,進行自定義的分詞器開發,因此掌握插件的使用和分詞器的使用是基礎中的基礎。