exploring cypher with graph gists

33
Exploring the Cypher Query Language through GraphGists Luanne Misquitta @luannem

Upload: luanne-misquitta

Post on 27-Jan-2015

117 views

Category:

Technology


2 download

DESCRIPTION

Talk at the Great Indian Developer Summit(http://www.developermarch.com/developersummit/conference.html) 2014, Bangalore.

TRANSCRIPT

Page 1: Exploring cypher with graph gists

Exploring the Cypher Query Language through GraphGists

Luanne Misquitta @luannem

Page 2: Exploring cypher with graph gists

Our Path

Neo4j Cypher ExamplesGraphGists

Page 3: Exploring cypher with graph gists

What if…

Credit: Shantaram Waingankar

Page 4: Exploring cypher with graph gists

Or

Credit: Luanne M.

Page 5: Exploring cypher with graph gists

Or

Credit: Florent Biville

Page 6: Exploring cypher with graph gists

Or

6Credit: Luanne M.

Page 7: Exploring cypher with graph gists

All you need is…

According to The Beatles

Page 8: Exploring cypher with graph gists

But that’s not why you’re here

Page 9: Exploring cypher with graph gists

What you really need is… a Graph

Page 10: Exploring cypher with graph gists

What is Neo4j?

• A Graph Database • A labelled property graph • Nodes with properties and labels (both optional) • Directed and named relationships with properties

(optional) • ACID • Schema free • Scalable: Billions of nodes and relationships • Fast: More than 2 million traversals per second • Suited for highly connected and complex data

Page 11: Exploring cypher with graph gists

What is Neo4j

name:Neo

name: Agent Smith

name: Cypher

name: Morpheus

name:TrinityLoves

KnowsKnows {since: 1999}

Knows

Knows

CrewCrew

Crew

CrewMatrixMatrix

Page 12: Exploring cypher with graph gists

Cypher

• Declarative graph query language • Based on English prose and neat

iconography • Focuses on

• What to retrieve from the graph and not how to retrieve it

• Pattern matching • Your domain instead of database access

• Humane query language, suitable for developers and operations professionals

Page 13: Exploring cypher with graph gists

Patterns and picturesNeo Trinity

LovesNeo loves Trinity

(Neo)-[:LOVES]->(Trinity)

Cypher Agent Smith

KnowsCypher knows Agent Smith

(Cypher)-[:KNOWS]->(Agent Smith)

Page 14: Exploring cypher with graph gists

Pattern matching

14

MATCH (me:User {name:’Luanne’})-[f:FRIEND_OF]->(friend)-[:LISTENS_TO]->(artist)!WHERE NOT (me-[:LISTENS_TO]->(artist) AND f.similarity>0.7!RETURN artist

Luanne

The Beatles

F1

Aerosmith

F2

Pink Floyd

FRIEND_OF {similarity=0.9}

FRIEND_OF {similarity=0.4}

LISTENS_TO

LISTENS_TO

LISTENS_TOLISTENS_TO

Page 15: Exploring cypher with graph gists

Example Query Structure

15

MATCH (u:User)-[:LISTENS_TO]->(a:Artist) !WHERE a.genre=“Rock” !WITH u, a, count(a) as artistCount !MATCH (a)-[r:PLAYING_IN]->(city) !WHERE r.ticketsAvailable=true !RETURN u,a,city.name as cityName !ORDER BY cityName LIMIT 5

Page 16: Exploring cypher with graph gists

MATCH

• Allows you to specify patterns that Cypher will search for in the graph

16

Page 17: Exploring cypher with graph gists

Match

17

MATCH (u:User)-[:LISTENS_TO]->(a:Artist) !WHERE a.genre=“Rock” !WITH u, a, count(a) as artistCount !MATCH (a)-[r:PLAYING_IN]->(city) !WHERE r.ticketsAvailable=true !RETURN u,a,city.name as cityName !ORDER BY cityName LIMIT 5

Page 18: Exploring cypher with graph gists

Where

• Filters the results • Adds constraints to the pattern described in

MATCH

18

Page 19: Exploring cypher with graph gists

Where

19

MATCH (u:User)-[:LISTENS_TO]->(a:Artist) !WHERE a.genre=“Rock” !WITH u, a, count(a) as artistCount !MATCH (a)-[r:PLAYING_IN]->(city) !WHERE r.ticketsAvailable=true !RETURN u,a,city.name as cityName !ORDER BY cityName LIMIT 5

Page 20: Exploring cypher with graph gists

Return

• Returns results

20

Page 21: Exploring cypher with graph gists

Return

21

MATCH (u:User)-[:LISTENS_TO]->(a:Artist) !WHERE a.genre=“Rock” !WITH u, a, count(a) as artistCount !MATCH (a)-[r:PLAYING_IN]->(city) !WHERE r.ticketsAvailable=true !RETURN u,a,city.name as cityName !ORDER BY cityName LIMIT 5

Page 22: Exploring cypher with graph gists

Order By, Limit

• Sort the output • Return a subset

22

Page 23: Exploring cypher with graph gists

Order By, Limit

23

MATCH (u:User)-[:LISTENS_TO]->(a:Artist) !WHERE a.genre=“Rock” !WITH u, a, count(a) as artistCount !MATCH (a)-[r:PLAYING_IN]->(city) !WHERE r.ticketsAvailable=true !RETURN u,a,city.name as cityName !ORDER BY cityName LIMIT 5

Page 24: Exploring cypher with graph gists

WITH

• Manipulate the result sequence before it is passed on to the following query parts.

• The manipulations can be of the shape and/or number of entries in the result set.

• Combines queries

24

Page 25: Exploring cypher with graph gists

With

25

MATCH (u:User)-[:LISTENS_TO]->(a:Artist) !WHERE a.genre=“Rock” !WITH u, a, count(a) as artistCount !MATCH (a)-[r:PLAYING_IN]->(city) !WHERE r.ticketsAvailable=true !RETURN u,a,city.name as cityName !ORDER BY cityName LIMIT 5

Page 26: Exploring cypher with graph gists

GraphGists

Page 27: Exploring cypher with graph gists

What’s a Gist?

27

• A simple way to share snippets of code or data

• Automatically versioned, forkable, usable from Git

• Nicely rendered and presented

Page 28: Exploring cypher with graph gists

Okay, why?

• How do you share your graph model? • Whiteboard? • Document? • How do you demonstrate queries? • Need a live database? • Modelling is incremental

28

Page 29: Exploring cypher with graph gists

What’s a GraphGist?

29

• An AsciiDoc file with: • A graph domain model • Descriptive text and pictures • Example queries against the model • Interactive and executable Cypher queries • A Neo4j console for further exploration

• Nicely rendered and presented

Page 30: Exploring cypher with graph gists

GraphGist demos• Trek and mountaineering routing

http://gist.neo4j.org/?09520d20fbe707951e1b @shantaramw

• Organization learninghttp://gist.neo4j.org/?8021754 @luannem

• Doctor Finder http://gist.neo4j.org/?8748719 @fbiville

• Bombay Railway Routes http://gist.neo4j.org/?8159102 @luannem 30

Page 31: Exploring cypher with graph gists

Resources

Page 32: Exploring cypher with graph gists

32

Neo4j http://www.neo4j.org/ !Explore and create your own GraphGist http://www.neo4j.org/learn/graphgist !Online Training http://www.neo4j.org/learn/online_course

Spreading Graph Love

Page 33: Exploring cypher with graph gists