spring data elasticsearch如何使用offset和limit来查询 [英] How spring data elasticsearch use offset and limit to query
本文介绍了spring data elasticsearch如何使用offset和limit来查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
spring data elasticsearch如何使用offset和limit来查询.我想使用偏移量和限制参数来查询页面.但我找不到方法支持.例如:
How spring data elastisearch use offset and limit to query. I want to use offset and limit param to query page.But I can not find methods support. For Example:
queryBuild.withPageable(PageRequest.of(pageIndex, pageSize));
Page<Content> content = elasticsearchOperations.queryForPage(queryBuild.build(),Content.class);
应该没问题
但是我找不到带有偏移和限制的方法
But I can not found method with offset and limit
queryBuild.withPageable(PageRequest.of(offset, limit));
推荐答案
我也有同样的问题,所以实现了下面的类
I have the same problem, so I implemented the following class
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
public class OffsetLimitPageable implements Pageable {
private int offset;
private int page;
private int size;
private Sort sort = Sort.unsorted();
protected OffsetLimitPageable(int offset, int page, int size) {
if (offset < 0) {
throw new IllegalArgumentException("Offset must not be less than zero!");
}
if (page < 0) {
throw new IllegalArgumentException("Page index must not be less than zero!");
}
if (size < 1) {
throw new IllegalArgumentException("Page size must not be less than one!");
}
this.offset = offset;
this.page = page;
this.size = size;
}
public static OffsetLimitPageable of(int offset, int page, int size) {
return new OffsetLimitPageable(offset, page, size);
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#getPageNumber()
*/
@Override
public int getPageNumber() {
return page;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#getPageSize()
*/
@Override
public int getPageSize() {
return size;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#getOffset()
*/
@Override
public long getOffset() {
return offset + page * size;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#getSort()
*/
@Override
public Sort getSort() {
return sort;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#next()
*/
public Pageable next() {
return of(offset, page + 1, size);
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#previousOrFirst()
*/
public Pageable previousOrFirst() {
return hasPrevious() ? of(offset, page - 1, size) : first();
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#first()
*/
public Pageable first() {
return of(offset, 0, size);
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#hasPrevious()
*/
public boolean hasPrevious() {
return page > 0;
}
}
而且,像这样使用
queryBuild.withPageable(OffsetLimitPageable.of(offset, page, limit));
这篇关于spring data elasticsearch如何使用offset和limit来查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文