facebook graph search by ole martin mørk for jdays2013 gothenburg
DESCRIPTION
FACEBOOK GRAPH SEARCH, Presentation at jdays2013 www.jdays.seTRANSCRIPT
![Page 1: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/1.jpg)
FACEBOOK GRAPH SEARCH
How to create your own graph search using Neo4j
jDays 2013 Ole-Martin Mørk
26/11/13
![Page 2: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/2.jpg)
![Page 3: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/3.jpg)
FACEBOOK GRAPH SEARCH
How to create your own graph search using Neo4j
jDays 2013 Ole-Martin Mørk
26/11/13
![Page 4: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/4.jpg)
ABOUT ME
Ole-Martin Mørk Scientist Bekk Consulting AS Oslo, Norway twitter: olemartin
![Page 5: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/5.jpg)
INTRODUCTION TO SEARCH
INTRODUCTION TO NEO4J
INTRODUCTION TO PARSING
GRAPH SEARCH
AGENDA
![Page 6: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/6.jpg)
![Page 7: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/7.jpg)
![Page 8: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/8.jpg)
![Page 9: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/9.jpg)
![Page 10: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/10.jpg)
![Page 11: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/11.jpg)
![Page 12: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/12.jpg)
![Page 13: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/13.jpg)
GRAF
LOVES
BETRAYS
KNOWS
KNOWS
KNOWS
![Page 14: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/14.jpg)
NODE
PERSON
navn: Thomas alder: 24
ADRESSE
gate: Aker
nummer: 15
BODDE
![Page 15: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/15.jpg)
RELASJON
BODDE fra: til:
![Page 16: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/16.jpg)
![Page 17: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/17.jpg)
RELASJONSDATABASER
![Page 18: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/18.jpg)
“PATH EXISTS” RESPONSTID
- One database containing 1000 persons
- Max 50 friends
- Detect if two random persons are connected via friends
Neo4j 1.000.000 2ms Neo4j 1.000 2ms
Antall personer Responstid Relational db 1.000 2000ms
![Page 19: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/19.jpg)
Neo4j
![Page 20: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/20.jpg)
GRAF
A
D
C B
E
G
H
F
I
![Page 21: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/21.jpg)
CYPHER
![Page 22: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/22.jpg)
GRAF
A
D
C B
E
G
H
F
I
![Page 23: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/23.jpg)
Cypher
![Page 24: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/24.jpg)
CYPHER
( ) --> ( )
![Page 25: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/25.jpg)
CYPHER
(a) --> (b) a b
![Page 26: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/26.jpg)
CYPHER
(a)-->(b)<--(c) a b c
![Page 27: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/27.jpg)
CYPHER
(a) –[:kjenner]-> (b) a b kjenner
![Page 28: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/28.jpg)
CYPHER SØK
START person=node:person(name=“Ole-Martin”) START school=node:school(“name:Norw*”) START student=node:student(“year:(3 OR 4 OR 5)”)
![Page 29: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/29.jpg)
FACEBOOK GRAPH SEARCH WITH CYPHER
START me=node:person(name = “Ole-Martin”), location=node:location(location=“Göteborg”), cuisine=node:cuisine(cuisine=“Sushi”) MATCH (me)-[:IS_FRIEND_OF]->(friend)-[:LIKES]->(restaurant) -[:LOCATED_IN]->(location),(restaurant)-[:SERVES]->(cuisine) RETURN restaurant
![Page 30: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/30.jpg)
Grammar
![Page 31: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/31.jpg)
GRAMMAR
A “language” can be formally defined as “any system of formalized symbols, signs, etc. used for communication” A “grammar” can be defined as a “the set of structural rules that governs sentences, words, etc. in a natural language”
![Page 32: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/32.jpg)
TEXT PARSING
PEG CFG
![Page 33: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/33.jpg)
GRAMMAR
Alfred, who loved fishing, bought fish at the store downtown
(Alfred, (who loved (fishing)), (bought (fish (at (the store (downtown))))))
![Page 34: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/34.jpg)
additionExp! : multiplyExp! ( '+' multiplyExp! | '-' multiplyExp! )* ! ; !!multiplyExp! : atomExp! ( '*' atomExp! | '/' atomExp! )* ! ; !!atomExp! : Number ! | '(' additionExp ')' ! ; !!Number ! : ('0'..'9')+ ! ; !
An additionExp is defined as a multiplyExp + or - a multiplyExp
A multiplyExp is defined as an atomExp * or / an atomExp
An atomExp is defined as a number or a parenthesized additionExp
Number is one or more character between 0-9
![Page 35: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/35.jpg)
class CalculatorParser extends BaseParser<> { ! ! Rule Expression() { ! return Sequence( ! Term(), ! ZeroOrMore(AnyOf("+-"), Term()) ! ); ! } ! ! Rule Term() { ! return Sequence( ! Factor(), ! ZeroOrMore(AnyOf("*/"), Factor()) ! ); ! } ! ! Rule Factor() { ! return FirstOf( ! Number(), ! Sequence('(', Expression(), ')') ! ); ! } ! ! Rule Number() { ! return OneOrMore(CharRange('0', '9')); ! } ! } !
An expression is a sequence of Term followed by zero or more “+” or “-” followed by a Term
Term is a Factor followed by zero or more sequences of “*” or “/” followed by a factor
Factor is a number or a parenthesized expression
Number is a one or more characters between 0-9
![Page 36: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/36.jpg)
PEG VS CFG
PEGs firstof operator vs CFG’s | operator PEG does not have a separate tokenizing step
CFG might come across as more powerful, but also more difficult to master PEG does not allow ambiguity in the grammar
![Page 37: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/37.jpg)
PARBOILED
![Page 38: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/38.jpg)
Parsing expression grammars parser
Lightweight
Easy to use
Implementation in Scala and Java
Rules are written in the programming language
PARBOILED
![Page 39: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/39.jpg)
class CalculatorParser extends BaseParser<> { ! ! Rule Expression() { ! return Sequence( ! Term(), ! ZeroOrMore(AnyOf("+-"), Term()) ! ); ! } ! ! Rule Term() { ! return Sequence( ! Factor(), ! ZeroOrMore(AnyOf("*/"), Factor()) ! ); ! } ! ! Rule Factor() { ! return FirstOf( ! Number(), ! Sequence('(', Expression(), ')') ! ); ! } ! ! Rule Number() { ! return OneOrMore(CharRange('0', '9')); ! } ! } !
An expression is a sequence of Term followed by zero or more “+” or “-” followed by a Term
Term is a Factor followed by zero or more sequences of “*” or “/” followed by a factor
Factor is a number or a parenthesized expression
Number is a one or more characters between 0-9
![Page 40: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/40.jpg)
I went for a walk downtown
Sequence( “I”, “went”, “for”, “a”, “walk”, “downtown”)
![Page 41: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/41.jpg)
I went
for a walk downtown
Sequence( String(“I”), FirstOf(“went”, “wend”), Sequence(“for”, “a”, “walk”), FirstOf(“downtown”, “to the city”));
wend to the city
![Page 42: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/42.jpg)
I went
for a walk downtown
Sequence( …, Optional(String(“today”)));
walked to the city today
![Page 43: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/43.jpg)
Sequence( Today(), …, Today());
Rule Today() { return Optional(String(“today”)); }
I went
for a walk downtown
walked to the city today today
![Page 44: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/44.jpg)
Rule AnyOf(java.lang.String characters) Creates a new rule that matches any of the characters in the given string.
Rule Ch(char c) Explicitly creates a rule matching the given character.
Rule CharRange(char cLow, char cHigh) Creates a rule matching a range of characters from cLow to cHigh (both inclusively).
Rule FirstOf(java.lang.Object... rules) Creates a new rule that successively tries all of the given subrules and succeeds when the first one of its subrules matches.
Rule IgnoreCase(char... characters) Explicitly creates a rule matching the given string in a case-independent fashion.
Rule NoneOf(char... characters) Creates a new rule that matches all characters except the ones in the given char array and EOI.
Rule NTimes(int repetitions, java.lang.Object rule) Creates a new rule that repeatedly matches a given sub rule a certain fixed number of times.
!
![Page 45: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/45.jpg)
Sequence( …, FirstOf( Sequence(push(“downtown”), “downtown”, Sequence(push(“city”), “to the city”)));
I went
for a walk downtown
walked to the city today today
![Page 46: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/46.jpg)
BEKK CV-db
![Page 47: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/47.jpg)
A search for Java
yields 224 hits!
![Page 48: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/48.jpg)
![Page 49: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/49.jpg)
public Rule Expression() { ! return Sequence( ! Start(), ! FirstOf(People(), Projects(), Technologies()), ! OneOrMore( ! FirstOf( ! And(), ! Sequence( ! Know(), ! Subjects() ! ), ! Sequence( ! WorkedAt(), ! Customers() ! ), ! Sequence( ! Know(), ! Customers() ! ), ! Sequence( ! Know(), ! Technologies() !
") " ") " ") " ); !} !
![Page 50: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/50.jpg)
start !"fag=node:fag(navn = "Neo4J"), "fag1=node:fag(navn = "Java"), "prosjekt2=node:prosjekt(navn ="Modernisering") !
match !"CONSULTANTS -[:KAN]-> fag, !"CONSULTANTS -[:KAN]-> fag1, !"CONSULTANTS -[:KONSULTERTE]-> prosjekt !
return !"distinct CONSULTANTS !
!
![Page 51: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/51.jpg)
Demo
![Page 52: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/52.jpg)
LEARN MORE
graphdatabases.com neo4j.org bit.ly/neo-cyp parboiled.org
![Page 53: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/53.jpg)
?
![Page 54: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg](https://reader033.vdocuments.net/reader033/viewer/2022060203/559e057e1a28abab428b45a4/html5/thumbnails/54.jpg)
Thank you!
@olemartin