Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cypher Sleuthing: Tips and Tricks for Querying a Graph

Jennifer Reif
September 17, 2021

Cypher Sleuthing: Tips and Tricks for Querying a Graph

Searching for ways to improve your knowledge and skill in writing queries with graph data? While many marketing and sales messages tell us graphs are easier to understand and relatable for other users, writing and structuring graph queries is often different than the skills we currently possess. So how can we up-skill without the pain and time of trial-and-error?

Join us as we dive into the structure of a graph database and understand how queries operate. To help us understand the query logic, we will explore tools and tricks to finding out where things went wrong or where we can optimize our queries. We will cover tips and tricks, as well as pitfalls, to help us avoid future road blocks or performance hits.

Jennifer Reif

September 17, 2021

More Decks by Jennifer Reif

Other Decks in Technology


  1. Jennifer Reif Email: jennifer.reif@neo4j.com Twitter: @JMHReif LinkedIn: linkedin.com/in/jmhreif Github: GitHub.com/JMHReif

    Website: jmhreif.com Cypher Sleuthing: Tips and Tricks for Querying a Graph
  2. Who Am I? • Developer • Continuous learner • Conference

    speaker • Blogger • Geek Jennifer Reif Email: jennifer.reif@neo4j.com Twitter: @JMHReif LinkedIn: linkedin.com/in/jmhreif Github: GitHub.com/JMHReif Website: jmhreif.com
  3. Why do we care? And how did this come about?

  4. Cypher, graph, query languages? • What does it have to

    do with you? • Documenting my journey learning Cypher/graph • Lessons for anything: • Adding skills • Graph/NoSQL not going away • Understanding data structures • Query language logic Photo by Matt Howard on Unsplash
  5. All about logic and understanding • Programming language vs query

    language logic • Pattern data structures vs categorical data structures • These tidbits can apply to: • Other languages • Other products • Other solutions • NOT just Neo4j and/or Cypher! Photo by AbsolutVision on Unsplash
  6. Tackling a solution… • Trial-and-error • At the mercy of

    the language • Whack-a-mole fi xes • What if…. Photo by Michal Vrba on Unsplash
  7. Understanding is the key • How to wield the tool

    brings mastery • Knowing when and how —> • E ffi ciency • Better results Credit: unsplash.com
  8. Cypher query language

  9. • Graph query language • Created by Neo4j ~10yrs ago

    • Open sourced • Widely supported • Part of GQL standardization • opencypher.org What is Cypher? Photo by Eugene Chystiakov on Unsplash
  10. • Follow path of connected data • Help understand data

    patterns • Surface hidden connections • Traverse graph data in a database What does it do? Photo by ConvertKit on Unsplash
  11. Quick lesson on Cypher syntax

  12. Functional and visual • Designed to store and retrieve data

    in a graph • Based on ASCII-art • Declarative query language • Focus on what to retrieve, not how A B LIKES MATCH ( A ) - [ : LIKES] - > ( B )
  13. Cypher in 20 seconds… • Nodes look like this: •

    (var:Label) OR (var:Label {propKey: propValue}) • Relationships look like this: • -[var:REL_TYPE]-> OR -[var:REL_TYPE {propKey: propValue}]-> • Using Cypher is looking for patterns of those nodes/relationships: • (var1:Label)-[var2:REL_TYPE]->(var3:Label)
  14. Cypher create/insert Jennifer Neo4j WORKS_FOR CREATE (:Person { name: ‘Jennifer’})

  15. Cypher search/match Jennifer Neo4j WORKS_FOR MATCH (:Person { name: ‘Jennifer’}

    ) -[:WORKS_FOR]-> ( whom ) 
 RETURN whom
  16. Here’s what I’ve learned

  17. Cypher CASE

  18. Pitfalls (mine, at least) • Logic like other languages (e.g.

    Java) • Variable-setting • Complex conditionals Photo by Wander Fleur on Unsplash
  19. Examples //Bad syntax LOAD CSV…AS row MERGE (c:Company {companyId: row.Id})

    WITH row, c, CASE row.type WHEN 'P' THEN row.type = 'Public' WHEN 'R' THEN row.type = 'Private' WHEN 'G' THEN row.type = 'Government' ELSE row.type = 'Other' END SET c.businessType = row.type RETURN * //Bad syntax 2 LOAD CSV…AS row WITH row, CASE row.BusinessType WHEN 'P' THEN type = 'Public' WHEN 'R' THEN type = 'Private' WHEN 'G' THEN type = 'Government' ELSE type = 'Other' END RETURN row.CompanyId, row.CompanyName, type
  20. Correct Logic • 1. Expression comparison against multiple values MATCH

    (p:Person)-[r:IS_MANAGED_BY]->(m:Manager)-[r2:OVERSEES]->(d:Department) RETURN p.name, CASE p.role WHEN 'management' THEN d.departmentPhone WHEN 'business' THEN p.businessPhone WHEN 'technical' THEN p.emailAddress ELSE d.departmentEmail END as personContact • 2. Multiple conditional statements expressed MATCH (p:Person) RETURN p.name, CASE WHEN dateHired is null THEN 'candidate' WHEN dateHired > date(‘2020–09–17') THEN 'newHire' ELSE 'employee' END as personStatus, CASE WHEN dateFired is null THEN dateHired WHEN dateHired is null THEN entryDate ELSE 'n/a' END as leadDate
  21. In Action!

  22. Cypher Temporals

  23. Pitfalls • Date format • ISO 8601 • Pesky literal

    'T' • Durations • Precision • Groups Credit: unsplash.com
  24. Date format • ISO 8601 standard • Date: 2021-09-17 •

    Datetime: 2021-09-17T09:00:15Z • Literal 'T' between date and time values Credit: unsplash.com
  25. Truncate temporals • Ghosting any smaller units (defaults) • Helpful

    for generic searches or defaulting chunks of time • Date search for current year • Activity in speci fi c month • Any temporal type accepts .truncate() • Example: WITH date() as toda y RETURN date.truncate('month',today);
  26. In Action!

  27. Translating to ISO 8601 • Cypher accepts: • ISO 8601

    • Strings in ISO 8601 format • What if you have ANYTHING else? • Error!
  28. APOC to the rescue!

  29. What is APOC? • Utility library for Neo4j • Widely

    used, broadly applicable • 550+ procedures and functions Photo by Alexis Fauvet on Unsplash
  30. Epoch time • Unix system date/time • Processes, logs, etc.

    • Seconds since 1970-01-01T00:00:00Z • Example: 1631889000 (end time for this session) • Handled with apoc.date.format() or apoc.date.toISO8601() https://www.epochconverter.com/
  31. In Action!

  32. Date Strings • Process: • Temporal string -> ISO 8601

    string -> Neo4j temporal • Any temporal string with speci fi ed format • Handled with apoc.date.convertFormat() or apoc.temporal.toZonedTemporal()
  33. In Action!

  34. Cypher Temporal Durations

  35. Durations • Distance in time measurements • Literal ‘P’ and

    ’T’ in syntax • Precision - months, days, seconds • P1M3D60000S • Duration functions (between, inMonths, inDays, inSeconds) Photo by Randy Fath on Unsplash
  36. In Action!

  37. Duration conversions • Duration functions: inMonths, inDays, inSeconds • Most

    components -> whole values only, no remainders • Convert between units: functions + components • Component groups
  38. Components

  39. In Action!

  40. More APOC

  41. Examples WITH datetime(‘2021-09-30T23:59:59') as eo m RETURN apoc.date. fi elds(apoc.temporal.format(eom,

    'yyyy-MM-dd HH:mm:ss'),'yyyy- MM-dd HH:mm:ss'); WITH datetime() as no w RETURN apoc.date.toYears(now.epochMillis), duration.inMonths(date('1970-01-01'), localDatetime(now)).years;
  42. Eager operator

  43. What is Eager? • Eager loading • Consistency and con

    fl icts • Operations occur to all rows before continuing • Avoid read/write con fl icts Credit: unsplash.com
  44. What’s the difference? Non-Eager Eager Row-by-row Operation-by-operation

  45. Does it matter? • Low memory/heap • Large dataset •

    Query performance Credit: unsplash.com
  46. In Action!

  47. How to avoid eager? • Might not be an issue

    for smaller operations/data sets • Separate operations • Avoids situations where con fl icts might occur • Use PROFILE/EXPLAIN on queries • Having trouble? Ask for help! • dev.neo4j.com/forum -> Cypher channel
  48. Recap!

  49. CASE reminders • Uses: • Compare against values • Multiple

    conditionals • NOT: • Programming language logic • Variable-setting Photo by JESHOOTS.COM on Unsplash
  50. Temporals reminders • Formats: • Temporal/string in ISO 8601 •

    Other must be translated • Durations: • Groupings (months, days, seconds) • Component groups for translations Photo by JESHOOTS.COM on Unsplash
  51. Eager reminders • Data set: • Large set • Heavy

    processing (i.e. cleaning, operations, etc) • AND queries slow • To do: • Separate operations • PROFILE/EXPLAIN is your friend! Photo by JESHOOTS.COM on Unsplash
  52. Resources • Blog posts: CASE, temporals, eager, and more! •

    jmhreif.com/blog/ • Repository: • github.com/JMHReif/cypher-sleuthing • Ask for help! • dev.neo4j.com/forum -> Cypher channel Credit: unsplash.com Jennifer Reif Email: jennifer.reif@neo4j.com Twitter: @JMHReif LinkedIn: linkedin.com/in/jmhreif Github: GitHub.com/JMHReif Website: jmhreif.com