package com.arms.config; import com.arms.egovframework.javaservice.esframework.annotation.ElasticSearchTemplateConfig; import com.arms.egovframework.javaservice.esframework.annotation.RollingIndexName; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.client.indices.GetIndexResponse; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.IndexOperations; import org.springframework.data.elasticsearch.core.index.AliasAction; import org.springframework.data.elasticsearch.core.index.AliasActionParameters; import org.springframework.data.elasticsearch.core.index.AliasActions; import org.springframework.data.elasticsearch.core.index.PutTemplateRequest; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.Set; import static com.arms.egovframework.javaservice.esframework.util.ReflectionUtil.findAnnotatedClasses; import static com.arms.egovframework.javaservice.esframework.util.ReflectionUtil.methodInfo; @Component @AllArgsConstructor @Slf4j public class EsIndexTemplateConfig { private final ElasticsearchOperations operations; private final RestHighLevelClient restHighLevelClient; @PostConstruct public void run() { Set annotatedClasses = findAnnotatedClasses(ElasticSearchTemplateConfig.class, "com.arms.*"); annotatedClasses.stream().map(clazz -> { try { return Class.forName(clazz); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(e); } }).forEach(clazz->{ Document document = AnnotationUtils.findAnnotation(clazz, Document.class); if(document!=null){ IndexCoordinates indexCoordinatesFor = operations.getIndexCoordinatesFor(clazz); String indexName = indexCoordinatesFor.getIndexName(); var templateName = indexName+"-template"; var templatePattern = indexName+"-*"; var indexOperations = operations.indexOps(clazz); if (!indexOperations.existsTemplate(templateName)) { makeTemplate(clazz, indexName, templateName, templatePattern, indexOperations); } } }); } private void makeTemplate(Class clazz, String indexName, String templateName, String templatePattern, IndexOperations indexOperations) { log.info("template-{} 생성진행", templateName); var mapping = indexOperations.createMapping(); var request = PutTemplateRequest.builder(templateName, templatePattern) .withMappings(mapping) .withAliasActions(new AliasActions().add( new AliasAction.Add(AliasActionParameters.builderForTemplate() .withAliases(indexOperations.getIndexCoordinates().getIndexNames()) .build()) )) .build(); try{ indexOperations.putTemplate(request); }catch (Exception e){ log.error("템플레이트 생성에러 : {}",e.getMessage()); } try{ Method method = methodInfo(clazz, RollingIndexName.class); Constructor constructor = clazz.getConstructor(); Object o = constructor.newInstance(); String rollingIndexName = indexName + "-" + method.invoke(o); GetIndexRequest getIndexRequest = new GetIndexRequest(rollingIndexName); boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT); if(!exists){ CreateIndexRequest createIndexRequest = new CreateIndexRequest(rollingIndexName); restHighLevelClient.indices().create(createIndexRequest,RequestOptions.DEFAULT); } }catch (Exception e) { log.error("인덱스 생성에러 : {}",e.getMessage()); } try{ GetIndexResponse getIndexResponse = restHighLevelClient.indices() .get(new GetIndexRequest(indexName + "*"), RequestOptions.DEFAULT); AliasActions add = new AliasActions().add( new AliasAction.Add(AliasActionParameters.builderForTemplate() .withAliases(indexOperations.getIndexCoordinates().getIndexNames()) .withIndices(getIndexResponse.getIndices()) .build()) ); indexOperations.alias(add); }catch (Exception e){ log.error("알리아스 생성에러 : {}",e.getMessage()); } } }