Spring Data Neo4j - Page returning all the contents instead of size


I am using Spring data neo4j (version 5.1.9.Release) and spring-data-commons (2.1.9.Release).

In order to paginate my results, I am using Page
Page<Part> fetchPartsById(@Param("Id") String Id, Pageable pageable);

I call the method fetchPartsById as follows -
Page<Part> results = fetchPartsById("1", PageRequest(0, 5))

I was expecting that as I am passing size of 5, results.getContent() should return a list of only 5 elements. However, the list contains all the 40 elements. This defeats the purpose of pagination.

I also tried using Slice instead of Part
Slice<Part> fetchPartsById(@Param("Id") String Id, Pageable pageable);
Slice<Part> results = fetchPartsById("1", PageRequest(0, 5))

Slice does return only the requested 5 elements. However, the problem with using Slice is that I am not aware how to get the next set of results. I tried using results.nextPageable(). This causes the page number to go to next page however it returns no result the second time around. Looks like the results are only on page number 0.

Does anybody know how to use Pagination with Spring data neo4j.
Note : These results are fed to an UI. Hence, the parameters to the PageRequest.of() are driven by the UI. I want to load initial 5 elements from db and on next request next 5 should be loaded.


We've been using @Query with these two attributes:

@Query(value="cypherQuery", countQuery="cypherQueryReturnCount")
Page<Part> fetchPartsById(@Param("Id") String Id, Pageable pageable);

We use the regular Neo4jRepository and @Neo4jRepository OGM configurations. I've thought about passing in the SKIP and LIMIT Cypher words into the value query to further optimize the query so OGM has to do less work.

Thanks for your reply @mike_blum_neo4j. I have already that on the top of my query in the repository. I just did not show that in the question for brevity. My issue is not that I am not getting results, its that Page is returning all the elements inside content list and is ignoring the size parameter that I am passing.

Apparently findAllById doesn't support paged querying. I have added this Support generated ids in derived finder methods. [DATAGRAPH-1286] · Issue #1848 · spring-projects/spring-data-neo4j · GitHub and will investigate.

Thanks for reporting this, @varun85.jobs. There's sadly a ton of stuff going wrong in SDN.

One last reply:

  • The queries as shown above should work. When supplying both custom query and count query both must include corresponding placehodlers for limit and skip.
  • findAllById(Iterable<ID> ids, PageRequest) feels a bit strange: You have a list of ids and you want to create pages from it. Why not just slicing the ids?
  • Spring Data Commons turns it into a part tree query, that is a query where properties are compared through an operation with a parameter. In that case, "id equals list of ids". This is debatable, but we are dependend on Spring Data Commons here. Cannot change that.
  • You have to use findAllByIdsIn(Iterable<ID>, PageRequest). That will work BUT has a bug with native ids. I'm working on fixing the later.

Thanks for your reponse @michael_simons1. I think you hit the nail on its head. I was passing in the count query but i was not passing in LIMIT and SKIP to the count query. I was just passing it to the actual query.
I got around my issue by using LIMIT and SKIP explicitly as I wanted them in the middle of the query and not at the end as added by default when using PagingAndSorting repository.