我使用spring-data-elasticsearch框架从elasticsearch服务器获取查询结果,这样的java代码:
public void testQuery() {
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withFields("createDate","updateDate").withQuery(matchAllQuery()).withPageable(new PageRequest(0,Integer.MAX_VALUE)).build();
List<Entity> list = template.queryForList(searchQuery, Entity.class);
for (Entity e : list) {
System.out.println(e.getCreateDate());
System.out.println(e.getUpdateDate());
}
}
我在服务器中获取原始查询日志,如下所示:
{"from":0,"size":10,"query":{"match_all":{}},"fields":["createDate","updateDate"]}
根据查询日志,spring-data-elasticsearch将为查询添加大小限制。 "from":0, "size":10
,如何避免添加大小限制?
答案 0 :(得分:4)
您不想这样做,您可以在返回Iterable的存储库上使用findAll功能。我认为获取所有项目的最佳方法是使用扫描/滚动功能。也许以下代码块可以让您朝着正确的方向前进:
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchAllQuery())
.withIndices("customer")
.withTypes("customermodel")
.withSearchType(SearchType.SCAN)
.withPageable(new PageRequest(0, NUM_ITEMS_PER_SCROLL))
.build();
String scrollId = elasticsearchTemplate.scan(searchQuery, SCROLL_TIME_IN_MILLIS, false);
boolean hasRecords = true;
while (hasRecords) {
Page<CustomerModel> page = elasticsearchTemplate.scroll(scrollId, SCROLL_TIME_IN_MILLIS, CustomerModel.class);
if (page != null) {
// DO something with the records
hasRecords = (page.getContent().size() == NUM_ITEMS_PER_SCROLL);
} else {
hasRecords = false;
}
}