neo4j/cypher: Using a WHERE clause to filter paths
One of the cypher queries that I wanted to write recently was one to find all the players that have started matches for Arsenal this season and the number of matches that they’ve played in.
The data model that I’m querying looks like this:
I started off with the following query which traverses from Arsenal to all the games that they’ve taken part in and finds all the players who’ve played in those games:
START team = node:teams('name:"Arsenal"')
MATCH team-[:home_team|away_team]-game-[:played_in]-player
RETURN player.name, COUNT(player.name) as games
ORDER BY games desc
It returns the following result set:
------------------------------+
| player.name | games |
+------------------------------+
| "Cazorla" | 25 |
| "Arteta" | 22 |
| "Mertesacker" | 22 |
| "Vermaelen" | 22 |
| "Podolski" | 21 |
| "Gibbs" | 18 |
| "Szczesny" | 17 |
…
| "Tiote" | 1 |
| "Diame" | 1 |
| "Ridgewell" | 1 |
| "Lampard" | 1 |
| "Bramble" | 1 |
| "Simpson" | 1 |
+------------------------------+
258 rows
which is partially right but also includes a bunch of players who played for the opposition rather than for Arsenal.
The reason for this is that we’re following the 'played_in' relationship from the games and that relationship doesn’t distinguish between players playing for a specific team in the match.
I initially tried to reduce the number of rows returned by adding to the MATCH clause like this:
START team = node:teams('name:"Arsenal"')
MATCH team-[:home_team|away_team]-game-[:played_in]-player,
player-[:played]-()-[:for]-team
RETURN player.name, COUNT(player.name) as games
ORDER BY games desc
This did work to an extent - it now returned only Arsenal players but the count of games was completely wrong:
+------------------------------+
| player.name | games |
+------------------------------+
| "Cazorla" | 625 |
| "Mertesacker" | 484 |
| "Arteta" | 484 |
| "Vermaelen" | 484 |
| "Podolski" | 441 |
| "Giroud" | 400 |
| "Gibbs" | 324 |
...
+------------------------------+
21 rows
The reason this doesn’t work is that for every Team -> Game -> Player traversal in the first MATCH clauses we are doing a Player->Stats->Team traversal.
Taking Cazorla as an example: the first traversal matches 25 paths and then the second traversal also matches 25 paths.
The reason we end up with 625 matching paths for him is that we’re doing that second traversal 25 times, once for each of the matching paths from the first traversal.
What we actually need to do is make that second MATCH clause a WHERE clause which I hadn’t realised was possible until a few days ago.
In all the previous queries I’ve written with cypher I only ever used the WHERE clause to filter based on node or relationship properties.
If we make this change we end up with the following query
START team = node:teams('name:"Arsenal"')
MATCH team-[:home_team|away_team]-game-[:played_in]-player
WHERE player-[:played]-()-[:for]-team
RETURN player.name, COUNT(player.name) as games
ORDER BY games desc
This returns the following result set:
+------------------------------+
| player.name | games |
+------------------------------+
| "Cazorla" | 25 |
| "Arteta" | 22 |
| "Mertesacker" | 22 |
| "Vermaelen" | 22 |
| "Podolski" | 21 |
| "Gibbs" | 18 |
| "Szczesny" | 17 |
| "Sagna" | 16 |
| "Wilshere" | 16 |
...
+------------------------------+
21 rows
Which is exactly what we want!
The use of WHERE clauses in this way is explained in more detail towards the end of the documentation.
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.