Search code examples
redisgraph

Is it currently possible to do match intersection queries in redis-graph?


Is it currently possible to do a query in RedisGraph for match intersections given the current Cypher limitations?

Eg something equivalent to the following Cypher script that would return countries that have been visited by both Mor Yesharim and Noam Nativ:

MATCH (p:Person)-[x:VISITED]->(c:Country)
WHERE p.name = 'Mor Yesharim' OR p.name = 'Noam Nativ'
WITH c, count(DISTINCT p) as cnt
WHERE cnt = 2
RETURN c

Solution

  • Your provided example can be re-written in a single pattern, like so:

    MATCH (p:Person)-[:VISITED]->(c:Country)<-[:VISITED]-(q:person)
    WHERE p.name = 'Mor Yesharim' AND q.name = 'Noam Nativ'
    RETURN c
    

    This is not viable for more complex patterns, however (if the in-degree for Country was greater than 2, for example). In these cases, you can combine WITH and ID matching:

    MATCH (p:Person)-[:VISITED]->(c:Country)
    WHERE p.name = 'Mor Yesharim' WITH c AS first
    MATCH (q:Person)-[:VISITED]->(d:Country)
    WHERE q.name = 'Noam Nativ'
    AND ID(first) = ID(d)
    RETURN d