Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

54
FACEBOOK GRAPH SEARCH How to create your own graph search using Neo4j jDays 2013 Ole-Martin Mørk 26/11/13

description

FACEBOOK GRAPH SEARCH, Presentation at jdays2013 www.jdays.se

Transcript of Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

Page 1: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

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
Page 3: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

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

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

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
Page 7: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg
Page 8: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg
Page 9: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg
Page 10: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg
Page 11: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg
Page 12: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg
Page 13: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

GRAF

LOVES

BETRAYS

KNOWS

KNOWS

KNOWS

Page 14: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

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

RELASJON

BODDE fra: til:

Page 16: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg
Page 17: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

RELASJONSDATABASER

Page 18: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

“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

Neo4j

Page 20: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

GRAF

A

D

C B

E

G

H

F

I

Page 21: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

CYPHER

Page 22: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

GRAF

A

D

C B

E

G

H

F

I

Page 23: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

Cypher

Page 24: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

CYPHER

( ) --> ( )

Page 25: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

CYPHER

(a) --> (b) a b

Page 26: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

CYPHER

(a)-->(b)<--(c) a b c

Page 27: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

CYPHER

(a) –[:kjenner]-> (b) a b kjenner

Page 28: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

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

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

Grammar

Page 31: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

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

TEXT PARSING

PEG CFG

Page 33: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

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

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

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

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

PARBOILED

Page 38: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

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

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

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

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

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

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

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

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

BEKK CV-db

Page 47: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

A search for Java

yields 224 hits!

Page 48: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg
Page 49: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

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

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

Demo

Page 52: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

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

?

Page 54: Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg

Thank you!

@olemartin