简体   繁体   中英

Check if Neighbors have subset of connected nodes

I currently have a Neo4J Graph Database that stores 4 different kind of nodes that are connected via edges. Yellow/Red nodes that are connected to blue nodes and the yellow/red nodes have green nodes connected that give additional info on them.

What I want to do is to check if the selected node(either yellow or red) has a neighbor that is connected via the blue node, has a subset of common connected green nodes.

For example if I select the lower red node the upper red node would be returned as they both share the same far left green node neighbor which is a subset of the green node neighbors of the selected red node.

Example Graph Structure

I currently have the following Cypher query, where the table nodes are the yellow and red nodes and the keyNode are the green nodes. The blue node is the objectType.

MATCH(table:TABLE)
where table.Name = $name
MATCH (table)-[keyRel:IS_DEFINED_BY_KEY]->(keyNode)
MATCH (table)<-[createRel:IS_CREATED_IN]-(objectType)-[createRel2:IS_CREATED_IN]->(createTables)
MATCH (createTables)-[keyRel2:IS_DEFINED_BY_KEY]->(addKey)

So at the end of the current query I have the searched/selected table, its neighboring green nodes, the yellow/red nodes that are connected via the blue node and the green nodes that are connected to them.

With that I now want to only return the connected yellow/red nodes that are connected to a subset of green nodes. So in the example only the upper red node.

Has anybody an idea how I can achieve this?

I think I have found a solution.

I thought it the other way around. So instead of searching via the blue node that is connected to both my nodes I went via the green node.

The green neighbors of the other nodes can only be a subset of the selected nodes green neighbors if all of the other nodes neighbors are also connected to the selected node. You can find the used cypher query below. The interesting part where the subset is tested is:

Match (table) WHERE NOT ALL(t2key in t2keys WHERE (table)-[:IS_DEFINED_BY_KEY]->(t2key)) .

MATCH(table:TABLE)
where table.Name = $name
Match (table)<-[:IS_CREATED_IN]-(objectType)

WITH Distinct table, objectType

MATCH (table)-[:IS_DEFINED_BY_KEY]->(keyNode)

MATCH (keyNode)<-[:IS_DEFINED_BY_KEY]-(T2:RECORDTABLE)
WHERE (T2.Neo4JId)<>(table.Neo4JId) and Exists ((objectType)-[:IS_CREATED_IN]->(T2))

MATCH (T2)-[:IS_DEFINED_BY_KEY]->(T2Key)

WITH table, collect(T2Key) as t2keys

// RETURN DISTINCT table, t2keys
Match (table)
WHERE NOT ALL(t2key in t2keys WHERE (table)-[:IS_DEFINED_BY_KEY]->(t2key))

Return table 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM