es sql查询分页问题
1.操作版本 7.122.es分页问题总结1.es的sql分页不支持 limit startIndex,endIndex。开始
·
1.操作版本 7.12
2.es分页问题总结
1.es的sql分页不支持 limit startIndex,endIndex。开始行和结束行这种查询
2.es支持:select * from 索引名 limit 1000; 指定行数。
3.分页做法
3.1根据es支持的cursor模块
首次查询
首次查询返回的数据:
根据cursor查询
当返回值不再有cursor字段时,说明到最后一页
![](https://img-blog.csdnimg.cn/20210609103726368.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hpeGl5dWd1YW5n,size_16,color_FFFFFF,t_70)
如果未到最后一页,结束查询,需要关闭cursor,节约资源
3.2代码自定义逻辑分页
用代码将获取到的所有数据进行分页处理
参考如下:
/**
* 获取手动分页
* @param rows 数据列表,由返回的json转换而来
* @param pageNum 当前页码
* @param pageSize 每页条数
* @return
*/
private List getSubList(JSONArray rows, Integer pageNum, Integer pageSize) {
//分页操作
int startIndex = 0;
int endIndex = rows.size();
if (pageNum != null && pageSize != null) {
startIndex = PageBean.getStartIndex(pageNum, pageSize);
endIndex = PageBean.getEndIndex(pageNum, pageSize);
}
List subList;
if (rows.size() >= endIndex) {
subList = rows.subList(startIndex, endIndex);
} else {
subList = rows.subList(startIndex, rows.size());
}
return subList;
}
/**
* 获取起始位置
* @return
*/
public static int getStartIndex(int pageNum,int pageSize){
return 0+(pageNum-1)*pageSize;
}
/**
* 获取结束位置
* @return
*/
public static int getEndIndex(int pageNum,int pageSize){
return pageNum*pageSize;
}
更多推荐
所有评论(0)