0

I am trying to trace messages in a graph of messages. For example, node A sends message to node B which sends message to node C (and so on), how can I devise a query in Cypher that will keeping calling the next node until a terminal node is reached.

A -> B -> C -> D -> E -> F

start search is A, returns a list containing B,C,D,E,F (in the classic neo4j graph visualisation where these nodes are connected because B sent message to C and so on til F.

The code I have is

MATCH p=(a { address: "A" })-[r]->(b)
RETURN *

This only returns me A and the nodes A sent a message to. How can I modify it to accomplish the recursive call I am seeking.

Note: I have referred to this post and browsed the neo4j manual. However, I still don't get it (either could not find the answer or perhaps I am not 'getting it'). Any help is truly appreciated!

Community
  • 1
  • 1
Roy
  • 277
  • 1
  • 5
  • 17

1 Answers1

1

This call:

MATCH p=(a { address: "A" })-[r*]->(b)
RETURN b;

will match as many hops away from A as you want, because of the asterisk on the relationship. The b variable will end up being everything that's downstream of a.

I'm not sure what you mean by "call the next node". This will just return the data behind that node. You don't actually need recursion to do this at all with cypher and neo4j. Rather, you should just ask cypher for what data you want, and it will get it for you. If you were implementing this stuff on a non-graph database, you might use recursion as part of a depth-first or breadth-first search, but it simply isn't necessary with a graph DB. The query language handles all of that for you.

FrobberOfBits
  • 17,634
  • 4
  • 52
  • 86
  • thanks FrobberOfBits, when I return b, it takes forever, so i limited b to 50. works. But when I limit to 1, i get 'too many neighbors' error, do you know why? – Roy May 08 '15 at 12:40
  • "Too many neighbors" happens in the browser when you try to click and expand a node that has many relationships. If a node has say 5,000 rels, the browser can't display so much on screen so it will say too many neighbors. This query you're running traces to *EVERYTHING* downstream of "a", so that could be a lot of stuff if your graph is big, that's why it's taking a long time. You might want to limit how many hops away you're jumping, or place some restrictions on what b can be. – FrobberOfBits May 08 '15 at 14:42
  • yes i understand that so i did the ... RETURN b limit 1 - but this is what causes the error of 'too many neighbors'. Return b limit 10 does not, so how can i modify my query to just know the immediate neighbors of node a? – Roy May 08 '15 at 16:51
  • "Too many neighbors" is not a cypher error, it's a browser error. Try returning a node property instead of the node itself. That will cause the browser to return data, and not the full node. When it returns the full node, it tries to graph it. When there are too many things to graph in one display, you get "too many neighbors" – FrobberOfBits May 08 '15 at 17:04