GraphDB Leaderboard
Neo4j 2.0 introduces node labels. Here’s a quick example showing how labels work using Cypher. We’re going to create a
track and a few drivers that have raced.
Create the Track record
CREATE (n:Track {name: 'International Raceway'});
Create the Driver records
CREATE (n:Driver {name: 'Driver 1'}); CREATE (n:Driver {name: 'Driver 2'}); CREATE (n:Driver {name: 'Driver 3'}); CREATE (n:Driver {name: 'Driver 4'}); CREATE (n:Driver {name: 'Driver 5'});
Add track time result to the driver
MATCH d:Driver, t:Track WHERE d.name = 'Driver 1' AND t.name = 'International Raceway' CREATE d-[:RESULT {time: 140}]->t;
Here’s how a Cypher query is executed in plain english:
MATCH
Assign d to Driver
Assign t to Track
WHERE
This works like regular SQL, so basically we are doing where driver name is Driver and track name is International Raceway
CREATE
Create a relationship with the time result, and link it to driver and the track
*Time is stored in seconds
Here’s how relationships are created and we’re adding the rest of the lap times.
MATCH d:Driver, t:Track WHERE d.name = 'Driver 2' AND t.name = 'International Raceway' CREATE d-[:RESULT {time: 122}]->t; MATCH d:Driver, t:Track WHERE d.name = 'Driver 3' AND t.name = 'International Raceway' CREATE d-[:RESULT {time: 92}]->t; MATCH d:Driver, t:Track WHERE d.name = 'Driver 4' AND t.name = 'International Raceway' CREATE d-[:RESULT {time: 122}]->t; MATCH d:Driver, t:Track WHERE d.name = 'Driver 5' AND t.name = 'International Raceway' CREATE d-[:RESULT {time: 89}]->t;
1:06 – 66
1:29 – 89
1:32 – 92
1:46 – 106
2:02 – 122
Create the Leaderboard
To create the leaderboard, we execute the following query which hands us our driver’s quickest lap times in ascending order,
listing the fastest driver first.
MATCH driver-[r:RESULT]-track WHERE track.name = 'International Raceway' WITH distinct driver.name as name, r.time as time RETURN name, min(time) ORDER BY min(time);
==> +---------------------------+ ==> | driver.name | min(r.time) | ==> +---------------------------+ ==> | "Driver 1" | 66 | ==> | "Driver 5" | 89 | ==> | "Driver 3" | 92 | ==> | "Driver 4" | 122 | ==> | "Driver 2" | 122 | ==> +---------------------------+
Finding all the results
If we want to see all of the times posted by our drivers, we would execute the following:
MATCH driver-[r:RESULT]-track WHERE track.name = 'International Raceway' RETURN driver.name, r.time ORDER BY r.time
Even though this is a simple example, it shows how to set up a graph to represent your data, set up relationships and
query against that data to provide a leaderboard and a listing of all of the driver times.
Tags: Neo4j