Skip to content

Commit

Permalink
refactor: 优化索引模版创建逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed May 21, 2024
1 parent 3d7d3ef commit a819096
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,6 +16,7 @@
import java.util.List;
import java.util.Map;

@Slf4j
public abstract class TemplateElasticSearchIndexStrategy extends AbstractElasticSearchIndexStrategy {

public TemplateElasticSearchIndexStrategy(String id, ReactiveElasticsearchClient client, ElasticSearchIndexProperties properties) {
Expand Down Expand Up @@ -43,11 +45,16 @@ public String getIndexForSearch(String index) {

@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())));
}

Expand All @@ -59,6 +66,7 @@ protected PutIndexTemplateRequest createIndexTemplateRequest(ElasticSearchIndexM
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 {
Expand Down

0 comments on commit a819096

Please sign in to comment.