文档
https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
游标查询
keepAlive参数:
"5m" 表示保持查询上下文打开5分钟。常见的时长单位包括 m(分钟)、s(秒)等
如果需要更长的保持时间,可以在后续的 scroll 请求中通过 scroll 参数更新 keepAlive 的值。
使用:
String scrollKeepAlive = "30m"; // Scroll有效期为30分钟
String defaultValue = "20m"; // 滚动保持时间,例如1分钟
String scrollId = null;
RestHighLevelClient client= RestHighLevelClientUtil.getInstance(). getClient();
try {
// 第一步:启动Scroll查询
SearchRequest searchRequest = new SearchRequest(SearchConfig.COMPANY_OPERATE_BIDDING);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
……
searchSourceBuilder.query(boolQueryBuilder);
searchRequest.source(searchSourceBuilder);
searchRequest.scroll(TimeValue.parseTimeValue(scrollKeepAlive, defaultValue));
// String dsl = searchRequest.source().toString();
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
scrollId = searchResponse.getScrollId(); // 保存Scroll ID以便后续使用
// log.info("企业数量:"+searchResponse.getHits().getHits().length);
// 处理当前批次的结果
for (SearchHit hit : searchResponse.getHits().getHits()) {
String sourceAsString = hit.getSourceAsString();
cn.hutool.json.JSONObject source = JSONUtil.parseObj(sourceAsString);
JSONArray biddingEnterpriseList = source.getJSONArray("xgss");
}
while (true) {
// 第二步:获取更多批次
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
// 第二步:续时间
scrollRequest.scroll(TimeValue.parseTimeValue(scrollKeepAlive, defaultValue));
searchResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
// 处理当前批次的结果
for (SearchHit hit : searchResponse.getHits().getHits()) {
String sourceAsString = hit.getSourceAsString();
cn.hutool.json.JSONObject source = JSONUtil.parseObj(sourceAsString);
JSONArray biddingEnterpriseList = source.getJSONArray("xgss");
}
// 检查是否还有更多结果
if (searchResponse.getHits().getHits().length == 0) {
break;
}
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
// 第三步:清理Scroll
if (scrollId != null) {
ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
clearScrollRequest.addScrollId(scrollId);
try {
client.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}