DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Neo4j/Cypher: Using a WHERE Clause to Filter Paths

Neo4j/Cypher: Using a WHERE Clause to Filter Paths

Mark Needham user avatar by
Mark Needham
·
Feb. 20, 13 · Interview
Like (0)
Save
Tweet
Share
6.96K Views

Join the DZone community and get the full member experience.

Join For Free

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.

 



Database Filter (software)

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why Does DevOps Recommend Shift-Left Testing Principles?
  • Upgrade Guide To Spring Data Elasticsearch 5.0
  • Choosing the Best Cloud Provider for Hosting DevOps Tools
  • Type Variance in Java and Kotlin

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: