用例是以最快的方式检索ES中的所有文档。我已经实现了ES - 滚动API,如下所示。实施步骤如下:
不幸的是,我只能检索前1000个文档,当我传递滚动ID以检索下一个1000时,我得到零结果。
public PageableDTO scanAndScroll(String apiVersion,String indexName,String indexType,String pageIndex,String scrollId) {
try {
final TimeValue timeoutScroll = TimeValue.timeValueMinutes(30L);
SearchResponse scrollResp;
//If scroll id is passed to the API use it else use the scroll ID from the
//matchAllResponse
if (StringUtils.isEmpty(scrollId)) {
scrollResp =
client.prepareSearch(indexName)
.setTypes(indexType)
.setFrom(Integer.valueOf(pageIndex)) // pageIndex should be 0 in normal usage!
.setScroll(timeoutScroll)
.setSize(1000)
.execute()
.actionGet();
}
else {
scrollResp =
client.prepareSearchScroll(scrollId)
.setScroll(timeoutScroll)
.execute()
.actionGet();
}
if (null != scrollResp)
{
for (SearchHit hit : scrollResp.getHits().getHits()) {
result.add(hit.getSource());
}
Map<String, Object> scrollMapping = new HashMap<>();
scrollMapping.put("ScrollId", scrollResp.getScrollId());
result.add(scrollMapping);
}
}
catch (Exception e) {
}
pageableDTO.setTotalRecords(result.size());
pageableDTO.setList(result);
return pageableDTO;
}