Facebook Graph Search with Cypher and Neo4j

56
Facebook Graph Search with Neo4j Max De Marzi Monday, April 29, 13

description

How to build your own Facebook Graph Search with Cypher and Neo4j. Demo: neographsearch.herokuapp.com Blog post: http://maxdemarzi.com/2013/01/28/facebook-graph-search-with-cypher-and-neo4j/ Github: https://github.com/maxdemarzi/neo_graph_search

Transcript of Facebook Graph Search with Cypher and Neo4j

Page 1: Facebook Graph Search with Cypher and Neo4j

Facebook Graph Search with Neo4j

Max De Marzi

Monday, April 29, 13

Page 2: Facebook Graph Search with Cypher and Neo4j

Can I Haz?Monday, April 29, 13

Page 3: Facebook Graph Search with Cypher and Neo4j

NopeMonday, April 29, 13

Page 4: Facebook Graph Search with Cypher and Neo4j

#9,998,383,750,000

Monday, April 29, 13

Page 5: Facebook Graph Search with Cypher and Neo4j

Can I make my own?

Monday, April 29, 13

Page 6: Facebook Graph Search with Cypher and Neo4j

MaybeMonday, April 29, 13

Page 7: Facebook Graph Search with Cypher and Neo4j

What do I know about

NLP?Monday, April 29, 13

Page 8: Facebook Graph Search with Cypher and Neo4j

NothingMonday, April 29, 13

Page 9: Facebook Graph Search with Cypher and Neo4j

What do I know?

Monday, April 29, 13

Page 10: Facebook Graph Search with Cypher and Neo4j

Monday, April 29, 13

Page 11: Facebook Graph Search with Cypher and Neo4j

andMonday, April 29, 13

Page 12: Facebook Graph Search with Cypher and Neo4j

CypherMonday, April 29, 13

Page 13: Facebook Graph Search with Cypher and Neo4j

I can query a graph

Monday, April 29, 13

Page 14: Facebook Graph Search with Cypher and Neo4j

How?Monday, April 29, 13

Page 15: Facebook Graph Search with Cypher and Neo4j

����������������� ���������������

Matching PatternsMonday, April 29, 13

Page 16: Facebook Graph Search with Cypher and Neo4j

ASCII Art

Monday, April 29, 13

Page 17: Facebook Graph Search with Cypher and Neo4j

() --> ()

ASCII Art

Monday, April 29, 13

Page 18: Facebook Graph Search with Cypher and Neo4j

Named Nodes

Monday, April 29, 13

Page 19: Facebook Graph Search with Cypher and Neo4j

(A) --> (B)

Named Nodes

Monday, April 29, 13

Page 20: Facebook Graph Search with Cypher and Neo4j

LOVES

Typed Relationships

Monday, April 29, 13

Page 21: Facebook Graph Search with Cypher and Neo4j

A -[:LOVES]-> B

LOVES

Typed Relationships

Monday, April 29, 13

Page 22: Facebook Graph Search with Cypher and Neo4j

Describing a Path

Monday, April 29, 13

Page 23: Facebook Graph Search with Cypher and Neo4j

A --> B --> C

Describing a Path

Monday, April 29, 13

Page 24: Facebook Graph Search with Cypher and Neo4j

A

B C

Multiple Paths

Monday, April 29, 13

Page 25: Facebook Graph Search with Cypher and Neo4j

A --> B --> C, A --> C

A

B C

Multiple Paths

Monday, April 29, 13

Page 26: Facebook Graph Search with Cypher and Neo4j

A --> B --> C, A --> C

A

B C

A --> B --> C <-- A

Multiple Paths

Monday, April 29, 13

Page 27: Facebook Graph Search with Cypher and Neo4j

The START Clause

Monday, April 29, 13

Page 28: Facebook Graph Search with Cypher and Neo4j

The START Clause

START me=node(1)RETURN me

Monday, April 29, 13

Page 29: Facebook Graph Search with Cypher and Neo4j

with an Index

Monday, April 29, 13

Page 30: Facebook Graph Search with Cypher and Neo4j

with an Index

START me=node:Users(name=‘Max’)RETURN me

Monday, April 29, 13

Page 31: Facebook Graph Search with Cypher and Neo4j

The MATCH Clause

Monday, April 29, 13

Page 32: Facebook Graph Search with Cypher and Neo4j

The MATCH Clause

START me=node:Users(name=‘Max’)MATCH me -[:friends]-> peopleRETURN people

