How can I do paging and sorting using spring data with Couchbase

2.1k views Asked by At

I am trying to do paging and sorting using spring-data-couchbase but it seems org.springframework.data.couchbase.repository has only CouchbaseRepository which is extending CrudRepository<T,ID>.

There is no interface extending from PagingAndSortingRepository<T,ID>.

http://docs.spring.io/spring-data/couchbase/docs/1.0.x/api/org/springframework/data/couchbase/repository/CouchbaseRepository.html

I did implement an unconventional solution to the problem setting skip and limit. But want to get total number of page count, which is not available with this solution. It seems Page<T> does have .getTotalPages()

4

There are 4 answers

0
Laurent Doguin On

there is currently no paging support with Spring Data Couchbase, you need to use the lower level API, which is basically the first version of Couchbase's Java SDK.

0
ASH On

4 Years on, sadly still no ReactiveCouchbasePagingAndSortingRepository. Instead I will use indexes, and a combination of the offset parameter and the count select to solve this problem. https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/aggregatefun.html#countn https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/offset.html

@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} ORDER BY name LIMIT $1 OFFSET $2;")
Flux<Location> findPage(int limit, int offset);

If they do add it, it should appear here (or in a later version)

https://docs.spring.io/spring-data/couchbase/docs/4.0.x/api/index.html?org/springframework/data/couchbase/repository/package-tree.html

0
Vijay Kesanupalli On

Below are the steps to get the Pagination working.

  1. update spring-data-couchbase version to 4.3.1 or above
  2. Use extends CouchbaseRepository<Object, Object>
  3. Build Pagebale Object.
  4. Page<Object> findAllById(String id, Pageable page)

That's it

0
RICH.LEE On

Since Spring-Data-Couchbase 2.0.x support PagingAndSortingRepository to paging and sorting data.

The interface define:

public interface PagingAndSortingRepository<T, ID extends Serializable>
  extends CrudRepository<T, ID> {

  Iterable<T> findAll(Sort sort);

  Page<T> findAll(Pageable pageable);
}

Paging Project example:

public interface ProjectRepository extends PagingAndSortingRepository<Project, String> {

    Page<Project> findByName(String name, Pageable pageable);
}