gzg
gzg

Reputation: 1489

How to get nodes from relationships with any depth including relation filter

I'm using a query similar to this one:

(n)-[*]->(m)
Any depth.

But I cannot filter the relation name in such a query like this:

(n)-[*:DOES]->(m)
Any depth.

I need to filter the relation name since there are different relations on the related path. If it helps, here is my graph:

CREATE (Computer { name:'Computer' }),(Programming { name:'Programming' }),(Java { name:'Java' }),(GUI { name:'GUI' }),(Button { name:'Button' }), Computer<-[:IS]-Programming, Programming<-[:IS]-Java, Java<-[:IS]-GUI, GUI<-[:IS]-Button, (Ekin { name:'Ekin' }), (Gunes { name:'Gunes' }), (Ilker {name:'Ilker'}), Ekin-[:DOES]->Programming, Ilker-[:DOES]->Java, Ilker-[:DOES]->Button, Gunes-[:DOES]->Java

I'd like to get the names (Ekin, Ilker and Gunes) which have "DOES" relationship connected to "Programming" with any depth.

Edit:

I'm able to get the values I want by merging two different queries' results (think 13 is the top node that I want to reach):

START n=node(13) 
MATCH p-[:DOES]->()-[*]->(n) 
RETURN DISTINCT p

START n=node(13) 
MATCH p-[:DOES]->(n) 
RETURN DISTINCT p

I want to do it in a single query.

Upvotes: 1

Views: 1401

Answers (2)

JavaNoScript
JavaNoScript

Reputation: 2413

Just to update the answer with Neo4j 3.0.

MATCH p-[:DOES*0..]->(n)
RETURN DISTINCT(p.name)

It returns the same result as the accepted answer.

Upvotes: 2

Lisa Li
Lisa Li

Reputation: 2592

Change the matching pattern to "p-[:DOES]->()-[*0..]->n",

Match p-[:DOES]->()-[*0..]->n
Return distinct p.name

The variable length relationship "[*]" means 1..*. You need 0..* length relationships on the path.

Upvotes: 2

Related Questions