Monday, April 29, 13

Page 33: Facebook Graph Search with Cypher and Neo4j

My friends who like cheese

Monday, April 29, 13

Page 34: Facebook Graph Search with Cypher and Neo4j

My friends who like cheese

START me=node:Users(name=‘Max’) thing=node:Things(name=‘Cheese’)MATCH me -[:friends]-> people -[:like]-> thingRETURN people

Monday, April 29, 13

Page 35: Facebook Graph Search with Cypher and Neo4j

My friends who like cheese

Monday, April 29, 13

Page 36: Facebook Graph Search with Cypher and Neo4j

My friends who like cheese

START me=node:Users(name=‘Max’) thing=node:Things(name=‘Cheese’)MATCH me -[:friends]-> people, people -[:like]-> thingRETURN people

Monday, April 29, 13

Page 37: Facebook Graph Search with Cypher and Neo4j

My friends who like ?

Monday, April 29, 13

Page 38: Facebook Graph Search with Cypher and Neo4j

My friends who like ?START me=node({me}), thing=node:Things({thing})MATCH me -[:friends]-> people, people -[:like]-> thingRETURN people

Params :{“me”: 1, “thing”: “name: cheese”}

Monday, April 29, 13

Page 39: Facebook Graph Search with Cypher and Neo4j

My friends who like ? and ?

Monday, April 29, 13

Page 40: Facebook Graph Search with Cypher and Neo4j

My friends who like ? and ?

START me=node({me}), thing1=node:Things({thing1}), thing2=node:Things({thing2})MATCH me -[:friends]-> people, people -[:like]-> thing1, people -[:like]-> thing2RETURN peopleParams :{“me”: 1, “thing1”: “name: cheese”, “thing2”: “name: wine”}

Monday, April 29, 13

Page 41: Facebook Graph Search with Cypher and Neo4j

I need to build a Cypher Query

Monday, April 29, 13

Page 42: Facebook Graph Search with Cypher and Neo4j

SEMR

✦ Gateway drug to NLP

✦ 4 years old

✦ Didn’t work on my Mac

✦ Pointed me to Treetop

Monday, April 29, 13

Page 43: Facebook Graph Search with Cypher and Neo4j

Treetop✦ Create a Grammar by

making some Rules

✦ Turn expression into Syntax Tree

✦ Build custom Syntax Nodes

✦ Prune the tree

✦ to_cypher

Monday, April 29, 13

Page 44: Facebook Graph Search with Cypher and Neo4j

Friends Rule

Monday, April 29, 13

Page 45: Facebook Graph Search with Cypher and Neo4j

Friends Rule

rule friends “friends” <Friends>end

Monday, April 29, 13

Page 46: Facebook Graph Search with Cypher and Neo4j

friends to_cypher

Monday, April 29, 13

Page 47: Facebook Graph Search with Cypher and Neo4j

friends to_cypher class Friends < Treetop::Runtime::SyntaxNode

def to_cypher

return {:start => "me = node({me})",

:match => "me -[:friends]-> people",

:return => "people",

:params => {"me" => nil }}

end

end

Monday, April 29, 13

Page 48: Facebook Graph Search with Cypher and Neo4j

Likes Rule

Monday, April 29, 13

Page 49: Facebook Graph Search with Cypher and Neo4j

Likes Rule

rule likes "who like" <Likes>end

Monday, April 29, 13

Page 50: Facebook Graph Search with Cypher and Neo4j

likes to_cypher

Monday, April 29, 13

Page 51: Facebook Graph Search with Cypher and Neo4j

likes to_cypher class Likes < Treetop::Runtime::SyntaxNode

def to_cypher

return {:match => "people -[:likes]-> thing"}

end

end

Monday, April 29, 13

Page 52: Facebook Graph Search with Cypher and Neo4j

Thing Rule

Monday, April 29, 13

Page 53: Facebook Graph Search with Cypher and Neo4j

Thing Rule

rule thing [a-zA-Z0-9]+ <Thing>end

Monday, April 29, 13

Page 54: Facebook Graph Search with Cypher and Neo4j

thing to_cypher

Monday, April 29, 13

Page 55: Facebook Graph Search with Cypher and Neo4j

thing to_cypherclass Thing < Treetop::Runtime::SyntaxNode

def to_cypher

return {:start => "thing = node:things({thing})",

:params => {"thing" => "name: " + self.text_value } }

end

end

Monday, April 29, 13