0
votes

I am trying to figure out how to write a Cypher query for Neo4J. I have a linked list of nodes like this:

n-[FIRST_NODE]->n-[NEXT_NODE]->n-[NEXT_NODE]->.....

The FIRST_NODE relationship has a property that says how deep in the list we should retrieve nodes. I want to retrieve a list of nodes possibly skipping one based on a property in n and retrieve x amount of nodes where x is the depth we should traverse in the list. Does this make sense?

I have come up with the below query but it doesn't work!

MATCH (x)-[firstIssue:FIRST_NODE]->(y:Type1)
MATCH (z)-[:NEXT_NODE*1..{firstIssue.Count}]->(a:Type1)
RETURN x,y,z,a

Any help would be apprectiated!.

1
Check out the APOC Procedures library cybersam suggested first. If that still isn't working for you, you may need to abandon Cypher and instead look to Neo4j's traversal framework in Java.InverseFalcon

1 Answers

1
votes

Cypher does not support dynamic bounds for variable length paths.

However, in neo4j 3.x, you can install the APOC plugin and use the apoc.path.expand procedure. For example:

MATCH (x)-[firstIssue:FIRST_NODE]->(y:Type1)
CALL apoc.path.expand(y, 'NEXT_NODE>', '+Type1', 1, firstIssue.Count) YIELD path
RETURN x, firstIssue, path;