【问题标题】:how to disable page query in Spring-data-elasticsearch如何在 Spring-data-elasticsearch 中禁用页面查询
【发布时间】:2015-08-24 04:19:38
【问题描述】:

我使用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,如何避免添加大小限制?

【问题讨论】:

    标签: java elasticsearch spring-data-elasticsearch


    【解决方案1】:

    您不想这样做,您可以在返回 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;
            }
        }
    

    【讨论】:

    • 使用上面的代码,我可以一次检索所有文件吗?例如,如果我的 ES 中有 10000 个文档,上面的代码是否会返回所有文档?
    • 这就是滚动 API @ShivkumarMallesappa 背后的想法,所以应该可以。
    猜你喜欢
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 2020-01-20
    • 2016-05-10
    • 1970-01-01
    相关资源
    最近更新 更多