ES插件开发之--如何添加本身的动态设置项

ES中,有一类参数是能够动态调整的,好比副本数量: number_of_replicas
在插件开发中,如何添加本身的自定义参数呢?
在插件的入口,添加onModule(ClusterModule module)便可。json

public class ShgyPlugin extends Plugin {
    @Override
    public String name() {
        return "shgy-plugin";
    }

    @Override
    public String description() {
        return "shgy plugin ";
    }

    public void onModule(ClusterModule module){

        module.registerIndexDynamicSetting("index.custom_setting", new Validator() {
            @Override
            public String validate(String setting, String value, ClusterState clusterState) {
                if (value == null) {
                    throw new NullPointerException("value must not be null");
                }
                return null;
            }
        });
    }
}

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

curl -X PUT "localhost:9200/twitter/_settings" -H 'Content-Type: application/json' -d'
{
    "index" : {
        "custom_setting" : 2
    }
}'

curl -XGET 'http://localhost:9200/twitter/_settings?pretty'

在代码中使用参数,通常是在TransportAction中使用, 代码片断以下:curl

ClusterState clusterState = clusterService.state();
  clusterState.blocks().globalBlockedRaiseException(ClusterBlockLevel.READ);

  String concreteSingleIndex = indexNameExpressionResolver.concreteSingleIndex(clusterState, request);

  IndexMetaData indexMeta = clusterState.getMetaData().index(concreteSingleIndex);
  int sectionCnt = indexMeta.getSettings().getAsInt("index.custom_settings",-1);

即经过clusterService获取到clusterState, 而后获取到IndexMetaData, 而后获取到Settings。ide

自定义动态参数, 配合templates的使用,就不须要频繁手动建立索引了。 这个知识点应该概括到 ES插件开发的一部分。测试

相关文章
相关标签/搜索