@@ -392,10 +392,7 @@ public boolean createIndex(String indexName) {
392392 return false ;
393393 }
394394
395- /**
396- * 创建索引模板
397- */
398- public boolean createIndexTemplateIfNotExist (String indexTemplateName , String config ) {
395+ public boolean templateExist (String indexTemplateName ){
399396 ESClient esClient = null ;
400397
401398 try {
@@ -410,15 +407,37 @@ public boolean createIndexTemplateIfNotExist(String indexTemplateName, String co
410407 if (null != templateConfig ) {
411408 return true ;
412409 }
410+ } catch (Exception e ) {
411+ LOGGER .warn ( "method=templateExist||indexTemplateName={}||msg=exception!" ,
412+ indexTemplateName , e );
413+ } finally {
414+ if (esClient != null ) {
415+ this .returnESClientToPool (esClient );
416+ }
417+ }
418+
419+ return false ;
420+ }
421+
422+ /**
423+ * 创建索引模板
424+ */
425+ public boolean createIndexTemplateIfNotExist (String indexTemplateName , String config ) {
426+ ESClient esClient = null ;
427+
428+ try {
429+ esClient = this .getESClientFromPool ();
430+
431+ //存在模板就返回,不存在就创建
432+ if (templateExist (indexTemplateName )){return true ;}
413433
414434 // 创建新的模板
415435 ESIndicesPutTemplateResponse response = esClient .admin ().indices ().preparePutTemplate ( indexTemplateName )
416436 .setTemplateConfig ( config ).execute ().actionGet ( 30 , TimeUnit .SECONDS );
417437
418438 return response .getAcknowledged ();
419439 } catch (Exception e ) {
420- LOGGER .warn (
421- "class=ESOpClient||method=createIndexTemplateIfNotExist||indexTemplateName={}||config={}||msg=exception!" ,
440+ LOGGER .warn ( "method=createIndexTemplateIfNotExist||indexTemplateName={}||config={}||msg=exception!" ,
422441 indexTemplateName , config , e
423442 );
424443 } finally {
0 commit comments