Dean
Dean

Reputation: 116

@Query not returning all data in Spring boot with Spring data neo4j

I have the query below in a Spring boot application using Spring data neo4j. When I run the query as a standalone cypher query, it returns all the data that I am looking for, however, when I run the same query in the spring boot application, it does not return all the data I need. It just goes about a level or 2 deep.

 MATCH path = (stepNode:StepNode {stepNodeId: "<id_goes_here>"})-[:PARENT*0..]-(descendant)
   WITH collect(path) AS paths, stepNode, collect(distinct descendant) AS descendants
   UNWIND descendants AS d
   OPTIONAL MATCH (d)-[r]->(related)
   RETURN stepNode, paths, collect(distinct r) AS relationships, collect(distinct related) AS relatedNodes;

Is there anything I am missing?

This is the query in the spring boot repository

 @Query("""
        MATCH path = (stepNode:StepNode {stepNodeId: $rootNodeId})-[:PARENT*0..]-(descendant)
        WITH collect(path) AS paths, stepNode, collect(distinct descendant) AS descendants
        UNWIND descendants AS d
        OPTIONAL MATCH (d)-[r]->(related)
        RETURN stepNode, paths, collect(distinct r) AS relationships, collect(distinct related) AS relatedNodes;
     """)
    Optional<StepNode> findTreeByRootNodeId(UUID rootNodeId);

I have also noticed that in my logs, right before my query is executed, spring data neo4j displays some data that matches the element_ids of the data that I am able to retrieve from a related entity. Below is the data that is pulled before my query runs with the query

2024-06-22T13:57:45.591Z DEBUG 752174 --- [Game Engine] [nio-9000-exec-1] org.springframework.data.neo4j.cypher    : Executing:
MATCH (rootNodeIds:`Product`) WHERE elementId(rootNodeIds) IN $rootNodeIds WITH collect(rootNodeIds) AS n OPTIONAL MATCH ()-[relationshipIds]-() WHERE elementId(relationshipIds) IN $relationshipIds WITH n, collect(DISTINCT relationshipIds) AS __sr__ OPTIONAL MATCH (relatedNodeIds) WHERE elementId(relatedNodeIds) IN $relatedNodeIds WITH n, __sr__ AS __sr__, collect(DISTINCT relatedNodeIds) AS __srn__ UNWIND n AS rootNodeIds WITH rootNodeIds AS product, __sr__, __srn__ RETURN product AS __sn__, __sr__, __srn__
2024-06-22T13:57:45.591Z TRACE 752174 --- [Game Engine] [nio-9000-exec-1] org.springframework.data.neo4j.cypher    : with parameters:
:param rootNodeIds => ['4:e980bb06-70fe-4527-9e8e-20567526a367:547']
:param relatedNodeIds => ['4:e980bb06-70fe-4527-9e8e-20567526a367:551', '4:e980bb06-70fe-4527-9e8e-20567526a367:557', '4:e980bb06-70fe-4527-9e8e-20567526a367:555', '4:e980bb06-70fe-4527-9e8e-20567526a367:553', '4:e980bb06-70fe-4527-9e8e-20567526a367:556', '4:e980bb06-70fe-4527-9e8e-20567526a367:554', '4:e980bb06-70fe-4527-9e8e-20567526a367:552', '4:e980bb06-70fe-4527-9e8e-20567526a367:561', '4:e980bb06-70fe-4527-9e8e-20567526a367:559', '4:e980bb06-70fe-4527-9e8e-20567526a367:556', '4:e980bb06-70fe-4527-9e8e-20567526a367:554', '4:e980bb06-70fe-4527-9e8e-20567526a367:553', '4:e980bb06-70fe-4527-9e8e-20567526a367:557', '4:e980bb06-70fe-4527-9e8e-20567526a367:555', '4:e980bb06-70fe-4527-9e8e-20567526a367:553', '4:e980bb06-70fe-4527-9e8e-20567526a367:556', '4:e980bb06-70fe-4527-9e8e-20567526a367:554', '4:e980bb06-70fe-4527-9e8e-20567526a367:552', '4:e980bb06-70fe-4527-9e8e-20567526a367:551', '4:e980bb06-70fe-4527-9e8e-20567526a367:556', '4:e980bb06-70fe-4527-9e8e-20567526a367:554', '4:e980bb06-70fe-4527-9e8e-20567526a367:552', '4:e980bb06-70fe-4527-9e8e-20567526a367:556', '4:e980bb06-70fe-4527-9e8e-20567526a367:554', '4:e980bb06-70fe-4527-9e8e-20567526a367:551', '4:e980bb06-70fe-4527-9e8e-20567526a367:561', '4:e980bb06-70fe-4527-9e8e-20567526a367:559']
:param relationshipIds => ['5:e980bb06-70fe-4527-9e8e-20567526a367:96', '5:e980bb06-70fe-4527-9e8e-20567526a367:291', '5:e980bb06-70fe-4527-9e8e-20567526a367:342', '5:e980bb06-70fe-4527-9e8e-20567526a367:287', '5:e980bb06-70fe-4527-9e8e-20567526a367:275', '5:e980bb06-70fe-4527-9e8e-20567526a367:341', '5:e980bb06-70fe-4527-9e8e-20567526a367:274', '5:e980bb06-70fe-4527-9e8e-20567526a367:345', '5:e980bb06-70fe-4527-9e8e-20567526a367:344', '5:e980bb06-70fe-4527-9e8e-20567526a367:289', '5:e980bb06-70fe-4527-9e8e-20567526a367:343', '5:e980bb06-70fe-4527-9e8e-20567526a367:306', '5:e980bb06-70fe-4527-9e8e-20567526a367:305', '5:e980bb06-70fe-4527-9e8e-20567526a367:326']
2024-06-22T13:57:45.653Z DEBUG 752174 --- [Game Engine] [nio-9000-exec-1] o.s.d.n.c.t.Neo4jTransactionManager      : Initiating transaction commit
2024-06-22T13:57:45.662Z  INFO 752174 --- [Game Engine] [nio-9000-exec-1] c.l.d.g.c.game.TemplateController        : [8A26299413CCEF2FB715D1D610E91EF6] product found, about to check for root node
2024-06-22T13:57:45.662Z DEBUG 752174 --- [Game Engine] [nio-9000-exec-1] o.s.d.n.c.t.Neo4jTransactionManager      : Creating new transaction with name [com.lucidarray.d_and_c.game_engine.services.game.StepNodeService.findTreeById]: PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT; 'transactionManager'
2024-06-22T13:57:45.674Z DEBUG 752174 --- [Game Engine] [nio-9000-exec-1] o.s.d.n.c.t.Neo4jTransactionManager      : Participating in existing transaction
2024-06-22T13:57:45.674Z DEBUG 752174 --- [Game Engine] [nio-9000-exec-1] org.springframework.data.neo4j.cypher    : Executing:
   MATCH path = (stepNode:StepNode {stepNodeId: "421d419a-05a5-4cd6-81f5-3fe5336eead0"})-[:PARENT*0..]-(descendant)
   WITH collect(path) AS paths, stepNode, collect(distinct descendant) AS descendants
   UNWIND descendants AS d
   OPTIONAL MATCH (d)-[r]->(related)
   RETURN stepNode, paths, collect(distinct r) AS relationships, collect(distinct related) AS relatedNodes;

