SlideShare una empresa de Scribd logo
1 de 37
Cypher Query
Language
Chicago Graph Database Meet-Up
Max De Marzi
Updated for Neo4j 2.x by Brian Underwood
What is Cypher?
•Graph Query Language for
Neo4j
•Aims to make querying simple
Motivation
Why Cypher?
• Existing Neo4j query mechanisms were
not simple enough
• Too verbose (Java API)
• Too prescriptive (Gremlin)
Motivation
SQL?
• Unable to express paths
• these are crucial for graph-based
reasoning
• Neo4j is schema/table free
Design Decisions
Pattern matching
Design Decisions
Pattern matching
A
B C
Design Decisions
Pattern matching
Design Decisions
Pattern matching
Design Decisions
Pattern matching
Design Decisions
Pattern matching
Design Decisions
ASCII-art patterns
() --> ()
Design Decisions
Directed relationship
(A) --> (B)
A B
Design Decisions
Undirected relationship
(A) -- (B)
A B
Design Decisions
specific relationships
A -[:LOVES]-> B
A B
LOVE
S
Design Decisions
Joined paths
A --> B --> C
A B C
Design Decisions
multiple paths
A --> B --> C, A --> C
A
B C
A --> B --> C <-- A
Design Decisions
Variable length paths
A -[*]-> B
A B
A B
A B
...
Design Decisions
Familiar for SQL users
select
from
where
group
by
order by
match
where
return
MATCH
SELECT *
FROM people
WHERE people.firstName = “Max”
MATCH (max:Person {firstName: ‘Max’})
RETURN max
MATCH (max:Person)
WHERE max.firstName = ‘Max’
RETURN max
MATCH
SELECT skills.*
FROM users
JOIN skills ON users.id = skills.user_id
WHERE users.first_name = ‘Max’
MATCH (user:User {firstName: ‘Max’}) -->
(skill:Skill)
RETURN skill
OPTIONAL MATCH
SELECT skills.*
FROM users
LEFT JOIN skills ON users.id = skills.user_id
WHERE users.first_name = ‘Max’
MATCH (user:User {firstName: ‘Max’})
OPTIONAL MATCH user –-> (skill:Skill)
RETURN skill
SELECT skills.*, user_skill.*
FROM users
JOIN user_skill ON users.id = user_skill.user_id
JOIN skills ON user_skill.skill_id = skill.id
WHERE users.first_name = ‘Max’
MATCH (user:User {firstName: ‘Max’})-
[user_skill]-> (skill:Skill)
RETURN skill, user_skill
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;
Complicated Match
Some UGLY recursive self join on the
groups table
MATCH group <-[:BELONGS_TO*]- (max:Person
{name: ‘Max’})
RETURN group
Where
SELECT person.*
FROM person
WHERE person.age >32
OR person.hair = "bald"
MATCH (person:Person)
WHERE person.age > 32 OR person.hair =
"bald"
RETURN person
Return
SELECT people.name, count(*)
FROM people
GROUP BY people.name
ORDER BY people.name
MATCH (person:Person)
RETURN person.name, count(*)
ORDER BY person.name
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 me.name, friends.name, fof.name, fofof.name, count(others)
ORDER BY friends.name, fof.name, fofof.name, count(others) DESC
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
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), me.name, count(r), min(r.date), max(r.date)
ORDER BY ID(me)
Functions
Collect: put aggregated values in a list
MATCH (a:User)-[:follows]->b
RETURN a.name, collect(b.name)
Each result row contains a name for each user
and a list of names which that user follows
Combine Functions
Collect the ID of friends
MATCH (me:User)<-[r:wrote]-(friends)
RETURN ID(me), me.name, collect(ID(friends)), collect(r.date)
ORDER BY ID(me)
Uses
Recommend Friends
MATCH (me)-[:friends]->(friend)-[:friends]->(foaf)
WHERE ID(me) = {node_id}
RETURN foaf.name
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) : person.name)
Length: counts the number of nodes along a path
Extract: gets the nodes/relationships from a path
http://thought-bytes.blogspot.com/2012/02/similarity-
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
START london = node(1), moscow = node(2)
MATCH path = london -[*]-> moscow
WHERE all(city in nodes(path) where city.capital = 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
Thanks for Listening!
Questions?
maxdemarzi.com

Más contenido relacionado

Similar a Intro to Cypher

The openCypher Project - An Open Graph Query Language
The openCypher Project - An Open Graph Query LanguageThe openCypher Project - An Open Graph Query Language
The openCypher Project - An Open Graph Query Language
Neo4j
 
Applied Redis
Applied RedisApplied Redis
Applied Redis
hotrannam
 
Football graph - Neo4j and the Premier League
Football graph - Neo4j and the Premier LeagueFootball graph - Neo4j and the Premier League
Football graph - Neo4j and the Premier League
Mark Needham
 

Similar a Intro to Cypher (20)

Cypher
CypherCypher
Cypher
 
Path Pattern Queries: Introducing Regular Path Queries in openCypher
Path Pattern Queries: Introducing Regular Path Queries in openCypherPath Pattern Queries: Introducing Regular Path Queries in openCypher
Path Pattern Queries: Introducing Regular Path Queries in openCypher
 
Intro to Cypher
Intro to CypherIntro to Cypher
Intro to Cypher
 
managing big data
managing big datamanaging big data
managing big data
 
Cypher and apache spark multiple graphs and more in open cypher
Cypher and apache spark  multiple graphs and more in  open cypherCypher and apache spark  multiple graphs and more in  open cypher
Cypher and apache spark multiple graphs and more in open cypher
 
The openCypher Project - An Open Graph Query Language
The openCypher Project - An Open Graph Query LanguageThe openCypher Project - An Open Graph Query Language
The openCypher Project - An Open Graph Query Language
 
Hands on Training – Graph Database with Neo4j
Hands on Training – Graph Database with Neo4jHands on Training – Graph Database with Neo4j
Hands on Training – Graph Database with Neo4j
 
Applied Redis
Applied RedisApplied Redis
Applied Redis
 
Football graph - Neo4j and the Premier League
Football graph - Neo4j and the Premier LeagueFootball graph - Neo4j and the Premier League
Football graph - Neo4j and the Premier League
 
Graph Database Use Cases - StampedeCon 2015
Graph Database Use Cases - StampedeCon 2015Graph Database Use Cases - StampedeCon 2015
Graph Database Use Cases - StampedeCon 2015
 
Graph database Use Cases
Graph database Use CasesGraph database Use Cases
Graph database Use Cases
 
Graph Databases in the Microsoft Ecosystem
Graph Databases in the Microsoft EcosystemGraph Databases in the Microsoft Ecosystem
Graph Databases in the Microsoft Ecosystem
 
Introduction to SQL Server Graph DB
Introduction to SQL Server Graph DBIntroduction to SQL Server Graph DB
Introduction to SQL Server Graph DB
 
The 2nd graph database in sv meetup
The 2nd graph database in sv meetupThe 2nd graph database in sv meetup
The 2nd graph database in sv meetup
 
Neo4j: Import and Data Modelling
Neo4j: Import and Data ModellingNeo4j: Import and Data Modelling
Neo4j: Import and Data Modelling
 
Neo4j Introduction (for Techies)
Neo4j Introduction (for Techies)Neo4j Introduction (for Techies)
Neo4j Introduction (for Techies)
 
Using Neo4j from Java
Using Neo4j from JavaUsing Neo4j from Java
Using Neo4j from Java
 
Neo4j Graph Database และการประยุกตร์ใช้
Neo4j Graph Database และการประยุกตร์ใช้Neo4j Graph Database และการประยุกตร์ใช้
Neo4j Graph Database และการประยุกตร์ใช้
 
Morpheus SQL and Cypher® in Apache® Spark - Big Data Meetup Munich
Morpheus SQL and Cypher® in Apache® Spark - Big Data Meetup MunichMorpheus SQL and Cypher® in Apache® Spark - Big Data Meetup Munich
Morpheus SQL and Cypher® in Apache® Spark - Big Data Meetup Munich
 
Morpheus - SQL and Cypher in Apache Spark
Morpheus - SQL and Cypher in Apache SparkMorpheus - SQL and Cypher in Apache Spark
Morpheus - SQL and Cypher in Apache Spark
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 

Intro to Cypher