BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("request_time").gte(startTime).lt(endTime));
String[] includes = new String[]{""};
SearchResponse response = getResponse(es_index_ss_cardresponse_operate, boolQueryBuilder, includes,
null, 10000);
if (null != response) {
SearchHit[] searchHit = response.getHits().getHits();
cfEsResHandleService.quota12(numArr, searchHit, tableList, toKafka);
String scrollId = response.getScrollId();
while (null != searchHit && searchHit.length > 0) {
response = getResponse(es_index_ss_cardresponse_operate, null, includes,
scrollId, 10000);
searchHit = response.getHits().getHits();
cfEsResHandleService.quota12(numArr, searchHit, tableList, toKafka);
}
//清除滚屏
boolean success = clearScroll(scrollId);
if (success) {
LOGGER.info("清除滚屏成功!");
} else {
LOGGER.info("清除滚屏失败!");
}
}
/**
* 获取es查询结果
*
* @param esIndex
* @param boolQueryBuilder
* @param scrollId
* @param size
* @return
*/
public SearchResponse getResponse(String esIndex, BoolQueryBuilder boolQueryBuilder, String[] includes, String
scrollId, int size) {
SearchResponse response = null;
try {
RestHighLevelClient restHighLevelClient = esClient.getClient();
if (null != boolQueryBuilder && null == scrollId) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(boolQueryBuilder);
searchSourceBuilder.explain(false);//大于查询结果不打分
searchSourceBuilder.size(size);
//传了查询过滤字段才过滤结果,否则取全部字段
if (null != includes && includes.length > 0) {
//设置要获取的字段和不要获取的字段
searchSourceBuilder.fetchSource(includes, new String[]{});
}
SearchRequest searchRequest = new SearchRequest().indices(esIndex + "_" + index_suffix).source(searchSourceBuilder);
//size 为0不能使用scroll,否则会抛异常
if (size != 0) {
searchRequest.scroll(TimeValue.MINUS_ONE);
}
response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
}
if (null == boolQueryBuilder && null != scrollId) {
SearchScrollRequest searchScrollRequest = new SearchScrollRequest(scrollId).scroll(TimeValue.MINUS_ONE);
response = restHighLevelClient.scroll(searchScrollRequest, RequestOptions.DEFAULT);
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
return response;
}