neo4j - Cypher: How to match relationship-node-relationship within a path -
i new cypher , neo4j (and stack overflow matter), question has easy answer. have spent hours reading documentation , googling, found nothing on point answer question.
apparently need more reputation post images, best text.
i can find path 1 node such:
match path = (a {name:"a"})-[*]-(x {name:"x"}) return extract(r in nodes(path) | r);
which return following 2 paths:
(a)-[:red]->(b)<-[:blue]-(c)-[:red]->(f)<-[:red]-(g)-[:blue]->(h)<-[:red]-(x) (z)-[:red]->(h)<-[:red]-(x)
so far good. there are, of course, lots of other relationships , nodes in database connect these nodes, right path.
i need find 1 node along path has 2 red relationships coming so:
-[:red]->(findme)<-[:red]-
in 2 path examples above, findme = (f) , (h).
note: there lots of matches in database, want 1 in path (should one). also, there many nodes , different relationships in path, or few 3 nodes each connected red relationship.
i tried matching using:
match (a)-[*]-(b)-[:red]->(findme)<-[:red]-(c)-[*]-(x) return findme;
which works long there @ least 5 nodes, won't work 3 nodes.
how can find pattern match within path?
you close! know, *
signifies variable length/depth. can use sign itself, in queries, or can specify range. without specified range, *
means "one or more". if apply last query
match (a)-[*]-(b)-[:red]->(findme)<-[:red]-(c)-[*]-(x) a.name = "a" , x.name = "x" return findme
to path 3 nodes
(a)-[:red]->(findme)<-[:red]-(x)
the 2 relationships in path have been matched non-variable part of query. there no relationships left variable part match. parts (a)-[*]-(b)
, (c)-[*]-(x)
each require "one or more", there 0 relationships left match.
you can change meaning of *
"zero or more" explicitly specifying range. in way 2 variable parts of query match when there no relationships left in path.
match (a)-[*0..]-(b)-[:red]->(findme)<-[:red]-(c)-[*0..]-(x) a.name = "a" , x.name = "x" return findme
this query match paths consisting of "red triple" looking for, paths have relationships extending on 1 or both sides of triple.
worth noting pattern "zero or more" range matching "zero case" have 2 identifiers in pattern, 1 node. node therefore bound both identifiers. identifiers in left hand side of pattern above, when matches three-node path, saying
node
a
, nodeb
connected zero-depth relationship
is silly way of saying
a
,b
same node
you can check out this console has five-node path between (a {name: "a"})
, (x {name: "x"})
, three-node path between (a {name: "a2"})
, (x {name: "x2"})
. same pattern match both of these paths (just change values name
in where
clause). 5 columns both paths, because query returns 5 identifiers. in case of three-node path, first result column identical second, , fourth fifth––because "node a
, node b
connected zero-depth relationship" means "a
, b
same node".
Comments
Post a Comment