From a819096019fa870beb464b5f1bad74f91f7a7f95 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Tue, 21 May 2024 09:45:44 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=E6=A8=A1=E7=89=88=E5=88=9B=E5=BB=BA=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TemplateElasticSearchIndexStrategy.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/jetlinks-components/elasticsearch-component/src/main/java/org/jetlinks/community/elastic/search/index/strategies/TemplateElasticSearchIndexStrategy.java b/jetlinks-components/elasticsearch-component/src/main/java/org/jetlinks/community/elastic/search/index/strategies/TemplateElasticSearchIndexStrategy.java index c01087897..8195aafb9 100755 --- a/jetlinks-components/elasticsearch-component/src/main/java/org/jetlinks/community/elastic/search/index/strategies/TemplateElasticSearchIndexStrategy.java +++ b/jetlinks-components/elasticsearch-component/src/main/java/org/jetlinks/community/elastic/search/index/strategies/TemplateElasticSearchIndexStrategy.java @@ -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.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 String getIndexForSearch(String index) { @Override public Mono 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 @@ protected PutIndexTemplateRequest createIndexTemplateRequest(ElasticSearchIndexM Map 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 {