Elasticsearch存储库分页未返回正确的页面大小并且未对数据进行排序

问题描述

我使用spring 2.2和spring弹性搜索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个数据

enter image description here

数据未按安装排序。我真的不知道该如何纠正这种行为。 预先感谢。

解决方法

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

要获得前20个,您将获得第20页,尺寸为20。