7
votes

My neo4j graph shows like below..
300 is connected to 100 and 100 is connected to 201 and 400.
I want to find out the 2nd level of connectivity in neo4j. For example: the 2nd level of node for 300 should return me 201 and 400
Is it possible in neo4j?

enter image description here

enter image description here

when I use below cypher query:

MATCH (n)-[:CALLED*2]-(result) WHERE n.name = '300' RETURN result
It should give me only 201 and 400 ,but it return like below

enter image description here

1

1 Answers

7
votes

This is very easy in Neo4j. If you're using Cypher it might be something like:

MATCH (n)-[:CALLED]-()-[:CALLED]-(result)
WHERE n.id = 300
RETURN result

I'm assuming here that the id property is what is holding the identifying numbers, but obviously you can change that.

You can even do variable length paths like this:

MATCH (n)-[:CALLED*2]-(result)
WHERE n.id = 300
RETURN result

Part of the problem here, though, is that this will return node #200. One query that would fit the result your looking for is:

MATCH (n)-[:CALLED]->()<-[:CALLED]-(result)
WHERE n.id = 300
RETURN result

This matches only where the middle node has the relationships pointing to it.