弹性搜索存储库的分页未返回正确的页面大小和未对数据进行排序。

huangapple go评论100阅读模式
英文:

Elasticsearch repository Pagination not returning correct page size and not sorting data

问题

我使用的是Spring 2.2和Spring ElasticSearch 3.2.4。

我有这个方法在我的ElasticSearch中查找所有类别为“Game”或“Apps”的apk,并按安装数量对它们进行排序。

  1. public ResponseList<Apk> findMostDownloadedApkByCategory(String categoryName, int page, int size) {
  2. logger.info("按类别查找最多下载的Apk");
  3. List<ApkDto> apkDtoList = new ArrayList<>();
  4. ResponseList<Apk> responseList = new ResponseList(apkDtoList, false, page);
  5. Page<Apk> apkList = apkRepo.findByCategoryNameIgnoreCaseOrderByInstallsDesc(categoryName, PageRequest.of(page, size));
  6. if (!apkList.isEmpty()) {
  7. ApkDto apkDto = null;
  8. // 遍历每个元素
  9. for (Apk apk : apkList) {
  10. System.out.println(apk.getInstalls());
  11. // 如果公司存在,我们使用公司完成dto
  12. Optional<Company> companyOpt = companyService.findById(apk.getCompanyId());
  13. if (companyOpt.isPresent()) {
  14. Company company = companyOpt.get();
  15. CompanyDto companyDto = new CompanyDto(apk.getCompanyId(), company.getName(), company.getDescription(), company.getAddress(), company.getCountry());
  16. // 转换apk为apk dto,并添加公司信息
  17. apkDto = convertApkToDto(apk, companyDto);
  18. apkDtoList.add(apkDto);
  19. }
  20. }
  21. responseList = new ResponseList(apkDtoList, apkList.hasNext(), page);
  22. }
  23. return responseList;
  24. }

我的Repository:

  1. Page<Apk> findByCategoryNameIgnoreCaseOrderByInstallsDesc(String categoryName, Pageable pageable);

在我的ElasticSearch中,有26条数据的类别是“Game”,安装量从0到56不等。

当我调用我的方法,例如:

page = 1,size = 20

ElasticSearch只返回了5条数据在内容中,但在计数中显示了26条数据。

数据未按安装数量排序。我真的不知道如何纠正这个行为。提前感谢。

英文:

I use spring 2.2 and spring elastic search 3.2.4.

I have this method to find in my elastic search all apk with category "Game" or "Apps" and sort them by the number of install.

  1. public ResponseList&lt;Apk&gt; findMostDownloadedApkByCategory(String categoryName, int page, int size) {
  2. logger.info(&quot;find most downloaded Apk By Category&quot;);
  3. List&lt;ApkDto&gt;apkDtoList = new ArrayList&lt;&gt;();
  4. ResponseList&lt;Apk&gt; responseList = new ResponseList(apkDtoList, false, page);
  5. Page&lt;Apk&gt; apkList = apkRepo.findByCategoryNameIgnoreCaseOrderByInstallsDesc(categoryName, PageRequest.of(page, size));
  6. if(!apkList.isEmpty()) {
  7. ApkDto apkDto = null;
  8. //iterate over each element
  9. for(Apk apk : apkList) {
  10. System.out.println(apk.getInstalls());
  11. //We complete the dto with the company if the company exist
  12. Optional&lt;Company&gt; companyOpt = companyService.findById(apk.getCompanyId());
  13. if(companyOpt.isPresent()) {
  14. Company company = companyOpt.get();
  15. CompanyDto companyDto = new CompanyDto(apk.getCompanyId(), company.getName(), company.getDescription(), company.getAdress(), company.getCountry());
  16. //convert the apk to apk dto, add the company
  17. apkDto = convertApkToDto(apk, companyDto);
  18. apkDtoList.add(apkDto);
  19. }
  20. }
  21. responseList = new ResponseList(apkDtoList, apkList.hasNext(), page);
  22. }
  23. return responseList;
  24. }

my repository:

  1. Page&lt;Apk&gt; findByCategoryNameIgnoreCaseOrderByInstallsDesc(String categoryName, Pageable pageable);

In my elastic search I have 26 data with catefory "Game" and installs from 0 to 56.

When I call my method for example with

> page = 1 and size = 20

elasticsearch returns just 5 data in content but shows in the count 26 data

弹性搜索存储库的分页未返回正确的页面大小和未对数据进行排序。

Data are not sorted by installs. I really don't know how to do to correct this behaviour.
Thanks in advance.

答案1

得分: 1

我在您的截图中看到apkList.content列表中有6个元素。由于这是第1页,页面大小为20,总共有26个元素,所以是正确的。

要获取前20个元素,您需要获取页面编号为0,大小为20的页面。

英文:

I see 6 elements in the apkList.content list in your screenshot. As as this is page 1 with a page size of 20, and total number of 26 elements, this is correct.

To get the first 20, you would get page 0 with size 20.

huangapple
  • 本文由 发表于 2020年8月15日 23:54:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63428027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定