使用java 创建elasticsearch(7.10.10)中的索引,生命周期策略,索引模板

在项目初始化时新建索引,生命周期策略,索引模板,不需要调用es ApI 创建索引,设置mapping
需要的es的包名路径

import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.AcknowledgedResponse;
import org.elasticsearch.client.indexlifecycle.*;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
//创建索引
    public void createIndex(String indexName,String mapping,Esconfig esconfig){
        try {
            CreateIndexRequest request = new CreateIndexRequest(indexName);//创建索引
            //创建的每个索引都可以有与之关联的特定设置。
            request.settings(Settings.builder()
                    .put("index.number_of_shards", esconfig.getShards())
                    .put("index.number_of_replicas", esconfig.getReplicas())
                    .put("index.max_result_window",esconfig.getMaxResult())
            );
            //创建索引时创建文档类型映射
            request.mapping("_doc",mapping, XContentType.JSON);
            //先判断索引是否存在,存在则删除后再创建
            GetIndexRequest getIndexRequest = new GetIndexRequest(indexName);
            boolean exist = client.indices().exists(getIndexRequest,RequestOptions.DEFAULT);
            if(exist) {
                DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
                org.elasticsearch.action.support.master.AcknowledgedResponse response = client.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
                if(response.isAcknowledged()) {
                    logger.info("索引:" + indexName+",删除成功!");
                }
            }
            CreateIndexResponse createIndexResponse = client.indices().create(request,RequestOptions.DEFAULT);
            boolean res = createIndexResponse.isAcknowledged();
            if(res) {
                logger.info("索引:" + indexName +",创建成功!");
            }
        }catch (Exception e){
            logger.error("createIndex: " + e);
            System.exit(errCode);
        }
    }

//创建索引模板
public void createIndexTemplate(String indexTemplateMapping,Esconfig esconfig){
        try {
            PutIndexTemplateRequest request = new PutIndexTemplateRequest(INDEX_TEMPLATE);
            request.settings(Settings.builder()
                    .put("number_of_shards",esconfig.getShards())
                    .put("number_of_replicas",esconfig.getReplicas())
                    .put("index.lifecycle.name",POLICY_NAME)
                    .put("index.lifecycle.rollover_alias",ROLLOVER_ALIAS));
            request.mapping(indexTemplateMapping, XContentType.JSON);
            Alias alias = new Alias(FLOW_INDEX_NAME);
            request.alias(alias);
            List indexPatterns = new ArrayList<>();
            indexPatterns.add(COMMON_INDEX);
            request.patterns(indexPatterns);
            //先判断是否存在对应的索引模板,存在则删除
            IndexTemplatesExistRequest indexTemplatesExistRequest = new IndexTemplatesExistRequest(INDEX_TEMPLATE);
            boolean exist = client.indices().existsTemplate(indexTemplatesExistRequest,RequestOptions.DEFAULT);
            if(exist) {
                DeleteIndexTemplateRequest deleteIndexTemplateRequest = new DeleteIndexTemplateRequest(INDEX_TEMPLATE);
                org.elasticsearch.action.support.master.AcknowledgedResponse response = client.indices().deleteTemplate(deleteIndexTemplateRequest,RequestOptions.DEFAULT);
                if(response.isAcknowledged()) {
                    logger.info("索引模板:" + INDEX_TEMPLATE+ ",删除成功!");
                }
            }
            org.elasticsearch.action.support.master.AcknowledgedResponse acknowledgedResponse = client.indices().putTemplate(request, RequestOptions.DEFAULT);
            boolean res = acknowledgedResponse.isAcknowledged();
            if(res) {
                logger.info("索引模板:" + INDEX_TEMPLATE+ ",创建成功!");
                /**要开始工作,您需要引导初始索引,并将其指定为索引模板中指定的滚动别名的写入索引。此索引的名称必须与模板的索引模式匹配,并以数字结尾。
                 * 在滚转时,将递增此值以生成新索引的名称。**/

                CreateIndexRequest indexRequest = new CreateIndexRequest(FIRST_INDEX);
                Alias indexAlias = new Alias(ROLLOVER_ALIAS);
                indexAlias.writeIndex(true);
                indexRequest.alias(indexAlias);
                CreateIndexResponse createIndexResponse = client.indices().create(indexRequest,RequestOptions.DEFAULT);
                boolean result = createIndexResponse.isAcknowledged();
                if(result) {
                    logger.info("设置写入成功!");
                }
            }
        }catch (Exception e){
            logger.error("createIndexTemplate: " + e);
            System.exit(errCode);
        }
    }

// 创建生命周期策略
 public void createLifecyclePolicy (Esconfig esconfig){
        PutLifecyclePolicyRequest lifecyclePolicyRequest = null;
        try {
            Map phaseMap = new HashMap<>();
            //hot
            Map hotMap = new HashMap<>();
            int age = esconfig.getAge();
            ByteSizeValue byteSizeValue = new ByteSizeValue(esconfig.getMaxSize());
            hotMap.put(RolloverAction.NAME, new RolloverAction(byteSizeValue, TimeValue.timeValueDays(age),esconfig.getMaxDoc()));
            Phase hotPhase = new Phase(HOT,TimeValue.timeValueDays(age),hotMap);
            phaseMap.put(HOT,hotPhase);
            //warm
            Map warmMap = new HashMap<>();
            warmMap.put(ForceMergeAction.NAME,new ForceMergeAction(esconfig.getSegments()));
            warmMap.put(ShrinkAction.NAME,new ShrinkAction(esconfig.getShrink()));
            Phase warmPhase = new Phase(WARM,TimeValue.timeValueDays(age),warmMap);
            phaseMap.put(WARM,warmPhase);
            //cold
            Map coldMap = new HashMap<>();
            Phase coldPhase = new Phase(COLD,TimeValue.timeValueDays(age),coldMap);
            phaseMap.put(COLD,coldPhase);
            //delete
            Map deleteMap = new HashMap<>();
            deleteMap.put(DeleteAction.NAME,new DeleteAction());
            Phase deletePhase = new Phase(DELETE,TimeValue.timeValueDays(age),deleteMap);
            phaseMap.put(DELETE,deletePhase);
            //得到四个阶段的phase
            LifecyclePolicy policy = new LifecyclePolicy(POLICY_NAME,phaseMap);
            lifecyclePolicyRequest = new PutLifecyclePolicyRequest(policy);
            //删除引用这个生命周期的策略
            GetIndexRequest getIndexRequest = new GetIndexRequest(FLOW_INDEX_NAME);
            boolean exist = client.indices().exists(getIndexRequest,RequestOptions.DEFAULT);
            if(exist) {
                DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(COMMON_INDEX);
                org.elasticsearch.action.support.master.AcknowledgedResponse response = client.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
                if(response.isAcknowledged()) {
                    logger.info("索引:" + FLOW_INDEX_NAME+",删除成功!");
                }
            }
            //先判断生命周期策略是否存在,存在的话先删除后再创建
            DeleteLifecyclePolicyRequest deleteLifecyclePolicyRequest = new DeleteLifecyclePolicyRequest(POLICY_NAME);
            GetLifecyclePolicyRequest getRequest = new GetLifecyclePolicyRequest(POLICY_NAME);
            GetLifecyclePolicyResponse getLifecyclePolicyResponse = client.indexLifecycle().getLifecyclePolicy(getRequest,RequestOptions.DEFAULT);
            if(null != getLifecyclePolicyResponse) {
                AcknowledgedResponse response = client.indexLifecycle().deleteLifecyclePolicy(deleteLifecyclePolicyRequest,RequestOptions.DEFAULT);
                if(response.isAcknowledged()) {
                    logger.info("生命周期策略:" + POLICY_NAME+ ",删除成功!");
                }
            }
            createPolicy(lifecyclePolicyRequest);
        }catch (Exception e){
            /**
             * es 7.62版本不支持使用GetIndexRequest查询生命周期策略,若策略不存在,则会报如下异常,7.10版本是正常的,因此7.62版本需要在这里捕捉后直接创建生命周期策略
             * Elasticsearch exception [type=resource_not_found_exception, reason=Lifecycle policy not found: flow_policy]
             * **/
            if(e.getMessage().contains("resource_not_found_exception")) {
                try {
                    createPolicy(lifecyclePolicyRequest);
                } catch (Exception e1) {
                    logger.error("createPolicy: " + e1);
                    System.exit(errCode);
                }
            }else {
                logger.error("createLifecyclePolicy: " + e);
                System.exit(errCode);
            }
        }
    }

    private void createPolicy(PutLifecyclePolicyRequest lifecyclePolicyRequest) throws IOException {
        AcknowledgedResponse acknowledgedResponse = client.indexLifecycle().putLifecyclePolicy(lifecyclePolicyRequest,RequestOptions.DEFAULT);
        boolean res = acknowledgedResponse.isAcknowledged();
        if(res) {
            logger.info("生命周期策略:" + POLICY_NAME +",创建成功!");
        }
    }

你可能感兴趣的:(elasticsearch,java,搜索引擎)