1

I'm trying use Cypher to get the entire graph that exists if I start at a given node in neo4j. When I say entire graph I mean all nodes and relationships that are connected to at least one other node in the graph.

I've seen examples where people can get all nodes that might be connected to a given start node with a known relationship. Examples of this include this and this, but how could I do this if I do not know the relationships?

Ultimately I'd like every node and relationship where I start at one given node and sprawl out, listing the nodes that are linked by every relationship.

I've tried this:

START n=node(441007) MATHC (n)-[:*]->(d) RETURN d

but the syntax is incorrect. I'm unsure if you can submit a wildcard relationship. Additionaly I do not think this will give me what I am looking for.

Community
  • 1
  • 1
WildBill
  • 9,143
  • 15
  • 63
  • 87

2 Answers2

1

Try this:

MATCH (n)-[r*]->(d)
WHERE ID(n) = 441007
RETURN r, d

This will fan out from n (if using an older version of Neo, you should revert to your START syntax) and return you the paths to each d Node that can be reached. It is relationship type agnostic through not defining the relationship label. If you didn't care about the path you could omit itwith:

MATCH (n)-[*]->(d)
WHERE ID(n) = 441007
RETURN d

Obviously on a large graph this will get expensive!

Edit
Meant to add the link to the cheat sheet, check out the section called Patterns.

JohnMark13
  • 3,709
  • 1
  • 15
  • 26
  • Is there a way to do this in an non-expensive way. Not to sound naive or cynical, but as neo4j is a graph database, shouldn't a breadth-first search be an EASY thing to do? I found this example (http://stackoverflow.com/questions/23399824/neo4j-breadth-first-search-is-too-much-slow) and am looking through it now and hoping I find a way that is not expensive... – WildBill Nov 06 '14 at 06:46
  • Actually reviewing my question I guess I do want an exhaustive search of the graph, but shouldn't this be a natural function/method of a graph database? – WildBill Nov 06 '14 at 06:48
0

Hej WildBill,

i have created a Company Graph for learning Neo4J, so i send the following Pattern against the Graph a got this result:

START a=node(9) 
MATCH (a)<-[rel]-(d) 
MATCH (d)-[sk]->(skill) 
RETURN a, d, skill

Result of the query

Node 9 is my Company, which is part of the Graph.

Patrick
  • 4,532
  • 2
  • 26
  • 32
  • I see you have two match statements which return related nodes. Will this only return the nodes that are of most 2 degree away from node 9? – WildBill Sep 07 '14 at 12:49
  • I started at Company which has an incoming Relationship [:WORKS_FOR] and i'll come to all my Employees, then i wanted to show also all Skills my Employees have, therefore the other MATCH Statement was. – Patrick Sep 07 '14 at 13:35
  • But this still requires a priori knowledge of the relationships at hand, correct? How could I get such nodes that are N-degree away from a starting node without knowing what relationships might exist? – WildBill Sep 07 '14 at 18:01
  • I can give an example of my graph but I want an answer that is independent of ANY graph. That is, how to do a breadth-first search for N levels for ANy graph.... – WildBill Nov 06 '14 at 06:46