Andrew
Andrew

Reputation: 23

How to query all chains which contain some node?

I have this type of graph:

(a)->[r1]->(b)-[r1]->(c)->[r1]->(d)... .Also every node may have additional one or several relationships r2, for example:

(b)->[r2]->(G)

(b)->[r2]->(K) and so on, or:

(c)->[r2]->(G)

(c)->[r2]->(K).

If I use query like this:

MATCH (n1)-[r]->(n2)-[r1]->(n3)-[r2]->(n4)-[r3]->(n5)-[r4]->(n6)
WHERE n1.property = "`DEF9747D6`"
RETURN r,r1,r2,r3,r4,n1,n2,n3,n4,n5,n6 LIMIT 10

It returns only specified node and relationship from n1 to n6, but the problem that I do not know before head how many nodes a chain contains.

So I need a query, which may return all chains and relationships even if I specify a property node in the middle. For example:

I specify some (c.Property) and query must return all chains form (a) and to end and all relationships r1 and r2.

Can you please help me with this query?

Upvotes: 2

Views: 213

Answers (1)

Bruno Peres
Bruno Peres

Reputation: 16365

The below query should work:

MATCH p = (n1)-[:n1|:n2*]->()
WHERE n1.property = "`DEF9747D6`"
RETURN relationships(p), nodes(p)
LIMIT 10

This query uses a variable-length pattern matching to MATCH all patterns starting from a node with n1.property = "DEF9747D6" transversing across relationships of type :n1 and :n2. The functions relationships() and nodes() are used to return all relationships and nodes from each path.

Tip: when working with relationship types, remember to add : before the name of the type. That is: use :n1 instead of n1.

Upvotes: 0

Related Questions