深入浅出Elasticsearch

Elasticsearch拼音分词器安装和使用

2019-10-10  本文已影响0人  zhenxianyimeng

1.概述

分词是Elasticsearch使用过程中一个很重要的功能,得益于Elasticsearch的插件机制,网上有很多开源的Elasticsearch分词器,可以基于Elasticsearch插件的形式进行安装。

本文基于Elasticsearch6.5.4版本进行拼音分词器的安装和使用,Elasticsearch的安装和ik分词器的使用可以参考:
Elasticsearch 安装和使用
Elasticsearch中ik分词器的使用

2.拼音分词器安装

拼音分词器的下载地址为:https://github.com/medcl/elasticsearch-analysis-pinyin
下载对应的代码到本地,注意分支只有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文件夹内。

解压成功后,重启ES即可,这时发现重启失败。原来v6.5.4tag下载下来的pom.xml文件Elasticsearch版本还是6.3.0,替换成6.5.4之后,重启成功

<elasticsearch.version>6.3.0</elasticsearch.version>

3.拼音分词器使用

重启ES成功以后,我们可以用Kibina来使用一下拼音分词器。首先我们使用ES的analyze功能,看一下拼音分词的效果

GET _analyze
{
  "analyzer": "pinyin", 
  "text": ["我们是一家人"]
}

分析的结果为:

{
  "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" : "啤酒"
        }
      }
    ]
  }
}

4.总结

Elasticsearch的插件功能异常的强大,有很多值得分析和研究的地方。而分词对于搜索来说也是非常重要的功能。在一些特殊的场景下,我们可能需要利用ES的插件框架,进行自定义的分词器开发,所以掌握插件的使用和分词器的使用是基础中的基础。

上一篇 下一篇

猜你喜欢

热点阅读