2024-06-22T13:57:45.675Z TRACE 752174 --- [Game Engine] [nio-9000-exec-1] org.springframework.data.neo4j.cypher    : with parameters:
:param 0 => "421d419a-05a5-4cd6-81f5-3fe5336eead0"
:param rootNodeId => "421d419a-05a5-4cd6-81f5-3fe5336eead0"
2024-06-22T13:57:45.705Z DEBUG 752174 --- [Game Engine] [nio-9000-exec-1] o.s.d.n.c.t.Neo4jTransactionManager      : Initiating transaction commit

The data that I get when I run the query directly in cypher (I used the table because it might be easier to compare than the graph)

enter image description here

I am using spring boot 3.2.5 and the spring-boot-starter-data-neo4j compatible with it

PS: I would also like to note that when I do a findById (which should in theory fetch as much as it can), I do not get the full tree either. It was partly why I had to write this query to begin with

Any help would be appreciated

Upvotes: 0

Views: 80

Answers (1)

cybersam
cybersam

Reputation: 67044

Your @Query completely ignores the $rootNodeId parameter passed to findTreeByRootNodeId().

If $rootNodeId is supposed to be used as the stepNodeId value, try this:

@Query("""
   MATCH path = (stepNode:StepNode {stepNodeId: $rootNodeId})-[:PARENT*0..]-(descendant)
   WITH collect(path) AS paths, stepNode, collect(distinct descendant) AS descendants
   UNWIND descendants AS d
   OPTIONAL MATCH (d)-[r]->(related)
   RETURN stepNode, paths, collect(distinct r) AS relationships, collect(distinct related) AS relatedNodes;
""")
Optional<StepNode> findTreeByRootNodeId(UUID rootNodeId);

Upvotes: 0

Related Questions