Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Intro to Cypher


Published on

Based on a slideshow by Max De Marzi:

Updated for Neo4j 2.x (removed `START` clauses and `?` syntax for relationships). I also removed some slides to make it more appropriate an a simply Cypher introduction

Published in: Technology
  • Be the first to comment

Intro to Cypher

  1. 1. Cypher Query Language Chicago Graph Database Meet-Up Max De Marzi Updated for Neo4j 2.x by Brian Underwood
  2. 2. What is Cypher? •Graph Query Language for Neo4j •Aims to make querying simple
  3. 3. Motivation Why Cypher? • Existing Neo4j query mechanisms were not simple enough • Too verbose (Java API) • Too prescriptive (Gremlin)
  4. 4. Motivation SQL? • Unable to express paths • these are crucial for graph-based reasoning • Neo4j is schema/table free
  5. 5. Design Decisions Pattern matching
  6. 6. Design Decisions Pattern matching A B C
  7. 7. Design Decisions Pattern matching
  8. 8. Design Decisions Pattern matching
  9. 9. Design Decisions Pattern matching
  10. 10. Design Decisions Pattern matching
  11. 11. Design Decisions ASCII-art patterns () --> ()
  12. 12. Design Decisions Directed relationship (A) --> (B) A B
  13. 13. Design Decisions Undirected relationship (A) -- (B) A B
  14. 14. Design Decisions specific relationships A -[:LOVES]-> B A B LOVE S
  15. 15. Design Decisions Joined paths A --> B --> C A B C
  16. 16. Design Decisions multiple paths A --> B --> C, A --> C A B C A --> B --> C <-- A
  17. 17. Design Decisions Variable length paths A -[*]-> B A B A B A B ...
  18. 18. Design Decisions Familiar for SQL users select from where group by order by match where return
  19. 19. MATCH SELECT * FROM people WHERE people.firstName = “Max” MATCH (max:Person {firstName: ‘Max’}) RETURN max MATCH (max:Person) WHERE max.firstName = ‘Max’ RETURN max
  20. 20. MATCH SELECT skills.* FROM users JOIN skills ON = skills.user_id WHERE users.first_name = ‘Max’ MATCH (user:User {firstName: ‘Max’}) --> (skill:Skill) RETURN skill
  21. 21. OPTIONAL MATCH SELECT skills.* FROM users LEFT JOIN skills ON = skills.user_id WHERE users.first_name = ‘Max’ MATCH (user:User {firstName: ‘Max’}) OPTIONAL MATCH user –-> (skill:Skill) RETURN skill
  22. 22. SELECT skills.*, user_skill.* FROM users JOIN user_skill ON = user_skill.user_id JOIN skills ON user_skill.skill_id = WHERE users.first_name = ‘Max’
  23. 23. MATCH (user:User {firstName: ‘Max’})- [user_skill]-> (skill:Skill) RETURN skill, user_skill
  24. 24. Indexes Used as multiple starting points, not to speed up any traversals CREATE INDEX ON :User(name); MATCH (a:User {name: ‘Max’})-[r:KNOWS]-b RETURN ID(a), ID(b), r.weight;
  25. 25. Complicated Match Some UGLY recursive self join on the groups table MATCH group <-[:BELONGS_TO*]- (max:Person {name: ‘Max’}) RETURN group
  26. 26. Where SELECT person.* FROM person WHERE person.age >32 OR = "bald" MATCH (person:Person) WHERE person.age > 32 OR = "bald" RETURN person
  27. 27. Return SELECT, count(*) FROM people GROUP BY ORDER BY MATCH (person:Person) RETURN, count(*) ORDER BY
  28. 28. Order By, Parameters Same as SQL {node_id} expected as part of request MATCH (me)-[:follows]->(friends)-[:follows]->(fof)-[:follows]->(fofof)- [:follows]->others WHERE ID(me) = {node_id} RETURN,,,, count(others) ORDER BY,,, count(others) DESC
  29. 29. Graph Functions Some UGLY multiple recursive self and inner joins on the user and all related tables MATCH p = shortestPath( lucy-[*]-kevin ) WHERE ID(lucy) = 1000 AND ID(kevin) = 759 RETURN p
  30. 30. Aggregate Functions ID: get the neo4j assigned identifier Count: add up the number of occurrences Min: get the lowest value Max: get the highest value Avg: get the average of a numeric value Distinct: remove duplicates MATCH (me:User)-[r:wrote]-() RETURN ID(me),, count(r), min(, max( ORDER BY ID(me)
  31. 31. Functions Collect: put aggregated values in a list MATCH (a:User)-[:follows]->b RETURN, collect( Each result row contains a name for each user and a list of names which that user follows
  32. 32. Combine Functions Collect the ID of friends MATCH (me:User)<-[r:wrote]-(friends) RETURN ID(me),, collect(ID(friends)), collect( ORDER BY ID(me)
  33. 33. Uses Recommend Friends MATCH (me)-[:friends]->(friend)-[:friends]->(foaf) WHERE ID(me) = {node_id} RETURN
  34. 34. Uses Six Degrees of Kevin Bacon MATCH path = allShortestPaths( me-[*]->them ) WHERE ID(me) = {start_node_id} AND ID(them) = {destination_node_id} RETURN length(path), extract(person in nodes(path) : Length: counts the number of nodes along a path Extract: gets the nodes/relationships from a path
  35. 35. based-recommendations-with.html MATCH (me:User {id: {me_id}}), (similarUser:User), (similarUsers)-[r:RATED]->(item) WHERE ID(similarUser) IN {previousResult) AND r.rating > 7 AND NOT((me)-[:RATED]->(item)) RETURN item Items with a rating > 7 that similar users rated, but I have not And: this and that are true Or: this or that is true Not: this is false Boolean Operations
  36. 36. START london = node(1), moscow = node(2) MATCH path = london -[*]-> moscow WHERE all(city in nodes(path) where = true) Predicates ALL: closure is true for all items ANY: closure is true for any item NONE: closure is true for no items SINGLE: closure is true for exactly 1 item
  37. 37. Thanks for Listening! Questions?