简体   繁体   English

Spring数据ElasticSearch扫描和滚动分页结果不起作用?

[英]Spring data elasticsearch scan and scroll paged result is not working?

I have problems using scan and scroll as the pages from scan return strange result. 我在使用扫描和滚动时遇到问题,因为扫描中的页面返回奇怪的结果。 I am not using them exactly as stated in the documentation . 我没有完全按照文档中的说明使用它们。 In the method getAllExampleItems() I have a while loop on the "page.hasNext()" method, which always return false. 在方法getAllExampleItems()中,我在“ page.hasNext()”方法上有一个while循环,该循环始终返回false。 The reason for that is the Pageable from the search query is not set on the result page, and the number of pages therefore is always 1. Another thing that is weird is that I set 100 as the result size and get 500 back! 原因是未在结果页面上设置搜索查询中的Pageable,因此页面数始终为1。另一件事很奇怪,我将结果大小设置为100,然后又返回500! Isn't that a bug or what do I do wrong? 那不是错误,还是我做错了什么? This is an example service: 这是一个示例服务:

package service;

import config.ElasticSearchConfig;
import items.ExampleItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import static org.elasticsearch.index.query.FilterBuilders.typeFilter;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;

@Service
public class ExampleService {

@Autowired
private ElasticsearchTemplate searchTemplate;


public List<ExampleItem> getAllExampleItems() {
    List<ExampleItem> allItems = new ArrayList<>();
    String scrollId = searchTemplate.scan(exampleItemSearchQuery(), 1000, false);
    Page<ExampleItem> page = searchTemplate.scroll(scrollId, 5000L, ExampleItem.class);
    if (page != null && page.hasContent()) {
        allItems.addAll(page.getContent());
        while (page != null && page.hasNext()) {
            page = searchTemplate.scroll(scrollId, 5000L, ExampleItem.class);
            if (page != null && page.hasContent()) {
                allItems.addAll(page.getContent());
            }
        }
    }
    return allItems;
}


private SearchQuery exampleItemSearchQuery() {
    return new NativeSearchQueryBuilder()
            .withQuery(matchAllQuery())
            .withFilter(typeFilter("exampleitem"))
            .withPageable(new PageRequest(0, 100))
            .build();
}


public void saveAllExampleItems(List<ExampleItem> items) {
         List<IndexQuery> indexQueries = new ArrayList<>();
         for (ExampleItem item : items) {
             IndexQuery qry = new IndexQuery();
             qry.setId(item.getId());
             qry.setObject(item);
             qry.setIndexName(ElasticSearchConfig.ITEMS);
             indexQueries.add(qry);
         }
         searchTemplate.bulkIndex(indexQueries);
         searchTemplate.refresh(ExampleItem.class, true);
     }
}

Could somebody explain to me why this does not work, or what I am doing wrong? 有人可以向我解释为什么这不起作用,或者我做错了什么吗?

Working example of scan and scroll with Spring data es can be found at TemplateTest Page 可以在TemplateTest页面上找到使用Spring数据进行扫描和滚动的工作示例

Reason you are getting 500 instead of 100 is because searchHit will be always 之所以得到500而不是100的原因是因为searchHit将始终

size * number_of_primary_shard in elasticsearch. 大小* elasticsearch中的number_of_primary_shard。

ref : es site ref: es网站

I faced the same problem. 我遇到了同样的问题。 Page must be not null and hasNext() is always true. 页面不能为null并且hasNext()始终为true。 hasContent() can assert the page. hasContent()可以声明页面。 Replace hasNext() with hasContent() and try again.... hasNext()替换为hasContent()然后重试。

I faced the same issue, then I figured that the difference between my code, and the "working example", is that I used page.hasNext() , just like you did above. 我遇到了同样的问题,然后我发现我的代码和“工作示例”之间的区别是,我使用了page.hasNext() ,就像上面的操作一样。 Remove that and try again. 删除它,然后重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM