David542
David542

Reputation: 110267

Cypher query for actors who have not worked with Gene Hackman but have worked with a Director of a Gene Hackman movie

To find the directors of a Gene Hackman movie in the Neo4j sample dataset of Person,Movie I am currently doing:

MATCH
  (p1:Person{name:"Gene Hackman"})-[:ACTED_IN]->(m:Movie)<-[:DIRECTED]-(p2:Person)
RETURN p1, m, p2

How would I get the actors that have been directed by a director of a Gene Hackman movie but who have not acted alongside Gene Hackman in a movie?


Here is some sample json data, or even easier just select the "Movies" dataset at https://sandbox.neo4j.com/

[
  {
    "p1": {
"identity": 89,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Gene Hackman"
      }
    },
    "m": {
"identity": 97,
"labels": [
        "Movie"
      ],
"properties": {
"tagline": "It's a hell of a thing, killing a man",
"title": "Unforgiven",
"released": 1992
      }
    },
    "p2": {
"identity": 99,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Clint Eastwood"
      }
    }
  },
  {
    "p1": {
"identity": 89,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Gene Hackman"
      }
    },
    "m": {
"identity": 97,
"labels": [
        "Movie"
      ],
"properties": {
"tagline": "It's a hell of a thing, killing a man",
"title": "Unforgiven",
"released": 1992
      }
    },
    "p2": {
"identity": 99,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Clint Eastwood"
      }
    }
  },
  {
    "p1": {
"identity": 89,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Gene Hackman"
      }
    },
    "m": {
"identity": 97,
"labels": [
        "Movie"
      ],
"properties": {
"tagline": "It's a hell of a thing, killing a man",
"title": "Unforgiven",
"released": 1992
      }
    },
    "p2": {
"identity": 98,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Richard Harris"
      }
    }
  },
  {
    "p1": {
"identity": 89,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Gene Hackman"
      }
    },
    "m": {
"identity": 97,
"labels": [
        "Movie"
      ],
"properties": {
"tagline": "It's a hell of a thing, killing a man",
"title": "Unforgiven",
"released": 1992
      }
    },
    "p2": {
"identity": 169,
"labels": [
        "Person"
      ],
"properties": {
"name": "Jessica Thompson"
      }
    }
  },
  {
    "p1": {
"identity": 89,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Gene Hackman"
      }
    },
    "m": {
"identity": 95,
"labels": [
        "Movie"
      ],
"properties": {
"tagline": "Come as you are",
"title": "The Birdcage",
"released": 1996
      }
    },
    "p2": {
"identity": 60,
"labels": [
        "Person"
      ],
"properties": {
"born": 1951,
"name": "Robin Williams"
      }
    }
  },
  {
    "p1": {
"identity": 89,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Gene Hackman"
      }
    },
    "m": {
"identity": 95,
"labels": [
        "Movie"
      ],
"properties": {
"tagline": "Come as you are",
"title": "The Birdcage",
"released": 1996
      }
    },
    "p2": {
"identity": 96,
"labels": [
        "Person"
      ],
"properties": {
"born": 1931,
"name": "Mike Nichols"
      }
    }
  },
  {
    "p1": {
"identity": 89,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Gene Hackman"
      }
    },
    "m": {
"identity": 95,
"labels": [
        "Movie"
      ],
"properties": {
"tagline": "Come as you are",
"title": "The Birdcage",
"released": 1996
      }
    },
    "p2": {
"identity": 80,
"labels": [
        "Person"
      ],
"properties": {
"born": 1956,
"name": "Nathan Lane"
      }
    }
  },
  {
    "p1": {
"identity": 89,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Gene Hackman"
      }
    },
    "m": {
"identity": 95,
"labels": [
        "Movie"
      ],
"properties": {
"tagline": "Come as you are",
"title": "The Birdcage",
"released": 1996
      }
    },
    "p2": {
"identity": 169,
"labels": [
        "Person"
      ],
"properties": {
"name": "Jessica Thompson"
      }
    }
  },
  {
    "p1": {
"identity": 89,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Gene Hackman"
      }
    },
    "m": {
"identity": 87,
"labels": [
        "Movie"
      ],
"properties": {
"tagline": "Pain heals, Chicks dig scars... Glory lasts forever",
"title": "The Replacements",
"released": 2000
      }
    },
    "p2": {
"identity": 88,
"labels": [
        "Person"
      ],
"properties": {
"born": 1970,
"name": "Brooke Langton"
      }
    }
  },
  {
    "p1": {
"identity": 89,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Gene Hackman"
      }
    },
    "m": {
"identity": 87,
"labels": [
        "Movie"
      ],
"properties": {
"tagline": "Pain heals, Chicks dig scars... Glory lasts forever",
"title": "The Replacements",
"released": 2000
      }
    },
    "p2": {
"identity": 170,
"labels": [
        "Person"
      ],
"properties": {
"name": "James Thompson"
      }
    }
  },
  {
    "p1": {
"identity": 89,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Gene Hackman"
      }
    },
    "m": {
"identity": 87,
"labels": [
        "Movie"
      ],
"properties": {
"tagline": "Pain heals, Chicks dig scars... Glory lasts forever",
"title": "The Replacements",
"released": 2000
      }
    },
    "p2": {
"identity": 168,
"labels": [
        "Person"
      ],
"properties": {
"name": "Angela Scope"
      }
    }
  },
  {
    "p1": {
"identity": 89,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Gene Hackman"
      }
    },
    "m": {
"identity": 87,
"labels": [
        "Movie"
      ],
"properties": {
"tagline": "Pain heals, Chicks dig scars... Glory lasts forever",
"title": "The Replacements",
"released": 2000
      }
    },
    "p2": {
"identity": 169,
"labels": [
        "Person"
      ],
"properties": {
"name": "Jessica Thompson"
      }
    }
  },
  {
    "p1": {
"identity": 89,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Gene Hackman"
      }
    },
    "m": {
"identity": 87,
"labels": [
        "Movie"
      ],
"properties": {
"tagline": "Pain heals, Chicks dig scars... Glory lasts forever",
"title": "The Replacements",
"released": 2000
      }
    },
    "p2": {
"identity": 1,
"labels": [
        "Person"
      ],
"properties": {
"born": 1964,
"name": "Keanu Reeves"
      }
    }
  },
  {
    "p1": {
"identity": 89,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Gene Hackman"
      }
    },
    "m": {
"identity": 87,
"labels": [
        "Movie"
      ],
"properties": {
"tagline": "Pain heals, Chicks dig scars... Glory lasts forever",
"title": "The Replacements",
"released": 2000
      }
    },
    "p2": {
"identity": 91,
"labels": [
        "Person"
      ],
"properties": {
"born": 1950,
"name": "Howard Deutch"
      }
    }
  },
  {
    "p1": {
"identity": 89,
"labels": [
        "Person"
      ],
"properties": {
"born": 1930,
"name": "Gene Hackman"
      }
    },
    "m": {
"identity": 87,
"labels": [
        "Movie"
      ],
"properties": {
"tagline": "Pain heals, Chicks dig scars... Glory lasts forever",
"title": "The Replacements",
"released": 2000
      }
    },
    "p2": {
"identity": 90,
"labels": [
        "Person"
      ],
"properties": {
"born": 1968,
"name": "Orlando Jones"
      }
    }
  }
]

Upvotes: 0

Views: 68

Answers (1)

PrashantUpadhyay
PrashantUpadhyay

Reputation: 877

This should work. Next time, If possible please attach a small sample dataset.

MATCH (p1:Person {name:"Gene Hackman"})-[:ACTED_IN]->(m:Movie)<-[:DIRECTED]-(director:Person)-[:DIRECTED]->(m2:Movie) 
WHERE NOT (m2)<-[:ACTED_IN]-(:Person {name: "Gene Hackman"}) 
MATCH (m2)<-[:ACTED_IN]-(p4:Person) 
RETURN p4

Upvotes: 1

Related Questions