Faquarl
Faquarl

Reputation: 158

Cypher path querying (using Neo4j)

I have a graph datebase so that there is in it some pattern like this one:

(n1)-[:a]->(n2),
(n1)-[:b]->(n2),
(n1)-[:c]->(n2),
(n1)-[:e]->(n2),
(n1)-[:d]->(n3),
(n2)-[:b]->(n4)

And I want to have all graph with this pattern

MATCH p={
 (n3)<-[:d]-(n1)-[:a]->(n2)-[:b]->(n4),
 (n1)-[:b]->(n2)<-[:c]-(n1),
 (n1)-[:e]->(n2)
}
RETURN p

Is it possible? I've search a little but I haven't found how to do it. I know we can use "|" for a type like this

 ()-[:a|b]->()

but there is no "&" and the path assigning only works on pattern which are written without ",".

Thanks

EDIT: If it could help, here is another example of what I'm seeking: In a database with movies, person and relations like ACTED_IN, KNOWS, FRIEND and HATE I want all the graphs containing an actor "Actor1" (who ACTED_IN a movie "M") who KNOWS "Person1", FRIEND "Person2" and HATE "Person3" which ACTED_IN the same movie "M".

An UNION like the one in the answer of "Michael Hunger" does not work because we have multiple subgraphs and not graphs. Moreover, some subgraph might not be correct answers for the bigger pattern.

Upvotes: 0

Views: 265

Answers (1)

Michael Hunger
Michael Hunger

Reputation: 41676

Your query will be very inefficient, as you don't restrict your search to a set of start nodes neither with labels or label+property combinations !!!!

You can use UNION for that:

MATCH p=(n3)<-[:d]-(n1)-[:a]->(n2)-[:b]->(n4) RETURN p
UNION
MATCH p=(n1)-[:b]->(n2)<-[:c]-(n1) RETURN p
UNION
MATCH p=(n1)-[:e]->(n2) RETURN p

Upvotes: 1

Related Questions