首页 > 解决方案 > Elasticsearch 存储库分页未返回正确的页面大小且未对数据进行排序

问题描述

我使用弹簧 2.2 和弹簧弹性搜索 3.2.4。

我有这种方法可以在我的弹性搜索中找到所有具有“游戏”或“应用程序”类别的 apk,并按安装数量对它们进行排序。

public ResponseList<Apk> findMostDownloadedApkByCategory(String categoryName, int page, int size) {
    
    logger.info("find most downloaded Apk By Category");
    List<ApkDto>apkDtoList = new ArrayList<>();
    ResponseList<Apk> responseList = new ResponseList(apkDtoList, false, page);
    Page<Apk> apkList =  apkRepo.findByCategoryNameIgnoreCaseOrderByInstallsDesc(categoryName, PageRequest.of(page, size));

    if(!apkList.isEmpty()) {

        ApkDto apkDto = null;
        //iterate over each element
        for(Apk apk : apkList) {

            System.out.println(apk.getInstalls());
            //We complete the dto with the company if the company exist
            Optional<Company> companyOpt = companyService.findById(apk.getCompanyId());

            if(companyOpt.isPresent()) {
                Company company = companyOpt.get();
                CompanyDto companyDto = new CompanyDto(apk.getCompanyId(), company.getName(), company.getDescription(), company.getAdress(), company.getCountry());
                //convert the apk to apk dto, add the company
                apkDto = convertApkToDto(apk, companyDto);
                apkDtoList.add(apkDto);
            }

        }
        responseList = new ResponseList(apkDtoList, apkList.hasNext(), page);
    }
    return responseList;
}

我的存储库:

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

在我的弹性搜索中,我有 26 个类别为“游戏”的数据,安装范围为 0 到 56。

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

页 = 1 和大小 = 20

elasticsearch 在内容中仅返回 5 个数据,但在计数 26 个数据中显示

在此处输入图像描述

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

标签: javaspringelasticsearchspring-data-elasticsearch

解决方案


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

要获得前 20 个,您将获得大小为 20 的第 0 页。


推荐阅读