方法一,直接更新
-
Close index
image.png - 更新_settings,索引(eg. indexusers)增加pinyin_analyzer
PUT /indexusers/_settings
{
"index" : {
"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
}
}
}
}
}
- Open index
- 更新mapping,给需要的字段(eg. name)指定pinyin_analyzer
PUT indexusers/_mapping/user
{
"properties": {
"name": {
"type": "text",
"fields": {
"pinyin": {
"type": "text",
"store": false,
"term_vector": "with_offsets",
"analyzer": "pinyin_analyzer",
"boost": 10
}
}
}
}
}
- 测试analyzer
GET /indexusers/_analyze
{
"text": ["南岩飞雪"],
"analyzer": "pinyin_analyzer"
}
Response
{
"tokens" : [
{
"token" : "nan",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 0
},
{
"token" : "南岩飞雪",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 0
},
{
"token" : "nyfx",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 0
},
{
"token" : "yan",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 1
},
{
"token" : "fei",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 2
},
{
"token" : "xue",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 3
}
]
}
- 添加数据,使用pinyin查询
PUT indexusers/user/id000
{
"name" : "南岩飞雪"
}
GET indexusers/_search?q=name.pinyin:nyfx
- pinyin_analyzer生效后,后续更新的数据,name字段都支持pinyin分词检索;之前的数据,需要自己触发更新
方法二,重建索引
- 新建索引
PUT /indexusers2/
{
"index" : {
"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
}
}
}
}
}
- _reindex
POST _reindex
{
"source": {
"index": "indexusers"
},
"dest": {
"index": "indexusers2"
}}
- 索引别名
{
"actions":[
{
"remove":{
"alias":"product",
"index":"indexusers"
}
},
{
"add":{
"alias":"product",
"index":"indexusers2"
}
}
]
}
网友评论