2

我使用 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, 我怎样才能避免它添加大小限制?

4

1 回答 1

5

您不想这样做,您可以在返回 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;
        }
    }
于 2015-06-09T21:26:06.530 回答