Search code examples
neo4jcypherspring-data-neo4j-4

Page results from query coming back in random order


A quick question on sort order using spring-data-neo4j

I'm using the following query to get a 'category path'.

@Query("MATCH (c1:Category {sceneId: {categoryId}})-[r:PARENT*]->(c2:Category) RETURN c1, r, c2")
    Page<Category> getCategoryPath(@Param("categoryId") String categoryId, Pageable pageable);

In our system, categories are structured as a tree, and a single category path is the path from a leaf category to the root category.

For example, the following is a category path:

(c1:Category)<-[:PARENT]-(c2:Category)<-[:PARENT]-(c3:Category)

The issue I am having with this is that the Categories are returned in the Page object out of order. I want the categories in order of leaf to parent, or parent to leaf (doesn't matter which), not random order.

Any tips?


Solution

  • Without an ORDER BY, there's no guarantee about page order. Not sure if there's a good way for you to add an order by for your use case. You may want a custom query result here but I can't guarantee that it'll fix your use case till we play around with this data some more