Search code examples
neo4jcyphermatchmovie

Neo4j: Find Person who acted in and directed any movies


I want to display all Person who act as well as direct a movie. It does not matter if the person direct a movie but does not act in the movie. As long as there are edges ACTED_IN and DIRECTED exist on a node, the query will display the result.

I have tried several Cypher queries. This one I believe show the nearest result I intend to:

MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE exists( (p)-[:DIRECTED]->() )
RETURN distinct *

Now, the issue is, one of the result shows "James Marshall" ACTED_IN "A Few Good Men" but he also DIRECTED two different movies which are "Ninja Assasin" and "V for Vendetta".

My current outcome only display "James Marshall" ACTED_IN "A Few Good Men" and does not show the other two movies he DIRECTED. So, how can I improve my Cypher?


Solution

  • You can first match on persons that have the relationships you need (this will be a degree check), then MATCH on the pattern using both relationships at once (which would be an OR match for the relationships in question otherwise):

    MATCH (p:Person)
    WHERE (p)-[:ACTED_IN]->() AND (p)-[:DIRECTED->()
    MATCH path = (p)-[:ACTED_IN | DIRECTED]->(m:Movie)
    RETURN path