es 5.0 拼音分词器 mac

本文介绍如何安装配置Elasticsearch的IK+pinyin分词器,并通过具体实例演示如何使用该分词器进行中文及拼音搜索。文章首先提供了分词器的下载链接与安装步骤,接着详细说明了如何配置索引分析器,最后通过几个测试案例展示了分词器的功能。

 

安装方法和ik中文分词器一样,

先下载:

https://github.com/medcl/elasticsearch-analysis-pinyin

 

执行:

mvn package;

打包成功以后,会生成一个target文件夹,在elasticsearch-analysis-ik-master/target/releases目录下,找到elasticsearch-analysis-ik-5.1.1.zip,这就是我们需要的安装文件。解压elasticsearch-analysis-ik-5.1.1.zip,得到下面内容:

 

如果mvn 有问题的话,可以将其导入eclipse中,进行maven clean ,maven install 

知道这个文件夹,将其拷贝出来,并解压,也可以.

将其放到es安装目录下,文件路径为:

 


 

 

 重启es;

 

测试:

 

 中文与拼音结合测试:

 

 

 

IK+pinyin分词配置

5.1创建索引与分析器设置

创建一个索引,并设置index分析器相关属性:

文档1:
curl -XPUT "http://localhost:9200/medcl/" -d' { "index": { "analysis": { "analyzer": { "ik_pinyin_analyzer": { "type": "custom", "tokenizer": "ik_smart", "filter": ["my_pinyin", "word_delimiter"] } }, "filter": { "my_pinyin": { "type": "pinyin", "first_letter": "prefix", "padding_char": " " } } } } }'
文档2:

curl -XPOST http://localhost:9200/medcl/folks/tina -d'{"name":"中华人民共和国国歌"}'

 

5.3测试(1)拼音分词

下面四条命命令都可以匹配”刘德华”:

1,curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:liu"

2,curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:de"

3,curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:hua"

4,curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:ldh"

5.4测试(2)IK分词测试

curl -XPOST "http://localhost:9200/medcl/_search?pretty" -d'
{
  "query": {
    "match": {
      "name.pinyin": "国歌"
    }
  },
  "highlight": {
    "fields": {
      "name.pinyin": {}
    }
  }
}'
结果如下:
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 16.698704,
    "hits" : [
      {
        "_index" : "medcl",
        "_type" : "folks",
        "_id" : "tina",
        "_score" : 16.698704,
        "_source" : {
          "name" : "中华人民共和国国歌"
        },
        "highlight" : {
          "name.pinyin" : [
            "<em>中华人民共和国</em><em>国歌</em>"
          ]
        }
      }
    ]
  }
}

5.3测试(4)pinyin+ik分词测试:

curl -XPOST "http://localhost:9200/medcl/_search?pretty" -d'
{
  "query": {
    "match": {
      "name.pinyin": "zhonghua"
    }
  },
  "highlight": {
    "fields": {
      "name.pinyin": {}
    }
  }
}'
结果如下


{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 5.9814634,
    "hits" : [
      {
        "_index" : "medcl",
        "_type" : "folks",
        "_id" : "tina",
        "_score" : 5.9814634,
        "_source" : {
          "name" : "中华人民共和国国歌"
        },
        "highlight" : {
          "name.pinyin" : [
            "<em>中华人民共和国</em>国歌"
          ]
        }
      },
      {
        "_index" : "medcl",
        "_type" : "folks",
        "_id" : "andy",
        "_score" : 2.2534127,
        "_source" : {
          "name" : "刘德华"
        },
        "highlight" : {
          "name.pinyin" : [
            "<em>刘德华</em>"
          ]
        }
      }
    ]
  }
}

 

 

 

 

 

 

参考文献:

https://github.com/medcl/elasticsearch-analysis-pinyin

http://blog.youkuaiyun.com/napoay/article/details/53907921

 

转载于:https://www.cnblogs.com/wangchuanfu/p/7239269.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值