15
15
import org .elasticsearch .action .DocWriteResponse ;
16
16
import org .elasticsearch .action .admin .indices .alias .Alias ;
17
17
import org .elasticsearch .action .admin .indices .alias .IndicesAliasesRequest ;
18
+ import org .elasticsearch .action .admin .indices .alias .get .GetAliasesRequest ;
18
19
import org .elasticsearch .action .admin .indices .create .CreateIndexRequest ;
19
20
import org .elasticsearch .action .admin .indices .delete .DeleteIndexRequest ;
20
21
import org .elasticsearch .action .admin .indices .get .GetIndexRequest ;
40
41
import org .elasticsearch .action .support .master .AcknowledgedResponse ;
41
42
import org .elasticsearch .action .update .UpdateRequest ;
42
43
import org .elasticsearch .action .update .UpdateResponse ;
44
+ import org .elasticsearch .client .GetAliasesResponse ;
43
45
import org .elasticsearch .client .RequestOptions ;
44
46
import org .elasticsearch .client .RestHighLevelClient ;
47
+ import org .elasticsearch .cluster .metadata .AliasMetaData ;
45
48
import org .elasticsearch .common .settings .Settings ;
46
49
import org .elasticsearch .common .unit .ByteSizeUnit ;
47
50
import org .elasticsearch .common .unit .ByteSizeValue ;
65
68
import java .util .List ;
66
69
import java .util .Map ;
67
70
import java .util .Objects ;
71
+ import java .util .Set ;
68
72
import java .util .concurrent .TimeUnit ;
69
73
import java .util .function .BiConsumer ;
70
74
import java .util .stream .Collectors ;
@@ -194,6 +198,20 @@ public boolean isIndexExists(String index) throws IOException {
194
198
return client .indices ().exists (request .indices (index ), RequestOptions .DEFAULT );
195
199
}
196
200
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
+
197
215
public void setMapping (String index , String type , Map <String , String > propertiesMap ) throws IOException {
198
216
199
217
if (MapUtil .isEmpty (propertiesMap )) {
@@ -459,7 +477,7 @@ public <T> List<T> pojoListByIds(String index, String type, Collection<String> i
459
477
throws IOException {
460
478
461
479
if (CollectionUtil .isEmpty (ids )) {
462
- return null ;
480
+ return new ArrayList <>( 0 ) ;
463
481
}
464
482
465
483
MultiGetRequest request = new MultiGetRequest ();
@@ -471,7 +489,7 @@ public <T> List<T> pojoListByIds(String index, String type, Collection<String> i
471
489
if (null == multiGetResponse
472
490
|| multiGetResponse .getResponses () == null
473
491
|| multiGetResponse .getResponses ().length <= 0 ) {
474
- return new ArrayList <>();
492
+ return new ArrayList <>(0 );
475
493
}
476
494
477
495
List <T > list = new ArrayList <>();
@@ -491,7 +509,7 @@ public <T> List<T> pojoListByIds(String index, String type, Collection<String> i
491
509
public long count (String index , String type , SearchSourceBuilder builder ) throws IOException {
492
510
SearchResponse response = query (index , type , builder );
493
511
if (response == null || response .status () != RestStatus .OK ) {
494
- return - 1L ;
512
+ return 0L ;
495
513
}
496
514
SearchHits searchHits = response .getHits ();
497
515
return searchHits .getTotalHits ();
@@ -550,15 +568,15 @@ public <T> PageData<T> pojoPage(String index, String type, int from, int size, Q
550
568
/**
551
569
* search after 分页
552
570
*/
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 ,
554
572
QueryBuilder queryBuilder , Class <T > clazz ) throws IOException {
555
573
556
574
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder ();
557
575
searchSourceBuilder .size (size );
558
576
searchSourceBuilder .sort (BaseEsEntity .DOC_ID , SortOrder .ASC );
559
- if (StrUtil .isNotBlank (lastId )) {
577
+ if (StrUtil .isNotBlank (scrollId )) {
560
578
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 ));
562
580
searchSourceBuilder .query (boolQueryBuilder );
563
581
} else {
564
582
searchSourceBuilder .query (queryBuilder );
@@ -639,25 +657,20 @@ public boolean pojoScrollEnd(String scrollId) throws IOException {
639
657
}
640
658
641
659
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 ())) {
645
661
return null ;
646
662
} else {
647
663
return JsonUtil .toBean (response .getSourceAsString (), clazz );
648
664
}
649
665
}
650
666
651
667
public <T > List <T > toPojoList (SearchResponse response , Class <T > clazz ) {
652
-
653
668
if (response == null || response .status () != RestStatus .OK ) {
654
- return new ArrayList <>();
669
+ return new ArrayList <>(0 );
655
670
}
656
-
657
671
if (ArrayUtil .isEmpty (response .getHits ().getHits ())) {
658
- return new ArrayList <>();
672
+ return new ArrayList <>(0 );
659
673
}
660
-
661
674
return Stream .of (response .getHits ().getHits ())
662
675
.map (hit -> JsonUtil .toBean (hit .getSourceAsString (), clazz ))
663
676
.collect (Collectors .toList ());
0 commit comments