Build a recommendation
engine with Neo4j
Logistics
Wi-fi
network: CodeNode
password: welovecode
Grab a USB key from one of the tables
Install Neo4j 3.x
Copy the data directory and apoc.jar onto
your machine
Join: meetup.com/graphdb-london
Join neo4j.com/slack #london-reco-201609
Introducing our data set...
meetup.coms recommendations
Recommendation queries
Several different types
groups to join
topics to follow
events to attend
As a user of meetup.com trying to find
groups to join and events to attend
The data
meetup.com/meetup_api/
What data do we have?
Groups
Members
Events
Topics
Time & Date
Location
Find similar groups to Neo4j
As a member of the Neo4j London group
I want to find other similar meetup groups
So that I can join those groups
What makes groups similar?
Recommend groups by topic
Open your browser to http://localhost:7474
Type the following command:
:play http://guides.neo4j.com/reco/file
Follow Along
Run the first guide with us
Take Note
Indexes and Constraints
Unique constraints
We create unique constraints to:
ensure uniqueness
allow fast lookup of nodes which match
these label/property pairs.
Unique constraints
We create unique constraints to:
ensure uniqueness
allow fast lookup of nodes which match
these label/property pairs.
CREATE CONSTRAINT ON (t:Topic)
ASSERT t.id IS UNIQUE
Indexes
We create indexes to:
allow fast lookup of nodes which match
these label/property pairs.
Indexes
We create indexes to:
allow fast lookup of nodes which match
these label/property pairs.
CREATE INDEX ON :Group(name)
Indexes
The following are index backed:
Equality
STARTS WITH
CONTAINS,
ENDS WITH
Range searches
(Non-)existence checks
How does Neo4j use indexes?
Indexes are only used to find the starting
point for queries.
Graph
Relational
Use index scans to look up
rows in tables and join them
with rows from other tables
Use indexes to find the starting
points for a query.
Continue
Continue with the Guide
Exercise
Explore the Graph
Solution
Explore the Graph
Explore the graph
Type the following command into the Neo4j
browser to see the answers:
:play http://guides.neo4j.com/reco/answers/1.html
Continue
Continue with the Guide
Next Step
Group Membership
Exclude groups Im a member of
As a member of the Neo4j London group
I want to find other similar meetup groups
that Im not already a member of
So that I can join those groups
Group memberships
Next Guide
Group Membership
Watch Out
Transactions & WITH
Periodic Commit
Cypher keeps all transaction state in memory
while running a query which is fine most of the
time.
Periodic Commit
Cypher keeps all transaction state in memory
while running a query which is fine most of the
time.
But when using LOAD CSV, this state can get
very large and may result in an OutOfMemory
exception.
Periodic Commit
// defaults to 1000
USING PERIODIC COMMIT
LOAD CSV
...
Periodic Commit
// defaults to 1000
USING PERIODIC COMMIT 10000
LOAD CSV
...
WITH
The WITH clause allows query parts to be chained
together, piping the results from one to be used
as starting points or criteria in the next.
WITH
Its used to:
limit the number of entries that are then
passed on to other MATCH clauses.
filter on aggregated values
separate reading from updating of the graph
Continue
Continue with the Guide
Exercise
Find yourself and your groups
Solution
Find yourself and your groups
Explore the graph
Type the following command into the Neo4j
browser to see the answers:
:play http://guides.neo4j.com/reco/answers/2.html
Continue
Continue with the Guide
Find my similar groups
As a member of several meetup groups
I want to find other similar meetup groups
that Im not already a member of
So that I can join those groups
Next Step
Member Interest
Member interests
Attention
Lists with split & UNWIND
Splitting up topic ids
The split function lets us convert a string
into a string array based on a delimiting
character.
Splitting up topic ids
The split function lets us convert a string
into a string array based on a delimiting
character.
RETURN split("1;2;3", ";") AS topicIds
[1, 2, 3]
Splitting up topic ids
We can use UNWIND to explode any array or list
back into individual rows.
Splitting up topic ids
We can use UNWIND to explode the resulting
array back into individual rows.
UNWIND [1,2,3] AS value
RETURN value
1
2
3
Splitting up topic ids
UNWIND split("1;2;3", ";") AS topicId
RETURN topicId
1
2
3
Continue
Continue with the Guide
Exercise
My inferred interests
Solution
My inferred interests
My inferred interests
Type the following command into the Neo4j
browser to see the answers:
:play http://guides.neo4j.com/reco/answers/3.html
Continue
Continue with the Guide
Next Guide
Events
Exercise
Event recommendations
Solution
Event recommendations
Event recommendations
Type the following command into the Neo4j
browser to see the answers:
:play http://guides.neo4j.com/reco/4.html
Continue
Continue with the Guide
Next Guide
Venues
Exercise
Import venues
Solution
Import venues
Import venues
Type the following command into the Neo4j
browser to see the answers:
:play http://guides.neo4j.com/reco/5a.html
Continue
Continue with the Guide
Next Step
Calculating Distances
Calculating distances
The distance function lets us find the
distance in metres between two points
Calculating distances
The distance function lets us find the
distance in metres between two points
Points can be maps or nodes which contain the
properties 'latitude' and 'longitude'.
RETURN distance(point1, point2)
Continue
Continue with the Guide
Exercise
Using venues in recommendation
Solution
Using venues in recommendation
Using venues in recommendations
Type the following command into the Neo4j
browser to see the answers:
:play http://guides.neo4j.com/reco/answers/5b.html
Using venues in recommendations
WITH {latitude: 51.518551, longitude: -0.086114} AS here
MATCH (member:Member {name: "Mark Needham"})
-[:MEMBER_OF]->()-[:HOSTED_EVENT]->(futureEvent),
(venue)<-[:VENUE]-(futureEvent)
WHERE futureEvent.time > timestamp()
RETURN group.name,
futureEvent.name,
round((futureEvent.time - timestamp()) / (24.0*60*60*1000)) AS days,
distance(venue, here) AS distance
ORDER BY days, distance
Venues close to here
WITH {latitude: 51.518551, longitude: -0.086114} AS here
MATCH (member:Member {name: "Mark Needham"})
-[:MEMBER_OF]->()-[:HOSTED_EVENT]->(futureEvent),
(venue)<-[:VENUE]-(futureEvent)
WHERE futureEvent.time > timestamp()
WITH group, futureEvent, distance(venue, here) AS distance
WHERE distance < 1000
RETURN group.name,
futureEvent.name,
round((futureEvent.time - timestamp()) / (24.0*60*60*1000)) AS days,
distance
ORDER BY days, distance
Next Guide
RSVPs
Exercise
Events at my venues
Solution
Events at my venues
Events at my venues
Type the following command into the Neo4j
browser to see the answers:
:play http://guides.neo4j.com/reco/6.html
Next Guide
Procedures
Exercise
Import photos metadata
Solution
Import photos metadata
Import photos metadata
Type the following command into the Neo4j
browser to see the answers:
:play http://guides.neo4j.com/reco/7.html
Next Guide
Latent Social Graph
Watch Out
Transaction State
Transaction State
Cypher keeps all transaction state in memory
while running a query which is fine most of the
time.
But when refactoring the graph, this state can
get very large and may result in an
OutOfMemory exception.
Batch all the things
We therefore need to take a batched approach
to large scale refactorings.
MATCH (m1:Process) WITH m1 LIMIT 1000
REMOVE m1:Process
WITH m1
// do the refactoring
Continue
Continue with the Guide
Exercise
Add friends to recommendation
Solution
Add friends to recommendation
Add friends to recommendation
Type the following command into the Neo4j
browser to see the answers:
:play http://guides.neo4j.com/reco/8.html
Next Guide
Scoring
Next Guide
Your turn
Thats all for today! Thank you!
Graph Connect San Francisco is only around the corner!
http://graphconnect.com/