甜思思 发表于 2017-5-21 10:30:15

通过关闭ElasticSearch的索引使索引配置变更立即生效

问题
  在处理elasticsearch的时候,通常需要不断地调整索引的配置,以期达到期望的效果。最近在试验各种analyzer的效果的时候就碰到一个问题:修改索引配置后,并不能立即生效。后来才发现需要先关闭索引,然后再打开才能生效。

过程
  下面是我的过程:

创建索引:

curl -XPUT http://localhost:9200/analyzetest/ -d '
{
"settings":{
"analysis":{
"analyzer":{
"testpatternanalyzer":{
"type":"pattern",
"pattern":"\\s+"
}
}
}
}
}
'

测试分析器效果:

curl -Xpost http://localhost:9200/analyzetest/_analyze?analyzer=testpatternanalyzer -d '
hello elasticsearch
'

  效果达成,试验一个自定义的分析器,该分析器采用nGram的tokenizer:

更新索引配置:

curl -XPUT http://localhost:9200/analyzetest/_settings -d '
{
"analysis":{
"analyzer":{
"acompoundenglish":{
"type":"custom",
"tokenizer":"my_ngram_tokenizer",
"filter":[
"my_ngram_filter"
]
}
},
"tokenizer":{
"my_ngram_tokenizer":{
"type":"nGram",
"min_gram":1,
"max_gram":3
}
},
"filter":{
"my_ngram_filter":{
"type":"nGram",
"min_gram":1,
"max_gram":3
}
}
}
}
'
 返回:

{
"ok": true
}

测试效果:

curl -Xpost http://localhost:9200/analyzetest/_analyze?analyzer=compoundenglish -d '
english
'
  结果返回的token只有一个:“english”。这明显不是nGram应该达到的效果。反复试验过几次均不见效果。
  通过

curl -Xget http://localhost:9200/analyzetest/_settings
  发现第二次设置的索引配置根本没有出现。可是更新配置的时候明名返回成功的。
  原来(https://groups.google.com/forum/#!msg/elasticsearch/dVPq9GAh8-g/Gs3Lg7Zjt1QJ)需要先关闭索引后才能生效:


kimchy 写道

It might just be the number of replicas setting, where it should handle things better in case of a closed index. All other settings should work. Open an issue for the number of replica settings update problem on a closed index.
  通过 

curl -XPOST http://localhost:9200/analyzetest/_close
curl -XPOST http://localhost:9200/analyzetest/_open
  发现更新过的配置生效了,分析器也返回了期望的效果。
页: [1]
查看完整版本: 通过关闭ElasticSearch的索引使索引配置变更立即生效