Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is part of a series of graphgists, taking the examples in the GraphDatabases book and making them available for playing with Neo4j 2.0 updates.

GraphDatabases Neoj4 2.0 examples

cropped graphdatabases cover390x5121

This graphgist is an attempt to give some live datasets for the Graphdatabases book by Ian Robinson and Jim Webber, based on the work and blog of Joerg Baach in his blog post

The Shakespeare setup

CREATE
    (shakespeare:Author { firstname: 'William', lastname: 'Shakespeare' }),
    (juliusCaesar:Character { title: 'Julius Caesar' }),
    (shakespeare)-[:WROTE_PLAY { year: 1599 }]->(juliusCaesar),
    (theTempest:Play { title: 'The Tempest' }),
    (shakespeare)-[:WROTE_PLAY { year: 1610}]->(theTempest),
    (rsc:Company { name: 'RSC' }),
    (production1:Production { name: 'Julius Caesar' }),
    (rsc)-[:PRODUCED]->(production1),
    (production1)-[:PRODUCTION_OF]->(juliusCaesar),
    (performance1:Performance { date: 20120729 }),
    (performance1:Performance)-[:PERFORMANCE_OF]->(production1),
    (production2:Production { name: 'The Tempest' }),
    (rsc)-[:PRODUCED]->(production2),
    (production2)-[:PRODUCTION_OF]->(theTempest),
    (performance2:Performance { date: 20061121 }),
    (performance2)-[:PERFORMANCE_OF]->(production2),
    (performance3:performance { date: 20120730 }),
    (performance3)-[:PERFORMANCE_OF]->(production1),
    (billy:Person { name: 'Billy' }),
    (review:Review { rating: 5, review: 'This was awesome!' }),
    (billy)-[:WROTE_REVIEW]->(review),
    (review)-[:RATED]->(performance1),
    (theatreRoyal:Venue { name: 'Theatre Royal' }),
    (performance1)-[:VENUE]->(theatreRoyal),
    (performance2)-[:VENUE]->(theatreRoyal),
    (performance3)-[:VENUE]->(theatreRoyal),
    (greyStreet:Street { name: 'Grey Street' }),
    (theatreRoyal)-[:STREET]->(greyStreet),
    (newcastle:City { name: 'Newcastle' }),
    (greyStreet)-[:CITY]->(newcastle),
    (tyneAndWear:County { name: 'Tyne and Wear' }),
    (newcastle)-[:COUNTY]->(tyneAndWear),
    (england:Country { name: 'England' }),
    (tyneAndWear)-[:COUNTRY]->(england),
    (stratford:City { name: 'Stratford upon Avon' }),
    (stratford)-[:COUNTRY]->(england),
    (rsc)-[:BASED_IN]->(stratford),
    (shakespeare)-[:BORN_IN]->stratford;

Contstraining Matches

Page 48:

match
    theater:Venue,
    newcastle:City,
    bard:Author,
    (newcastle)<-[:STREET|CITY*1..2]-(theater)
    <-[:VENUE]-()-[:PERFORMANCE_OF]->()-[:PRODUCTION_OF]->
    (play)<-[:WROTE_PLAY]-(bard)
where
    theater.name='Theatre Royal' and
    newcastle.name='Newcastle' and
    bard.lastname='Shakespeare'
return
    distinct play.title as play;

Processing Results

Page 49:

match
    theater:Venue,
    newcastle:City,
    bard:Author,
    (newcastle)<-[:STREET|CITY*1..2]-(theater)
    <-[:VENUE]-()-[p:PERFORMANCE_OF]->()-[:PRODUCTION_OF]->
    (play)<-[:WROTE_PLAY]-(bard)
where
    theater.name='Theatre Royal' and
    newcastle.name='Newcastle' and
    bard.lastname='Shakespeare'
return
    play.title as play, count(p) as performance_count
order by
    performance_count desc;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment