1
votes

Last days I am trying to figure something out in Neo4j, but I still didn't succeed. The situation:

enter image description here

You see a blue node (there are many in the database, but this one has a specific ID), the blue node is connected to orange nodes and the orange nodes to green nodes. The green nodes are further connected to other orange nodes.

Now it should be very nice if I can get all the orange nodes that are not directly connected to the blue node (marked with a square).

I can get all the orange nodes from each green node, but I only want the nodes not connected to blue (I also want the nodes directly connected to blue in another collect or other method, but that is not the problem to achieve).

I tried different methods, to give an example of one of the things I tried:

MATCH (b:BLUE{BLUE_ID:'234234'})-[a:relation_type_1]->(node_with_connection_to_blue)<-[c:relation_type_2]-(g:GREEN)-[d:relation_type_2]->(node_with_no_connection_to_blue)<-[e:relation_type_1]-(b:BLUE)
WHERE NOT (node_with_no_connection_to_blue)<-[a:relation_type_1]-(b:BLUE{BLUE_ID:'234234'})

With any help I would be very grateful! Thanks in advance!

Best regards

1

1 Answers

1
votes

There are multiple ways to solve this. Assuming a blue node is not directly connected to a huge number of orange nodes, I'd first build up a collection holding all direct neighbors of blue and in a second step do the traversal to the orange nodes of 2nd degree. Those need to be filtered by the direct neighbors in a WHERE:

MATCH (b:BLUE{BLUE_ID:'234234'})-[:relation_type_1]->(o)
WITH b, collect(o) as directNeighbors
MATCH (b)-[:relation_type_1]->()<-[:relation_type_2]-()-[:relation_type_2]->(orange)
WHERE NOT orange in directNeighbors
RETURN distinct orange