java - 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 个数据中显示
数据不按安装排序。我真的不知道如何纠正这种行为。提前致谢。
解决方案
apkList.content
我在您的屏幕截图中看到列表中有 6 个元素。因为这是第 1 页,页面大小为 20,总共有 26 个元素,所以这是正确的。
要获得前 20 个,您将获得大小为 20 的第 0 页。
推荐阅读
- android - 泄漏的窗口,但希望在用户单击肯定按钮时完成活动
- f# - 如何为 Saturn 应用程序构建集成测试
- javascript - 试图从 API 中列出用户的最爱,得到“TypeError: null is not an object (evalating 'firebase.auth().currentUser.uid')
- node.js - 我在 Heroku 上的 Node.js 应用程序没有显示在浏览器 Android 手机中
- python - 如何在 Opencv 图像上剪切选区
- android - 错误:功能必须是有效的 BrowserStack App-Automate App URL
- javascript - 使用事件侦听器检测按钮的点击,然后增加 html 元素的计数
- java - JPA如何从ManyToMany关系中的对象的Arraylist中获取属性
- c# - 如何在 EmguCV 中保存具有透明背景的图像
- node.js - 如何从 Node 的 .env 文件中引用环境变量?