ES插件开发之——如何添加自己的动态设置项

  

中,有一类参数是可以动态调整的,比如副本数量:<代码> number_of_replicas> 在插件开发中,如何添加自己的自定义参数呢?
在插件的入口,添加<代码> onModule (ClusterModule模块)即可。

  
 <代码>公共类ShgyPlugin扩展插件{
  @Override
  公共字符串名称(){
  返回“shgy-plugin”;
  }
  
  @Override
  公共字符串描述(){
  返回“shgy插件”;
  }
  
  公共空间onModule (ClusterModule模块){
  
  module.registerIndexDynamicSetting(“索引。custom_setting”,新确认器(){
  @Override
  公开验证字符串(字符串,字符串值,ClusterState ClusterState) {
  如果(value=https://www.yisu.com/zixun/=null) {
  把新NullPointerException(“价值不得空”);
  }
  返回null;
  }
  });
  }
  } 
  

编译代码,安装插件后,使用如下的脚本测试:

  
 <代码> curl - x将“localhost: 9200/twitter/_settings”- h“application/json - type:“- d”
  {
  "指数":{
  “custom_setting”: 2
  }
  }'
  
  旋度xget”http://localhost: 9200/微博/_settings ?漂亮的
   
  

在代码中使用参数,一般是在TransportAction中使用,代码片段如下:

  
 <代码> ClusterState ClusterState=clusterService.state ();
  clusterState.blocks () .globalBlockedRaiseException (ClusterBlockLevel.READ);
  
  字符串concreteSingleIndex=indexNameExpressionResolver。concreteSingleIndex (clusterState,请求);
  
  IndexMetaData indexMeta=clusterState.getMetaData () .index (concreteSingleIndex);
  int sectionCnt=indexMeta.getSettings () .getAsInt (" index.custom_settings ", 1);  
  

即通过clusterService获取到clusterState,然后获取到IndexMetaData,然后获取到设置。

  

自定义动态参数,配合模板的使用,就不需要频繁手动创建索引了。这个知识点应该归纳到ES插件开发的一部分。

ES插件开发之——如何添加自己的动态设置项