refactor: 优化索引模版创建逻辑

This commit is contained in:
zhouhao 2024-05-21 09:45:44 +08:00
parent 3d7d3efc86
commit a819096019
1 changed files with 10 additions and 2 deletions

View File

@ -1,5 +1,6 @@
package org.jetlinks.community.elastic.search.index.strategies;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.alias.Alias;
@ -15,6 +16,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public abstract class TemplateElasticSearchIndexStrategy extends AbstractElasticSearchIndexStrategy {
public TemplateElasticSearchIndexStrategy(String id, ReactiveElasticsearchClient client, ElasticSearchIndexProperties properties) {
@ -43,11 +45,16 @@ public abstract class TemplateElasticSearchIndexStrategy extends AbstractElastic
@Override
public Mono<ElasticSearchIndexMetadata> putIndex(ElasticSearchIndexMetadata metadata) {
String saveIndex = getIndexForSave(metadata.getIndex());
return client
.putTemplate(createIndexTemplateRequest(metadata))
//修改当前索引
.then(doPutIndex(metadata.newIndexName(getIndexForSave(metadata.getIndex())), true))
.then(doPutIndex(metadata.newIndexName(saveIndex), true)
//忽略修改索引错误
.onErrorResume(err -> {
log.warn("Update Index[{}] Mapping error", saveIndex, err);
return Mono.empty();
}))
.thenReturn(metadata.newIndexName(wrapIndex(metadata.getIndex())));
}
@ -59,6 +66,7 @@ public abstract class TemplateElasticSearchIndexStrategy extends AbstractElastic
Map<String, Object> mappingConfig = new HashMap<>();
mappingConfig.put("properties", createElasticProperties(metadata.getProperties()));
mappingConfig.put("dynamic_templates", createDynamicTemplates());
mappingConfig.put("_source", Collections.singletonMap("enabled", true));
if (client.serverVersion().after(Version.V_7_0_0)) {
request.mapping(mappingConfig);
} else {