How to find nodes that have one type of connection to a node that is not connected to another node that it is connected to?

When I make this query I get the attached screenshot in my database:

MATCH Nodes=()-[O:orthologous]-()-[G:is_on]-()-[H:homologous]->()

So there are 3 types of connections, all the orange and blue nodes in the big cluster on top have the is_on connection to the 2 nodes that are at the center for each color, and those 2 center nodes have the connection homologous to each other. How can I return only those orange nodes at the very bottom or left side of the screenshot which have the orthologous connection to the blue nodes which appear just next to them, such orange nodes that have the is_on connection to another orange node which doesn't appear here, (which does not have the homologous connection to the blue node at the center of the cluster on top?)

I am thankful for any suggestion even if that might not give me the direct answer.

It's a little tough to follow exactly what you want, and it would help if the orange and blue nodes had certain labels that you could share, that we could use in the query. That may aid our ability to better understand what you're trying to do, and to make the query more efficient. It's nearly always a good idea to use labels in your query, as that provides starting places in the graph to look (instead of needing to look at all nodes in the graph). That said, in Neo4j 4.3.x, if a relationship index is present, it may be able to start with a relationship scan for one of the relationships in the pattern. You can use an EXPLAIN of the query to determine how it is being planned. You do NOT want to see an AllNodesScan, as that is the slowest means of finding anchors in the graph to start from.

We can use a WHERE NOT clause here to exclude pattern conditions.

I couldn't tell if you wanted to exclude the pattern of the :is_on relationships to the exact nodes on the path, or any node that is :homologous to another node, but here's my attempt based on my guess of what you're after.

MATCH (anotherOrange)-[:is_on]-(orangeNode)-[:orthologous]-(blueNode)-[:is_on]-(centerNode)-[:homologous]->(someOtherCenterNode)
WHERE NOT (anotherOrange)-[:homologous]-(blueNode)
RETURN orangeNode

If this isn't correct, then please add additional clarification and description, as this complex case needs to be clear and precise for us to understand what you really want here. A small example case with details may help aid our understanding.

1 Like

Thank you very much! Yes, you understood my data structure perfectly, and using this logic, I managed to write the query that I wanted :slightly_smiling_face:

1 Like