ElasticSearch安装拼音插件(pinyin)

环境介绍

集群环境如下:

  • Ubuntu14.04
  • ElasticSearch 2.3.1(3节点)
  • JDK1.8.0_60

开发环境:

  • Windows10
  • JDK 1.8.0_66
  • Maven 3.3.3
  • Intellij IDEA 2016.1

下载编译Pinyin

  • clone elasticsearch-analysis-pinyin
    通过IntelliJ从git上克隆elasticsearch-analysis-pinyin工程
  • 修改ES版本
    下载完项目后修改项目根目录下pom.xml文件中的properties/elasticsearch.version节点值为2.3.1,以确保编译后的版本兼容ES2.3.1版本;
  • 编译
    打开IntelliJ Terminal工具,输入以下命令:
    mvn clean install -Dmaven.test.skip
    可以在项目目录elasticsearch-analysis-pinyin arget eleases看到编译后的结果elasticsearch-analysis-pinyin-1.7.4.zip,以及elasticsearch-analysis-pinyin arget目录下的elasticsearch-analysis-pinyin-1.7.4.jar。
    这里我们主要使用zip包。

安装部署

  • 安装
    在ES服务器每个节点的${ES_HOME}/plugins目录下新建文件夹,名为pinyin;
    解压上述zip压缩包,可见三个文件elasticsearch-analysis-pinyin-1.7.4.jar、plugin-descriptor.properties、pinyin4j-2.5.0.jar,将其上传到ES服务器pinyin文件夹内即可;
  • 重启
    节点安装完pinyin插件后,需要重启生效。
  • 多节点集群
    ES集群每个节点都进行上述安装。

测试

分词测试

  • 建立测试索引
    建立一个测试分词效果的索引medcl,在节点终端执行如下代码:
  1. curl -XPUT http://localhost:9200/medcl/-d'
  2. {
  3. "index" : {
  4. "analysis" : {
  5. "analyzer" : {
  6. "pinyin_analyzer" : {
  7. "tokenizer" : "my_pinyin",
  8. "filter" : ["standard"]
  9. }
  10. },
  11. "tokenizer" : {
  12. "my_pinyin" : {
  13. "type" : "pinyin",
  14. "first_letter" : "none",
  15. "padding_char" : " "
  16. }
  17. }
  18. }
  19. }
  20. }'
  • 通过浏览器测试分词
  1. http://10.110.13.144:9200/medcl/_analyze?text=%E5%88%98%E5%BE%B7%E5%8D%8E&analyzer=pinyin_analyzer

若测试成功,浏览器返回结果如下:

  1. {"tokens":[{"token":"liudehua","start_offset":0,"end_offset":3,"type":"word","position":0}]}

建立拼音索引

  • 建立索引并设置分词
  1. curl -XPOST http://localhost:9200/medcl/_close
  2. curl -XPUT http://localhost:9200/medcl/_settings -d'
  3. {
  4. "index" : {
  5. "analysis" : {
  6. "analyzer" : {
  7. "pinyin_analyzer" : {
  8. "tokenizer" : "my_pinyin",
  9. "filter" : ["standard"],
  10. "type":"pinyin"
  11. }
  12. },
  13. "tokenizer" : {
  14. "my_pinyin" : {
  15. "type" : "pinyin",
  16. "first_letter" : "none",
  17. "padding_char" : " "
  18. }
  19. }
  20. }
  21. }
  22. }'
  23. curl -XPOST http://localhost:9200/medcl/_open
  • 建立mapping
  1. curl -XPOST http://localhost:9200/medcl/folks/_mapping -d'
  2. {
  3. "folks": {
  4. "properties": {
  5. "name": {
  6. "type": "multi_field",
  7. "fields": {
  8. "name": {
  9. "type": "string",
  10. "store": "no",
  11. "term_vector": "with_positions_offsets",
  12. "analyzer": "pinyin_analyzer",
  13. "boost": 10
  14. },
  15. "primitive": {
  16. "type": "string",
  17. "store": "yes",
  18. "analyzer": "keyword"
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }'
  • 上传数据
  1. curl -XPOST http://localhost:9200/medcl/folks/andy -d'{"name":"刘德华"}'
  • 在浏览器请求检索
  1. http://10.110.13.144:9200/medcl/folks/_search?q=name:liudehua

若检索成功,浏览器返回以下结果:

  1. {"took":9,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":3.0685282,"hits":[{"_index":"pinyin","_type":"test","_id":"andy","_score":3.0685282,"_source":{"name":"刘德华"}}]}}

参考资料





附件列表

原文地址:https://www.cnblogs.com/myitroad/p/5901629.html