NoSQL Zone is brought to you in partnership with:

Mark is a graph advocate and field engineer for Neo Technology, the company behind the Neo4j graph database. As a field engineer, Mark helps customers embrace graph data and Neo4j building sophisticated solutions to challenging data problems. When he's not with customers Mark is a developer on Neo4j and writes his experiences of being a graphista on a popular blog at http://markhneedham.com/blog. He tweets at @markhneedham. Mark is a DZone MVB and is not an employee of DZone and has posted 519 posts at DZone. You can read more from them at their website. View Full User Profile

Neo4j/Cypher: Using a WHERE Clause to Filter Paths

02.20.2013
| 2988 views |
  • submit to reddit

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:

Games

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.

 



Published at DZone with permission of Mark Needham, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)