neo4jcypherneo4j-apocapoc

Neo4j: Searching in graph for exact same sub-graph


I have currently the problem, that I want to find an exactly same subgraph under one other node.

To make it more specific. I have multiple repair guides which have multiple repair steps. The repair steps can exist also inside another repair guide. Now I want to find similar repair guides which have the exact same repair steps.

MATCH (mp1:Maintenance{name:'13 Years (325000 km)'})-[:HAS]->(ws:WorkStep)
WHERE id(mp1)=49214
with collect(ws) as workingssteps, mp1
MATCH (mp2:Maintenance)

Now I'm stuck, how to proceed further to iterate over mp2 to find the same working steps from mp1.

Thanks for any help or tips. Christian


Solution

  • Here is one way of finding other Maintenance nodes connected to the same set of WorkStep nodes as a given Maintenance node:

    MATCH (m:Maintenance)-[:HAS]->(w:WorkStep)
    WHERE m.name = '13 Years (325000 km)'
    WITH m, COLLECT(w) AS ws
    MATCH (other:Maintenance)-[:HAS]->(w1:WorkStep)
    WHERE w1 IN ws
    WITH ws, other, COLLECT(w1) AS w1s
    WHERE ALL(x IN ws WHERE x IN w1s)
    RETURN other
    

    I assume above that the name property of Maintenance nodes is unique. It is not generally a reliable practice to find a node/relationship by its the native ID, as the native ID can be reused after entity deletion. Also, the ID() function is now deprecated.