Skip to content

Instantly share code, notes, and snippets.

@venriq
Last active December 17, 2015 16:19
Show Gist options
  • Save venriq/5637975 to your computer and use it in GitHub Desktop.
Save venriq/5637975 to your computer and use it in GitHub Desktop.
Cypher queries
START a=node(*)
MATCH (a)-[r:ACTED_IN]->(m)<-[:DIRECTED]-(d)
RETURN a.name AS actor, r.roles AS role, m.title AS movie, d.name AS director;
Paths
==================
START a=node(*)
MATCH path=(a)-[r:ACTED_IN]->(m)<-[:DIRECTED]-(d)
RETURN path; || RETURN head(path); || RETURN nodes(path)
START a=node(*)
MATCH path1=(a)-[r:ACTED_IN]->(m), path2=(d)-[:DIRECTED]->(m)
RETURN path1, path2;
Counts
=======================
START a=node(*)
MATCH (a)-[:ACTED_IN]->(m)<-[:DIRECTED]-(d)
RETURN a.name, d.name, count(*);
Acted and directed the movie
============================================
START a=node(*)
MATCH (a)-[:ACTED_IN]->(m)<-[:DIRECTED]-(a)
RETURN a.name, m.title
=============================================
START a=node(*)
MATCH (a)-[:ACTED_IN]->(m)<-[:DIRECTED]-(d)
RETURN a.name, d.name, count(*) as count
ORDER BY count DESC
SKIP 4
LIMIT 5;
Movies people has worked in together
======================================
START a=node(*)
MATCH (a)-[:ACTED_IN]->(m)<-[:DIRECTED]-(d)
RETURN a.name, d.name, collect(m.title), count(*)
ORDER BY count(*) DESC
After autoindexing is enabled in the config file, an autoindex can be created
============================================================================
neo4j-sh (?)$ index --create node_auto_index -t Node
To verify the index
===================================
neo4j-sh (?)$ index --indexes
Using index to find Tom Hanks
==============================================
START tom=node:node_auto_index(name="Tom Hanks")
RETURN tom.name
Movies Tom Hanks acted in
==============================================
START tom=node:node_auto_index(name="Tom Hanks")
MATCH (tom) -[:ACTED_IN]-> (movie)
RETURN movie.title
Directors of movies Tom Hanks acted in
===============================================
START tom=node:node_auto_index(name="Tom Hanks")
MATCH (tom) -[:ACTED_IN]-> () <-[:DIRECTED]-(d)
RETURN d.name
Movies Tom Hanks and Kevin Bacon and Ed Harris acted together
==================================================
START tom=node:node_auto_index(name="Tom Hanks"),
kevin=node:node_auto_index(name="Kevin Bacon"),
ed=node:node_auto_index(name="Ed Harris")
MATCH (tom) -[:ACTED_IN]-> (m),
(ed)-[:ACTED_IN]-> (m),
(kevin) -[:ACTED_IN]-> (m)
RETURN DISTINCT m.title
=======================================================
START a=node(*)
MATCH (a) -[:ACTED_IN]-> (m)
RETURN a.name, count(*)
ORDER BY count(*) DESC
LIMIT 5
Find 3 actors Keanu Reeves hasn't work with
=======================================================
START keanu=node:node_auto_index(name="Keanu Reeves")
MATCH (keanu) -[:ACTED_IN]-> (m) <-[:ACTED_IN]- (c),
(c) -[:ACTED_IN]-> () <-[:ACTED_IN]- (coc)
WHERE not((keanu) -[:ACTED_IN]-> () <-[:ACTED_IN]- (coc) )
AND coc <> keanu
RETURN coc.name, count(coc)
ORDER BY count(coc) DESC
LIMIT 3
LAB
==============================
CREATE({title:"Mystic River", released: 1993})
START movie=node:node_auto_index(title="Mystic River"),
kevin=node:node_auto_index(name="Kevin Bacon")
CREATE UNIQUE (kevin) -[:ACTED_IN {roles:["Sean"]}]-> (movie)
START kevin=node:node_auto_index(name="Kevin Bacon")
MATCH (kevin) -[:ACTED_IN]-> (movie)
RETURN movie.title
Change role of kevin to Sean Divine in the movie Mystic River
================================================
START movie=node:node_auto_index(title="Mystic River"),
kevin=node:node_auto_index(name="Kevin Bacon")
MATCH (kevin) -[r:ACTED_IN]-> (movie)
SET r.roles = "Sean Devine"
RETURN r.roles;
Add Clint Eastwood as director of Mystic River
==============================================
START movie=node:node_auto_index(title="Mystic River"),
clint=node:node_auto_index(name="Clint Eastwood")
CREATE UNIQUE (clint) -[:DIRECTED]-> (movie)
Get all characters in the movie "The Matrix"
================================================
START movie=node:node_auto_index(title="The Matrix")
MATCH () -[r:ACTED_IN]-> (movie)
RETURN r.roles
Create node
==================================================
CREATE({title:"Mystic River", released: 1993})
Add ACTED_IN relationship between Kevin Bacon and Mystic River
================================================
START movie=node:node_auto_index(title="Mystic River"),
kevin=node:node_auto_index(name="Kevin Bacon")
CREATE UNIQUE (kevin) -[:ACTED_IN {roles:["Sean"]}]-> (movie)
Get all movies Kevin Bacon acted in
==============================================
START kevin=node:node_auto_index(name="Kevin Bacon")
MATCH (kevin) -[:ACTED_IN]-> (movie)
RETURN movie.title
Change role of kevin to Sean Divine in the movie Mystic River
================================================
START movie=node:node_auto_index(title="Mystic River"),
kevin=node:node_auto_index(name="Kevin Bacon")
MATCH (kevin) -[r:ACTED_IN]-> (movie)
SET r.roles = "Sean Devine"
RETURN r.roles;
Add Clint Eastwood as director of Mystic River
==============================================
START movie=node:node_auto_index(title="Mystic River"),
clint=node:node_auto_index(name="Clint Eastwood")
CREATE UNIQUE (clint) -[:DIRECTED]-> (movie)
Get all characters in the movie "The Matrix"
================================================
START movie=node:node_auto_index(title="The Matrix")
MATCH () -[r:ACTED_IN]-> (movie)
RETURN r.roles
Delete nodes and relationships
===========================================
START emil=node:node_auto_index(name="Emil Eifrem")
MATCH (emil) -[r?]-> ()
DELETE r, emil
Create a KNOWS relationship between people who acted or directed same movie
===================================================
START a=node(*)
MATCH (a) -[:ACTED_IN|DIRECTED]-> () <-[:ACTED_IN|DIRECTED]- (b)
CREATE UNIQUE (a) -[:KNOWS]-> (b);
Get name of friend of friend
====================================================
START keanu=node:node_auto_index(name="Keanu Reeves")
MATCH (keanu) -[:KNOWS*2]-> (fof)
RETURN fof.name
START keanu=node:node_auto_index(name="Keanu Reeves")
MATCH (keanu) -[:KNOWS*2]-> (fof)
WHERE NOT (keanu -[:KNOWS]- (fof))
RETURN DISTINCT fof.name
Get shortest path between 2 nodes
======================================================
START keanu=node:node_auto_index(name="Charlize Theron"),
bacon=node:node_auto_index(name="Kevin Bacon")
MATCH p=shortestPath((keanu) -[:KNOWS*]-> (bacon))
RETURN length(rels(p))
START theron=node:node_auto_index(name="Charlize Theron"),
bacon=node:node_auto_index(name="Kevin Bacon")
MATCH p=shortestPath((theron) -[:KNOWS*]-> (bacon))
RETURN extract(n in nodes(p): n.name)
START theron=node:node_auto_index(name="Charlize Theron"),
bacon=node:node_auto_index(name="Kevin Bacon")
MATCH p=shortestPath((theron) -[:ACTED_IN*]- (bacon))
RETURN extract(n in nodes(p): coalesce(n.name?, n.title?))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment