英文:
Elasticsearch repository Pagination not returning correct page size and not sorting data
问题
我使用的是Spring 2.2和Spring ElasticSearch 3.2.4。
我有这个方法在我的ElasticSearch中查找所有类别为“Game”或“Apps”的apk,并按安装数量对它们进行排序。
public ResponseList<Apk> findMostDownloadedApkByCategory(String categoryName, int page, int size) {
    
    logger.info("按类别查找最多下载的Apk");
    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;
        // 遍历每个元素
        for (Apk apk : apkList) {
            System.out.println(apk.getInstalls());
            // 如果公司存在,我们使用公司完成dto
            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.getAddress(), company.getCountry());
                // 转换apk为apk dto,并添加公司信息
                apkDto = convertApkToDto(apk, companyDto);
                apkDtoList.add(apkDto);
            }
        }
        responseList = new ResponseList(apkDtoList, apkList.hasNext(), page);
    }
    return responseList;
}
我的Repository:
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.
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;
}
my repository:
Page<Apk> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论