Running APOC subGraphAll() in Spring Data Neo4j

36 Views Asked by At

I am using Spring Data Neo4j, and trying to execute the following code:

    @Query("""
         MATCH (orders:Order {id:1})
         CALL apoc.path.subgraphAll(orders, {})
         YIELD nodes, relationships
         RETURN  nodes, relationships
        """)
List<Order> getOrders(@Param("orderId") Long orderId);

However, it returns only one Order with null fields instead of two with all fields populated.

Before, I was using the following code:

      @Query("""
         MATCH path = (orders:Order {id: $orderId})-[*]-(commons)
         RETURN path
        """)
List<Order> getOrders(@Param("orderId") Long orderId);

However, it becomes prolonged after adding 10-20 nodes... So I either need to improve this query or somehow make Spring work with APOC procedures.

I really appreciate it if you could helm me on this matter.

Thank you in advance!

1

There are 1 best solutions below

0
cybersam On

Since your original query returns a path, you can try using apoc.path.expand instead of apoc.path.subgraphAll:

@Query("""
   MATCH (ord:Order {id: $orderId})
   CALL apoc.path.expand(ord, null, null, 0, -1) YIELD path
   RETURN path
""")
List<Order> getOrders(@Param("orderId") Long orderId);

NOTE: The above specifies a lower bound of 0, so a path containing just ord would be returned if that node has no relationships, which I think is maybe what you intended. But this behavior is different than your original query's, which used a (defaulted) lower bound of 1 and would return nothing if ord has no relationships. If the latter is what you intended, change my 0 to 1.

Alternatively, if acceptable, a simple workaround would be to limit the length of the variable-length path search. For example, to limit the search to at most 10 hops, you could replace [*] with [*..10] (or, to use 0 lower bound: [*0..10]) in your original query. With my query, use 10 instead of -1.