1515import org .elasticsearch .action .DocWriteResponse ;
1616import org .elasticsearch .action .admin .indices .alias .Alias ;
1717import org .elasticsearch .action .admin .indices .alias .IndicesAliasesRequest ;
18+ import org .elasticsearch .action .admin .indices .alias .get .GetAliasesRequest ;
1819import org .elasticsearch .action .admin .indices .create .CreateIndexRequest ;
1920import org .elasticsearch .action .admin .indices .delete .DeleteIndexRequest ;
2021import org .elasticsearch .action .admin .indices .get .GetIndexRequest ;
4041import org .elasticsearch .action .support .master .AcknowledgedResponse ;
4142import org .elasticsearch .action .update .UpdateRequest ;
4243import org .elasticsearch .action .update .UpdateResponse ;
44+ import org .elasticsearch .client .GetAliasesResponse ;
4345import org .elasticsearch .client .RequestOptions ;
4446import org .elasticsearch .client .RestHighLevelClient ;
47+ import org .elasticsearch .cluster .metadata .AliasMetaData ;
4548import org .elasticsearch .common .settings .Settings ;
4649import org .elasticsearch .common .unit .ByteSizeUnit ;
4750import org .elasticsearch .common .unit .ByteSizeValue ;
6568import java .util .List ;
6669import java .util .Map ;
6770import java .util .Objects ;
71+ import java .util .Set ;
6872import java .util .concurrent .TimeUnit ;
6973import java .util .function .BiConsumer ;
7074import java .util .stream .Collectors ;
@@ -194,6 +198,20 @@ public boolean isIndexExists(String index) throws IOException {
194198 return client .indices ().exists (request .indices (index ), RequestOptions .DEFAULT );
195199 }
196200
201+ public Set <String > getIndexSet (String alias ) throws IOException {
202+ GetAliasesRequest request = new GetAliasesRequest (alias );
203+ GetAliasesResponse response = client .indices ().getAlias (request , RequestOptions .DEFAULT );
204+ if (StrUtil .isNotBlank (response .getError ())) {
205+ String msg = StrUtil .format ("【ES】获取索引失败!alias: {}, error: {}" , alias , response .getError ());
206+ throw new ElasticsearchException (msg );
207+ }
208+ if (response .getException () != null ) {
209+ throw response .getException ();
210+ }
211+ Map <String , Set <AliasMetaData >> aliasMap = response .getAliases ();
212+ return aliasMap .keySet ();
213+ }
214+
197215 public void setMapping (String index , String type , Map <String , String > propertiesMap ) throws IOException {
198216
199217 if (MapUtil .isEmpty (propertiesMap )) {
@@ -459,7 +477,7 @@ public <T> List<T> pojoListByIds(String index, String type, Collection<String> i
459477 throws IOException {
460478
461479 if (CollectionUtil .isEmpty (ids )) {
462- return null ;
480+ return new ArrayList <>( 0 ) ;
463481 }
464482
465483 MultiGetRequest request = new MultiGetRequest ();
@@ -471,7 +489,7 @@ public <T> List<T> pojoListByIds(String index, String type, Collection<String> i
471489 if (null == multiGetResponse
472490 || multiGetResponse .getResponses () == null
473491 || multiGetResponse .getResponses ().length <= 0 ) {
474- return new ArrayList <>();
492+ return new ArrayList <>(0 );
475493 }
476494
477495 List <T > list = new ArrayList <>();
@@ -491,7 +509,7 @@ public <T> List<T> pojoListByIds(String index, String type, Collection<String> i
491509 public long count (String index , String type , SearchSourceBuilder builder ) throws IOException {
492510 SearchResponse response = query (index , type , builder );
493511 if (response == null || response .status () != RestStatus .OK ) {
494- return - 1L ;
512+ return 0L ;
495513 }
496514 SearchHits searchHits = response .getHits ();
497515 return searchHits .getTotalHits ();
@@ -550,15 +568,15 @@ public <T> PageData<T> pojoPage(String index, String type, int from, int size, Q
550568 /**
551569 * search after 分页
552570 */
553- public <T extends BaseEsEntity > ScrollData <T > pojoPageByLastId (String index , String type , String lastId , int size ,
571+ public <T extends BaseEsEntity > ScrollData <T > pojoPageByScrollId (String index , String type , String scrollId , int size ,
554572 QueryBuilder queryBuilder , Class <T > clazz ) throws IOException {
555573
556574 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder ();
557575 searchSourceBuilder .size (size );
558576 searchSourceBuilder .sort (BaseEsEntity .DOC_ID , SortOrder .ASC );
559- if (StrUtil .isNotBlank (lastId )) {
577+ if (StrUtil .isNotBlank (scrollId )) {
560578 BoolQueryBuilder boolQueryBuilder = QueryBuilders .boolQuery ();
561- boolQueryBuilder .must (queryBuilder ).must (QueryBuilders .rangeQuery (BaseEsEntity .DOC_ID ).gt (lastId ));
579+ boolQueryBuilder .must (queryBuilder ).must (QueryBuilders .rangeQuery (BaseEsEntity .DOC_ID ).gt (scrollId ));
562580 searchSourceBuilder .query (boolQueryBuilder );
563581 } else {
564582 searchSourceBuilder .query (queryBuilder );
@@ -639,25 +657,20 @@ public boolean pojoScrollEnd(String scrollId) throws IOException {
639657 }
640658
641659 public <T > T toPojo (GetResponse response , Class <T > clazz ) {
642- if (null == response ) {
643- return null ;
644- } else if (StrUtil .isBlank (response .getSourceAsString ())) {
660+ if (null == response || StrUtil .isBlank (response .getSourceAsString ())) {
645661 return null ;
646662 } else {
647663 return JsonUtil .toBean (response .getSourceAsString (), clazz );
648664 }
649665 }
650666
651667 public <T > List <T > toPojoList (SearchResponse response , Class <T > clazz ) {
652-
653668 if (response == null || response .status () != RestStatus .OK ) {
654- return new ArrayList <>();
669+ return new ArrayList <>(0 );
655670 }
656-
657671 if (ArrayUtil .isEmpty (response .getHits ().getHits ())) {
658- return new ArrayList <>();
672+ return new ArrayList <>(0 );
659673 }
660-
661674 return Stream .of (response .getHits ().getHits ())
662675 .map (hit -> JsonUtil .toBean (hit .getSourceAsString (), clazz ))
663676 .collect (Collectors .toList ());
0 commit comments