Search code examples
neo4jmatchcypherpartial

return partial match in cyhper


Hi I'm using this query to return the common child nodes of three starting nodes.

MATCH (c1:node)-[*]->(x), (c2:node)-[*]->(x), (c3:node)-[*]->(x)
USING INDEX c1:node(name)
USING INDEX c2:node(name)
USING INDEX c3:node(name)
WHERE c1.name = "Tobin" AND c2.name ="John" AND c3.name ="Clarke"
RETURN DISTINCT x

Currently it does not return anything unless all three nodes have a common child node. How can I have it return a partial match where say Tobin and John have a common child that Clarke does not?

Ideally in this case i would then separately have it return Clarke's first child even though it's not a match to the others.. but maybe i'm asking too much from one query?

Thanks!


Solution

  • It's kinda hard, because any child of any of your 3 nodes would match that condition. So what you're creating there is a cross product.

    In Neo4j 2.2.0 you could try this:

    MATCH (c1:node),(c2:node), (c3:node)
    USING INDEX c1:node(name)
    USING INDEX c2:node(name)
    USING INDEX c3:node(name)
    WHERE c1.name = "Tobin" AND c2.name ="John" AND c3.name ="Clarke"
    MATCH (c1)-[*]->(x)
    WHERE shortestPath((c2)-[*]->(x)) oR shortestPath((c3)-[*]->(x))
    RETURN DISTINCT x