環境介紹
集群環境如下:
- 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\target\releases看到編譯后的結果elasticsearch-analysis-pinyin-1.7.4.zip,以及elasticsearch-analysis-pinyin\target目錄下的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,在節點終端執行如下代碼:
curl -XPUT http://localhost:9200/medcl/-d'{"index" : {"analysis" : {"analyzer" : {"pinyin_analyzer" : {"tokenizer" : "my_pinyin","filter" : ["standard"]}},"tokenizer" : {"my_pinyin" : {"type" : "pinyin","first_letter" : "none","padding_char" : " "}}}}}'
- 通過瀏覽器測試分詞
http://10.110.13.144:9200/medcl/_analyze?text=%E5%88%98%E5%BE%B7%E5%8D%8E&analyzer=pinyin_analyzer
若測試成功,瀏覽器返回結果如下:
{"tokens":[{"token":"liudehua","start_offset":0,"end_offset":3,"type":"word","position":0}]}
建立拼音索引
- 建立索引並設置分詞
curl -XPOST http://localhost:9200/medcl/_closecurl -XPUT http://localhost:9200/medcl/_settings -d'{"index" : {"analysis" : {"analyzer" : {"pinyin_analyzer" : {"tokenizer" : "my_pinyin","filter" : ["standard"],"type":"pinyin"}},"tokenizer" : {"my_pinyin" : {"type" : "pinyin","first_letter" : "none","padding_char" : " "}}}}}'curl -XPOST http://localhost:9200/medcl/_open
- 建立mapping
curl -XPOST http://localhost:9200/medcl/folks/_mapping -d'{"folks": {"properties": {"name": {"type": "multi_field","fields": {"name": {"type": "string","store": "no","term_vector": "with_positions_offsets","analyzer": "pinyin_analyzer","boost": 10},"primitive": {"type": "string","store": "yes","analyzer": "keyword"}}}}}}'
- 上傳數據
curl -XPOST http://localhost:9200/medcl/folks/andy -d'{"name":"劉德華"}'
- 在瀏覽器請求檢索
http://10.110.13.144:9200/medcl/folks/_search?q=name:liudehua
若檢索成功,瀏覽器返回以下結果:
{"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":"劉德華"}}]}}
參考資料
附件列表
