NEO4J ESSENTIALS
Tuna VARGI
DOWNLOAD NEO4J
http://neo4j.com/download/
Why graphs?
What is graph ?
USE CASES
SOCIAL NETWORK
NETWORK ANALYSIS
ROUTE FINDING
RECOMMENDATIONS
SECURITY ANALYSIS
ETC
WHAT IS GRAPH ?
BLOCKS
- Nodes
- Relationships
- Properties
- Labels
RELATIONAL DB
GRAPH DB
CYPHER
a pattern-matching query language for graphs
DECLARATIVE
PATTERN MATCHING
EXPRESSIVE
CLAUSES
GENERAL CLAUSES
- RETURN
- ORDER BY
- LIMIT
READING CLAUSES
- MATCH
- OPTIONAL MATCH
- WHERE
- WITH
- AGGREGATION
WRITE CLAUSES
- CREATE
- MERGE
- SET
- DELETE
- REMOVE
QUERY STRUCTURE
MOVIES
PERSON
RELATIONSHIPS
- ACTED_IN
- DIRECTED
- FOLLOWS
- PRODUCED
- REVIEWED
- WROTE
QUERY EXAMPLES
MATCH (m:Movie)
WHERE m.released=1992
RETURN m
MATCH (m:Movie), (p: Person)
MATCH path=(m)<-[:ACTED_IN]-(p)
WHERE m.released=1992
return path
MATCH (m:Movie), (p: Person {name: "Tom Hanks"})
MATCH (p)-[:DIRECTED]->(m)
return m
MATCH (m:Movie)<-[:ACTED_IN]-(a:Person)
RETURN m.title as movie, collect(a.name) as cast
LIMIT 20
MATCH (p: Person {name: "Tom Hanks"})
MATCH (p)-[:DIRECTED]->(m:Movie)
WITH distinct m
return count(m)
CHROMA
WHAT WAS OUR PROBLEM ?
INFINITE CATEGORY TREE
PHOTOS ATTACHED TO CATEGORIES
GIVE ME THE LATEST PHOTO IN A CATEGORY TREE
MATCH (category:Category{id:'5294ecd1389a5e44303bd774'})
MATCH (category)-[:CHILD_OF*0..50]->(subcats: Category)<-[:PHOTO_OF]-(photo: Photo)
with photo
order by photo.created_at
return last(collect(photo.id)) as last_photo_id
WE NEED THE LATEST PHOTO IN A CATEGORY TREE BELONG TO PROVIDED USER
COMPLEX AGGREGATION
MATCH (cat_user:User {id:"51e52cfc389a5e35fa403436"}),
(category:Category)
WHERE category.parent_id='0'
MATCH (category)-[:CHILD_OF*0..50]->(subcats)
WITH distinct subcats as dsubcats, category, cat_user
MATCH (dsubcats)<-[:PHOTO_OF]-(photo:Photo)-[:OWNER]->(cat_user)
with photo, category, cat_user
order by photo.created_at
return distinct category.id, last(collect(photo.id)) as last_photo_id, count(distinct photo) as photo_count, category.name
GIVE ME THE PHOTOS CONNECTED TO PLACES NEAR LAT 22.7 LON 79.10
START place=node:geom('withinDistance:[22.7, 79.10, 100.0]')
where place.type='Place'
MATCH (place)-[:CHILD_OF*0..50]->(subplaces:Place)
with distinct subplaces, place
MATCH (subplaces)<-[:TAKEN_AT]-(photo:Photo)
return place.name, photo
GIVE ME THE COUNT OF PHOTOS IN NORTH AMERICA FOR WILD ANIMALS
MATCH (place: Place{id: '5571b6a691201b05075468f8'})
MATCH (category: Category{id: '529500b0389a5e442fd8fcad'})
MATCH (place)-[:CHILD_OF*0..10]->(subplaces)<-[:TAKEN_AT]-(photo:Photo)
MATCH (category)-[:CHILD_OF*0..10]->(subcategories: Category)<-[:PHOTO_OF]-(photo: Photo)
return count(distinct photo)
THANKS
NEO4J ESSENTIALS
By Tuna Vargı
NEO4J ESSENTIALS
- 2,707