概述
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的全部内容,希望文章能够帮你解决记录一次es商品门店查询,关键字SearchSourceBuilder所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复