Krid
Krid

Reputation: 309

Find all Nodes and Relations up to a defined path length within Neo4j using Cypher

I'm stuck with a problem returning all nodes and relationships within the following pattern:

(a:LABEL_A)-[:REL_A]->(x:LABEL_B)-[r:REL_B*1..n]->(y:LABEL_B)<-(b:LABEL_A)
WHERE a.prop IN [Array of Strings] AND b.prop IN [Array of Strings]

The "Array of Strings" at both ends of the path is the same. The result I'm looking for is a list of all the paths in-between a group of nodes up to a defined depth with all the properties of the "in-between" nodes and the properties of the "in-between" relationships

The result I'm struggling to achieve is shown in the following structure:

a.prop, x.prop, r.prop, y.prop, b.prop : in the case the path length is 1

up to...

a.prop, x.prop, r(1).prop, in-between_node(1).prop, r(2).prop, in-between_node(2).prop, ..., y.prop, b.prop : in the case the path length is n

Is there an easy way to create this as a simple list output?

Any tip greatly appreciated

Krid

Upvotes: 0

Views: 91

Answers (1)

cybersam
cybersam

Reputation: 66967

It sounds like you just want to get the paths that match your pattern. Each path contains all the nodes and relationships (and their properties), in path order. For example, this query:

WITH ['b','c'] AS arrayOfStrings
MATCH path=(a:LABEL_A)-[:REL_A]->(x:LABEL_B)-[r:REL_B*1..5]->(y:LABEL_B)<--(b:LABEL_A)
WHERE a.prop IN arrayOfStrings AND b.prop IN arrayOfStrings
RETURN path;

will return a result such as this (where only one path was found):

+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| path                                                                                                                                                               |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| (:LABEL_A {prop: "c"})-[:REL_A {foo: "bar"}]->(:LABEL_B {foo: "baz"})-[:REL_B {fie: "foe"}]->(:LABEL_B {foo: "bax"})<-[:REL_A {foo: "fop"}]-(:LABEL_A {prop: "b"}) |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Upvotes: 1

Related Questions