Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save manjunathshiva/9ae974ebd6b1a45bab37e19f53345b36 to your computer and use it in GitHub Desktop.

Select an option

Save manjunathshiva/9ae974ebd6b1a45bab37e19f53345b36 to your computer and use it in GitHub Desktop.
Neo4j Workshop
Demo Server : https://github.com/neo4j-graph-examples/recommendations
https://github.com/neo4j-graph-examples/recommendations/tree/main/data
The database is also available on https://demo.neo4jlabs.com:7473
Username "recommendations", password: "recommendations", database: "recommendations"
https://docs.google.com/document/d/1OLr3KnYpQpTRBJPKvSoY4ikmg5sP5Odwh-E6piuKQA0/edit?usp=sharing
SIMPLE MODEL
CREATE CONSTRAINT person_id_idx FOR (n:Person) REQUIRE n.id IS UNIQUE ;
CREATE CONSTRAINT address_id_idx FOR (n:Address) REQUIRE n.id IS UNIQUE ;
CREATE INDEX person_name_idx FOR (n:Person) ON n.name ;
DATA
CREATE (p:Person {id:1, name:'John Doe', gender:'Male'})
CREATE (a1:Address {id:1, line1:'1 first ln', city:'Edison', state:'NJ', zip:'11111'})
CREATE (a2:Address {id:2, line1:'13 second ln', city:'Edison', state:'NJ', zip:'11111'})
CREATE (a3:Address {id:3, line1:'13 third ln', city:'Edison', state:'NJ', zip:'11111'})
CREATE (a4:Address {id:4, line1:'1 fourth ln', city:'Edison', state:'NJ', zip:'11111'})
CREATE (a5:Address {id:5, line1:'5 other ln', city:'Edison', state:'NJ', zip:'11111'})
CREATE (p)-[:HAS_ADDRESS {start:'2001-01-01', end:'2003-12-31'}]->(a1)
CREATE (p)-[:HAS_ADDRESS {start:'2004-01-01', end:'2008-12-31'}]->(a2)
CREATE (p)-[:HAS_ADDRESS {start:'2009-01-01', end:'2015-12-31'}]->(a3)
CREATE (p)-[:HAS_ADDRESS {start:'2016-01-01', end:'2020-12-31'}]->(a4)
CREATE (p)-[:HAS_ADDRESS {start:'2021-01-01'}]->(a5)
Query 1 – Get the latest address
MATCH (p:Person {name:'John Doe'})-[r:HAS_ADDRESS]->(a)
WHERE r.end is null
RETURN a
Query 2 – Get the first address
MATCH (p:Person {name:'John Doe'})-[r:HAS_ADDRESS]->(a)
WITH r, a
ORDER BY r.start ASC
WITH r,a
RETURN a
LIMIT 1
Query 3 – Get the third address
MATCH (p:Person {name:'John Doe'})-[r:HAS_ADDRESS]->(a)
WITH r, a
ORDER BY r.start ASC
WITH r,a
RETURN a
SKIP 2
LIMIT 1
ADVANCE MODEL
CREATE CONSTRAINT person_id_idx FOR (n:Person) REQUIRE n.id IS UNIQUE ;
CREATE CONSTRAINT address_id_idx FOR (n:Address) REQUIRE n.id IS UNIQUE ;
CREATE INDEX person_name_idx FOR (n:Person) ON n.name ;
DATA
CREATE (p:Person {id:1, name:'John Doe', gender:'Male'})
CREATE (a1:Address {id:1, line1:'1 first ln', city:'Edison', state:'NJ', zip:'11111'})
CREATE (a2:Address {id:2, line1:'13 second ln', city:'Edison', state:'NJ', zip:'11111'})
CREATE (a3:Address {id:3, line1:'13 third ln', city:'Edison', state:'NJ', zip:'11111'})
CREATE (a4:Address {id:4, line1:'1 fourth ln', city:'Edison', state:'NJ', zip:'11111'})
CREATE (a5:Address {id:5, line1:'5 other ln', city:'Edison', state:'NJ', zip:'11111'})
CREATE (p)-[:FIRST]->(r1:Rental {start:'2001-01-01', end:'2003-12-31'})-[:HAS_ADDRESS]->(a1)
CREATE (r1)-[:NEXT]->(r2:Rental {start:'2004-01-01', end:'2008-12-31'})-[:HAS_ADDRESS]->(a2)
CREATE (r2)-[:NEXT]->(r3:Rental {start:'2009-01-01', end:'2015-12-31'})-[:HAS_ADDRESS]->(a3)
CREATE (r3)-[:NEXT]->(r4:Rental {start:'2016-01-01', end:'2020-12-31'})-[:HAS_ADDRESS]->(a4)
CREATE (r4)-[:NEXT]->(r5:Rental {start:'2021-01-01'})-[:HAS_ADDRESS]->(a5)
CREATE (p)-[:LATEST]->(r5)
Query 1 – Get the latest address
MATCH (p:Person {name:'John Doe'})-[:LATEST]->()-[:HAS_ADDRESS]->(a)
RETURN a
Query 2 – Get the first addressZ
MATCH (p:Person {name:'John Doe'})-[:FIRST]->()-[:HAS_ADDRESS]->(a)
RETURN a
Query 3 – Get the third address
MATCH (p:Person {name:'John Doe'})-[:FIRST]->()-[:NEXT*2..2]->()-[:HAS_ADDRESS]->(a)
RETURN a
1. BASIC - Find all movies
--------------------------
MATCH (m:Movie)
RETURN m.title, m.released
ORDER BY m.released
Returns all movies with their titles and release years, ordered chronologically.
================================================================
2. BASIC - Find a specific actor and their movies
--------------------------------------------------
MATCH (actor:Person {name: "Tom Hanks"})-[:ACTED_IN]->(movie:Movie)
RETURN actor.name, movie.title, movie.released
ORDER BY movie.released
Finds all movies that Tom Hanks acted in.
================================================================
3. BASIC - Find co-actors of a specific actor
----------------------------------------------
MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(movie:Movie)<-[:ACTED_IN]-(coActor:Person)
RETURN DISTINCT coActor.name
ORDER BY coActor.name
Finds all actors who have worked with Tom Hanks in any movie.
================================================================
4. INTERMEDIATE - Movies with their cast and crew counts
--------------------------------------------------------
MATCH (m:Movie)
OPTIONAL MATCH (m)<-[:ACTED_IN]-(actor:Person)
OPTIONAL MATCH (m)<-[:DIRECTED]-(director:Person)
OPTIONAL MATCH (m)<-[:PRODUCED]-(producer:Person)
WITH m, count(DISTINCT actor) AS actors, count(DISTINCT director) AS directors, count(DISTINCT producer) AS producers
RETURN m.title, m.released, actors, directors, producers
ORDER BY actors DESC
Returns movies with counts of actors, directors, and producers.
================================================================
5. INTERMEDIATE - Most prolific actors (by movie count)
--------------------------------------------------------
MATCH (actor:Person)-[:ACTED_IN]->(movie:Movie)
WITH actor, count(movie) AS movieCount
WHERE movieCount >= 3
RETURN actor.name, movieCount
ORDER BY movieCount DESC, actor.name
Finds actors who appeared in 3 or more movies, ordered by movie count.
================================================================
6. ADVANCED - Shortest path between two actors
-----------------------------------------------
MATCH path = shortestPath((actor1:Person {name: "Kevin Bacon"})-[*]-(actor2:Person {name: "Tom Hanks"}))
WHERE actor1 <> actor2
RETURN path, length(path) AS pathLength
Finds the shortest connection between Kevin Bacon and Tom Hanks (classic "Six Degrees of Kevin Bacon").
================================================================
7. ADVANCED - Movie recommendations based on shared actors
-----------------------------------------------------------
MATCH (user:Person {name: "Tom Hanks"})-[:ACTED_IN]->(movie:Movie)<-[:ACTED_IN]-(coActor:Person)
MATCH (coActor)-[:ACTED_IN]->(recommendation:Movie)
WHERE NOT (user)-[:ACTED_IN]->(recommendation)
WITH recommendation, count(DISTINCT coActor) AS sharedActors
RETURN recommendation.title, recommendation.released, sharedActors
ORDER BY sharedActors DESC, recommendation.title
LIMIT 10
Recommends movies based on actors who worked with Tom Hanks, ranked by number of shared connections.
================================================================
8. ADVANCED - Complex aggregation with multiple relationship types
-------------------------------------------------------------------
MATCH (m:Movie)
OPTIONAL MATCH (m)<-[:ACTED_IN]-(actor:Person)
OPTIONAL MATCH (m)<-[:DIRECTED]-(director:Person)
OPTIONAL MATCH (m)<-[:PRODUCED]-(producer:Person)
OPTIONAL MATCH (m)<-[:WROTE]-(writer:Person)
WITH m,
collect(DISTINCT actor.name) AS actors,
collect(DISTINCT director.name) AS directors,
collect(DISTINCT producer.name) AS producers,
collect(DISTINCT writer.name) AS writers
RETURN m.title,
m.released,
size(actors) AS actorCount,
size(directors) AS directorCount,
size(producers) AS producerCount,
size(writers) AS writerCount,
actors[0..3] AS topActors
ORDER BY actorCount DESC
Comprehensive movie analysis with all crew types and partial cast display.
================================================================
9. EXPERT - Network analysis: Find highly connected movie clusters
--------------------------------------------------------------------
MATCH (m1:Movie)<-[:ACTED_IN]-(actor:Person)-[:ACTED_IN]->(m2:Movie)
WHERE id(m1) < id(m2)
WITH m1, m2, count(actor) AS sharedActors
WHERE sharedActors >= 2
RETURN m1.title AS movie1,
m1.released AS year1,
m2.title AS movie2,
m2.released AS year2,
sharedActors,
abs(toInteger(m1.released) - toInteger(m2.released)) AS yearDiff
ORDER BY sharedActors DESC, yearDiff ASC
LIMIT 15
Finds movie pairs with significant actor overlap and calculates the time difference between releases.
================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment