Last active
March 4, 2017 21:58
-
-
Save freeeve/badd031d18a9222e806ce2dfcc6c494a to your computer and use it in GitHub Desktop.
queries for class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get an idea of schema for node properties | |
match (n) | |
with n, keys(n) as ks | |
limit 100000 | |
unwind ks as k | |
with n, k | |
order by k | |
return distinct labels(n), collect(distinct k) | |
// query to get actors/directors | |
match (p)-[:ACTED_IN]->(m1), | |
(p)-[:DIRECTED]->(m2) | |
return p.name, collect(distinct m1.title) as acted, collect(distinct m2.title) as directed | |
// query to get only actor/directors that didn't act in the movie they directed | |
match (p)-[:ACTED_IN]->(m1), | |
(p)-[:DIRECTED]->(m2) | |
where not (p)-[:ACTED_IN]->()<-[:DIRECTED]-(p) | |
return p.name, collect(distinct m1.title) as acted, collect(distinct m2.title) as directed | |
// quick create some sample data | |
with [x in range(1,100) | x^2] as coll | |
unwind coll as c | |
create (:Sample {val:c}) | |
// query to get movies that tom and kevin acted in | |
match (tom:Person)-[:ACTED_IN]->(m:Movie), | |
(kevin:Person)-[:ACTED_IN]->(m) | |
where tom.name = "Tom Hanks" | |
and kevin.name = "Kevin Bacon" | |
return m.title | |
// sample import from gist | |
load csv with headers from "https://gist.githubusercontent.com/freeeve/e67e098c3d484ace03e353d88b7188de/raw/23b5e8eff5fa0a53f7ff6a52401d6ea0ffb99021/gistfile1.txt" as record | |
create (:CsvPerson {name:record.name, id:toInteger(record.id)}) | |
// sample import from local file (in import folder) | |
load csv with headers from "file:///people.csv" as record | |
return record | |
// query for tom hanks people older | |
match (tom:Person)-[:ACTED_IN]->(m), | |
(person:Person)-[:ACTED_IN]->(m) | |
where tom.born > person.born | |
and tom.name = "Tom Hanks" | |
return distinct person.name, tom.born - person.born as ageDiff, tom.born, person.born | |
order by ageDiff desc | |
// actors who worked with keanu but not hugo | |
match (keanu:Person)-[:ACTED_IN]->(m), | |
(actor:Person)-[:ACTED_IN]->(m), | |
(hugo:Person) | |
where keanu.name = "Keanu Reeves" | |
and hugo.name = "Hugo Weaving" | |
and not (actor)-[:ACTED_IN]->()<-[:ACTED_IN]-(hugo) | |
return actor.name, m.title | |
// page 142 exercise (expanded) | |
match (n:Person)-[:ACTED_IN]->(m:Movie) | |
return n.name, count(*), collect({title:m.title, released:m.released}) | |
order by count(*) desc | |
limit 5 | |
// partial recommendation query for keanu | |
match (keanu:Person)-[:ACTED_IN]->(m:Movie), | |
(colleague:Person)-[:ACTED_IN|DIRECTED]->(m), | |
(colleague)-[:ACTED_IN|DIRECTED]->(m2:Movie), | |
(coc)-[:ACTED_IN]->(m2) | |
where keanu.name = "Keanu Reeves" | |
and keanu <> coc | |
and not (keanu)-[:ACTED_IN]->()<-[:ACTED_IN]-(coc) | |
return colleague.name, m.title, coc.name, m2.title | |
// top 3 recommendation | |
match (keanu:Person)-[:ACTED_IN]->(m:Movie), | |
(colleague:Person)-[:ACTED_IN|DIRECTED]->(m), | |
(colleague)-[:ACTED_IN|DIRECTED]->(m2:Movie), | |
(coc)-[:ACTED_IN]->(m2) | |
where keanu.name = "Keanu Reeves" | |
and keanu <> coc | |
and not (keanu)-[:ACTED_IN]->()<-[:ACTED_IN]-(coc) | |
return coc.name, count(*), collect([m2.title, colleague.name]) | |
order by count(*) desc | |
limit 3 | |
// get shortest path between two people and print a list of the connections | |
match p=shortestPath((person1)-[:ACTED_IN*]-(person2)) | |
where person1.name = "Kevin Bacon" | |
and person2.name = "Charlize Theron" | |
return [x in nodes(p) | coalesce(x.title, x.name)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment