elasticsearch 自定義分詞器git
安裝拼音分詞器、ik分詞器github
拼音分詞器: https://github.com/medcl/elasticsearch-analysis-pinyin/releasesspring
ik分詞器:https://github.com/medcl/elasticsearch-analysis-ik/releasesjson
下載源碼須要使用maven打包app
下載構建好的壓縮包解壓後放直接在elasticsearch安裝目錄下 plugins文件夾下,能夠重命名elasticsearch
1.在es中設置分詞maven
建立索引,添加setting屬性spa
PUT myindex { "settings": { "index":{ "analysis":{ "analyzer":{ "ik_pinyin_analyzer":{ "type":"custom", "tokenizer":"ik_smart", "filter":"pinyin_filter" } }, "filter":{ "pinyin_filter":{ "type":"pinyin", "keep_separate_first_letter" : false,
"keep_full_pinyin" : true,
"keep_original" : false,
"limit_first_letter_length" : 10,
"lowercase" : true,
"remove_duplicated_term" : true } } } } } }
添加屬性 設置mapping屬性code
PUT myindex/_mapping/users { "properties": { "uname":{ "type": "text", "analyzer": "ik_smart", "search_analyzer": "ik_smart", "fields": { "my_pinyin":{ "type": "text" , "analyzer": "ik_pinyin_analyzer", "search_analyzer": "ik_pinyin_analyzer" } } }, "age":{ "type": "integer" } } }
2.spring data elasticsearch設置分詞blog
建立實體類
@Mapping(mappingPath = "elasticsearch_mapping.json")//設置mapping
@Setting(settingPath = "elasticsearch_setting.json")//設置setting
@Document(indexName = "myindex",type = "users")
public class User {
@Id
private Integer id;
//
// @Field(type =FieldType.keyword ,analyzer = "pinyin_analyzer",searchAnalyzer = "pinyin_analyzer")//沒有做用
private String name1;
@Field(type = FieldType.keyword)
private String userName;
@Field(type = FieldType.Nested)
private List<Product> products;
}
在resources下建立elasticsearch_mapping.json 文件
{ "properties": { "uname": { "type": "text", "analyzer": "ik_smart", "search_analyzer": "ik_smart", "fields": { "my_pinyin": { "type": "text", "analyzer": "ik_pinyin_analyzer", "search_analyzer": "ik_pinyin_analyzer" } } }, "age": { "type": "integer" } } }
在resources下建立elasticsearch_setting.json 文件
{
"index": {
"analysis": {
"analyzer": {
"ik_pinyin_analyzer": {
"type": "custom",
"tokenizer": "ik_smart",
"filter": "pinyin_filter"
}
},
"filter": {
"pinyin_filter": {
"type": "pinyin",
//true:支持首字母
"keep_first_letter":true,
//false:不支持首字母分隔
"keep_separate_first_letter": false,
//true:支持全拼
"keep_full_pinyin": true,
"keep_original": false,
//設置最大長度
"limit_first_letter_length": 10,
//小寫非中文字母
"lowercase": true,
//重複的項將被刪除
"remove_duplicated_term": true
}
}
}
}
}
程序啓動後分詞並無設置分詞
實體建立後須要加上,建立的索引才能夠分詞
elasticsearchTemplate.putMapping(User.class);