refactor: 优化索引模版创建逻辑
This commit is contained in:
parent
3d7d3efc86
commit
a819096019
|
|
@ -1,5 +1,6 @@
|
||||||
package org.jetlinks.community.elastic.search.index.strategies;
|
package org.jetlinks.community.elastic.search.index.strategies;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.collections4.CollectionUtils;
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
import org.elasticsearch.Version;
|
import org.elasticsearch.Version;
|
||||||
import org.elasticsearch.action.admin.indices.alias.Alias;
|
import org.elasticsearch.action.admin.indices.alias.Alias;
|
||||||
|
|
@ -15,6 +16,7 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public abstract class TemplateElasticSearchIndexStrategy extends AbstractElasticSearchIndexStrategy {
|
public abstract class TemplateElasticSearchIndexStrategy extends AbstractElasticSearchIndexStrategy {
|
||||||
|
|
||||||
public TemplateElasticSearchIndexStrategy(String id, ReactiveElasticsearchClient client, ElasticSearchIndexProperties properties) {
|
public TemplateElasticSearchIndexStrategy(String id, ReactiveElasticsearchClient client, ElasticSearchIndexProperties properties) {
|
||||||
|
|
@ -43,11 +45,16 @@ public abstract class TemplateElasticSearchIndexStrategy extends AbstractElastic
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<ElasticSearchIndexMetadata> putIndex(ElasticSearchIndexMetadata metadata) {
|
public Mono<ElasticSearchIndexMetadata> putIndex(ElasticSearchIndexMetadata metadata) {
|
||||||
|
String saveIndex = getIndexForSave(metadata.getIndex());
|
||||||
return client
|
return client
|
||||||
.putTemplate(createIndexTemplateRequest(metadata))
|
.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())));
|
.thenReturn(metadata.newIndexName(wrapIndex(metadata.getIndex())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -59,6 +66,7 @@ public abstract class TemplateElasticSearchIndexStrategy extends AbstractElastic
|
||||||
Map<String, Object> mappingConfig = new HashMap<>();
|
Map<String, Object> mappingConfig = new HashMap<>();
|
||||||
mappingConfig.put("properties", createElasticProperties(metadata.getProperties()));
|
mappingConfig.put("properties", createElasticProperties(metadata.getProperties()));
|
||||||
mappingConfig.put("dynamic_templates", createDynamicTemplates());
|
mappingConfig.put("dynamic_templates", createDynamicTemplates());
|
||||||
|
mappingConfig.put("_source", Collections.singletonMap("enabled", true));
|
||||||
if (client.serverVersion().after(Version.V_7_0_0)) {
|
if (client.serverVersion().after(Version.V_7_0_0)) {
|
||||||
request.mapping(mappingConfig);
|
request.mapping(mappingConfig);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue