How to get all the nodes connected from 1 to n without knowing the depth where depth is variable and relation name is same

34 views
Skip to first unread message

Sukaant Chaudhary

unread,
Oct 2, 2015, 9:42:56 AM10/2/15
to ne...@googlegroups.com
Hi,
I've the graph as shown in the image.

My requirement are as follows:

1. We have to search for nodes 1 to n without knowing the depth of the graph as the depth may vary for all the nodes from 1 to n.

2. Here the relation name at the levels will be same.

3. When we search for say "2 1" then it should give the result as "all the parents connected above that node and all the child connected below that node with the relation".

4. The parent and child can be upto any n level and it should give all the parents and child connected to that node directly or indirectly with the relation.

5. Consider here for example:
5.1. If "1 1" is searched then it will give the result "2 1", "2 2", "2 3", "n 1", "n 2", "n 3", "n 4", "n 5" with all the relations from up to down.
5.2. If "2 3" is searched then it will give the result "1 1", "1 2", "n 3", "n 4", "n 5" with all the relations from up to down.
5.3. If "n 4" is searched then it will give the result "1 1", "1 2", "1 3", "2 3", "2 4" with all the relations from up to down.

Inline image 2
Any help will be really appreciated.

-Sukaant Chaudhary

Benoit Simard

unread,
Oct 3, 2015, 9:43:20 AM10/3/15
to ne...@googlegroups.com
Hi,

I think that something like this should works :

MATCH p=(n:Node { name:"34" })-[:CHILD*]->()
UNWIND nodes(p) AS node
WITH node
WHERE node.name <> "34"
RETURN DISTINCT node
UNION
MATCH p=()-[:CHILD*]->(n:Node { name:"34" })
UNWIND nodes(p) AS node
WITH node
WHERE node.name <> "34"
RETURN DISTINCT node

Cheers.
--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Hunger

unread,
Oct 4, 2015, 12:37:44 AM10/4/15
to ne...@googlegroups.com
MATCH path = (:Element {id:"11")-[:CHILD*]->()
RETURN path

or if you only want to return distinct nodes overall you can do

UNWIND nodes(path) as n
RETURN distinct n



<image.png>
Any help will be really appreciated.

-Sukaant Chaudhary
<image004.png>

Sukaant Chaudhary

unread,
Oct 4, 2015, 7:09:54 AM10/4/15
to ne...@googlegroups.com
Thanks Benoit !
Thanks Michael !
It worked for me fine.

There is one more query regarding this, if I'm searching for any node say "4 1" and it is not connected to any other node, in that case it should return node "4 1", now it is returning nothing.

-Sukaant Chaudhary

Reply all
Reply to author
Forward
0 new messages