SearchSourceBuilder 使用说明
参考:https://www.cnblogs.com/reycg-blog/p/9946821.html
@Override
public List<String> baseProductShopNotExistEsSearch(ReqBaseProductShopPageEsQueryVO reqVO) {
try {
SearchRequest searchRequest = new SearchRequest(EsOperateTables.BASE_PRODUCT_SHOP_SAP.getAlias());
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.fetchSource("shop_code", null);//过滤门店编码为null数据
Collection<String> ids = reqVO.getIds();
if (CollectionUtils.isNotEmpty(ids)) {
ids=ids.stream().distinct().collect(Collectors.toList());
sourceBuilder.query(QueryBuilders.termsQuery(EsUtils.ID, ids));//根据ID查询
}else{
throw new RRException("请求参数不能为空");
}
sourceBuilder.timeout(TimeValue.timeValueMinutes(2L));
sourceBuilder.size(new Long(ConstantsUtils.QueryPartams.MAX_RESULT_COUNT).intValue());//分页量
sourceBuilder.sort(EsUtils.ID, SortOrder.DESC);//排序
searchRequest.source(sourceBuilder);
//scroll 分页搜索
Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L));
searchRequest.scroll(scroll);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
String scrollId = searchResponse.getScrollId();
SearchHit[] searchHits = searchResponse.getHits().getHits();
List<String> mapList = Lists.newArrayList();
ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
while (true) {
if(StringUtils.isNotBlank(scrollId)){
clearScrollRequest.addScrollId(scrollId);
}
if(searchHits == null || searchHits.length == 0){
break;
}
Arrays.stream(searchHits).forEach(hit -> mapList.add(hit.getId()));
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
scroll = new Scroll(TimeValue.timeValueMinutes(1L));
scrollRequest.scroll(scroll);
searchResponse = restHighLevelClient.scroll(scrollRequest, RequestOptions.DEFAULT);
scrollId = searchResponse.getScrollId();
searchHits = searchResponse.getHits().getHits();
}
ClearScrollResponse clearScrollResponse = restHighLevelClient.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
boolean succeeded = clearScrollResponse.isSucceeded();
if(!succeeded){
throw new RRException("清除es游标失败");
}
if(CollectionUtils.isNotEmpty(mapList)){
return Lists.newArrayList(CollectionUtils.disjunction(ids,mapList));
}else{
return Lists.newArrayList(ids);
}
} catch (Exception e) {
log.error(e.getMessage());
throw new RRException(e.getMessage());
}
}
记录一次es商品门店查询,关键字SearchSourceBuilder
最新推荐文章于 2024-10-15 10:11:30 发布