neo4jcyphersimilaritysemanticsknowledge-graph

Shared triples between two knowledge graphs


I want to compare two semantic Knowledge Graphs, to see if they have any triple in common, using cypher.

MATCH (n1)-[r1]-(c1)
MATCH (n2)-[r2]-(c2)  
WHERE r1.filePath = "../data/graph1.json" 
AND r2.filePath = "../data/graph2.json"
AND n1 = n2
AND r1 = r2
AND c1 = c2
RETURN n1, n2, r1, r2, c1, c2

The two graphs are loaded in neo4j, and the only way to distinguish them is the rel property "filePath".

Is this the correct way of doing so? Are there other algorithms to search for similarities between graphs?


Solution

  • If you don't care whether r1 and r2 have the same directionality, nor whether they have the same type, this simple query should be sufficient to find all "common triples" between the 2 knowledge graphs (that share the same node pair):

    MATCH (n)-[r1]-(c)-[r2]-(n)
    WHERE r1.filePath <> r2.filePath
    RETURN n, c, r1, r2
    

    Or, if you want them to have the same directionality:

    MATCH (n)-[r1]-(c)-[r2]-(n)
    WHERE r1.filePath <> r2.filePath AND ENDNODE(r1) = ENDNODE(r2)
    RETURN n, c, r1, r2
    

    Or, if you want the same directionality and type (but do not want to specify a specific type in the MATCH pattern):

    MATCH (n)-[r1]-(c)-[r2]-(n)
    WHERE TYPE(r1) = TYPE(r2) AND r1.filePath <> r2.filePath AND ENDNODE(r1) = ENDNODE(r2)
    RETURN n, c, r1, r2
    

    Also, I'd suggest a shorter relationship property value, as long values can be wasteful of storage space. Something like "g1" and "g2" may be sufficient for your needs.