第一步:安裝ElasticSearch 7.6.0
由於IK和ElasticSerach已經是編譯好的,不需要編譯,直接在安裝解壓修改配置文件即可
詳情請見另一篇安裝博客:https://www.cnblogs.com/haolb123/p/14051580.html
第二步:下載拼音分詞器
由於拼音分詞器並非編譯好的,需要用Maven package 進行打包,maven的下載和使用請百度。
- pingying 下載地址: https://github.com/medcl/elasticsearch-analysis-pinyin
- 中文 分詞器地址:https://github.com/medcl/elasticsearch-analysis-ik
- 選擇完畢版本,進行選擇Zip包鏈接進行下載
下載到本地,進行解壓,解壓完畢之后發現是源碼Maven工程,進行mvn package,master版本對應Es7.X,默認打包出來是7.7.0,我的es版本是7.6.0,
所以要修改C:\Users\Administrator\Downloads\elasticsearch-analysis-pinyin-master\elasticsearch-analysis-pinyin-master\pom.xml的文件。修改后如下:
- 執行打包命令:mvn package
- 打包完成后編譯后的包在target/releases目錄下的elasticsearch-analysis-pinyin-7.6.0.zip
-
將編譯好的zip包進行解壓,進行拷貝,上傳到ElasticSerach的 plugins的文件夾下
- 重啟ElasticSearch:systemctl restart elasticsearch.service
第三步:測試拼音分詞器
創建索引
-
PUT /medcl/
-
{
-
"settings" : {
-
"analysis" : {
-
"analyzer" : {
-
"pinyin_analyzer" : {
-
"tokenizer" : "my_pinyin"
-
}
-
},
-
"tokenizer" : {
-
"my_pinyin" : {
-
"type" : "pinyin",
-
"keep_separate_first_letter" : false,
-
"keep_full_pinyin" : true,
-
"keep_original" : true,
-
"limit_first_letter_length" : 16,
-
"lowercase" : true,
-
"remove_duplicated_term" : true
-
}
-
}
-
}
-
}
-
}
測試分析器,分析一個中文名字
-
GET /medcl/_analyze
-
{
-
"text": ["郝利斌"],
-
"analyzer": "pinyin_analyzer"
-
}
返回查詢結果
創建映射
-
POST /medcl/_mapping
-
{
-
"properties": {
-
"name": {
-
"type": "keyword",
-
"fields": {
-
"pinyin": {
-
"type": "text",
-
"store": false,
-
"term_vector": "with_offsets",
-
"analyzer": "pinyin_analyzer",
-
"boost": 10
-
}
-
}
-
}
-
}
-
}
-
#添加單條數據
-
POST /medcl/_create/andy
-
{"name":"劉德華"}
-
-
#批量添加數據
-
POST /medcl/_bulk
-
{"index": {"_index": "medcl","_id": "1"}}
-
{"name":"儲之源"}
-
{"index": {"_index": "medcl","_id": "2"}}
-
{"name":"李根"}
-
{"index": {"_index": "medcl","_id": "3"}}
-
{"name":"趙陽"}
-
{"index": {"_index": "medcl","_id": "4"}}
-
{"name":"郝利斌"}
-
{"index": {"_index": "medcl","_id": "5"}}
-
{"name":"張旭"}
-
{"index": {"_index": "medcl","_id": "6"}}
-
{"name":"朱強"}
-
{"index": {"_index": "medcl","_id": "7"}}
-
{"name":"黃佳寧"}
-
{"index": {"_index": "medcl","_id": "8"}}
-
{"name":"袁曉軍"}
-
#根據條件查詢
-
GET /medcl/_search
-
{
-
"query": {
-
"match": {
-
"name.pinyin": "yxj"
-
}
-
}
-
}
返回結果: