修复自动创建ES索引模版失败问题

This commit is contained in:
zengqiao
2022-09-28 19:44:33 +08:00
committed by EricZeng
parent 9fc9c54fa1
commit 95f1a2c630

View File

@@ -392,10 +392,7 @@ public class ESOpClient {
return false;
}
/**
* 创建索引模板
*/
public boolean createIndexTemplateIfNotExist(String indexTemplateName, String config) {
public boolean templateExist(String indexTemplateName){
ESClient esClient = null;
try {
@@ -410,6 +407,29 @@ public class ESOpClient {
if (null != templateConfig) {
return true;
}
} catch (Exception e) {
LOGGER.warn( "method=templateExist||indexTemplateName={}||msg=exception!",
indexTemplateName, e);
} finally {
if (esClient != null) {
this.returnESClientToPool(esClient);
}
}
return false;
}
/**
* 创建索引模板
*/
public boolean createIndexTemplateIfNotExist(String indexTemplateName, String config) {
ESClient esClient = null;
try {
esClient = this.getESClientFromPool();
//存在模板就返回,不存在就创建
if(templateExist(indexTemplateName)){return true;}
// 创建新的模板
ESIndicesPutTemplateResponse response = esClient.admin().indices().preparePutTemplate( indexTemplateName )
@@ -417,8 +437,7 @@ public class ESOpClient {
return response.getAcknowledged();
} catch (Exception e) {
LOGGER.warn(
"class=ESOpClient||method=createIndexTemplateIfNotExist||indexTemplateName={}||config={}||msg=exception!",
LOGGER.warn( "method=createIndexTemplateIfNotExist||indexTemplateName={}||config={}||msg=exception!",
indexTemplateName, config, e
);
} finally {