Your	own
Recommendation
Engine
with	Neo4j and	Reco4PHP
About	me
• Christophe	Willemsen
• Neo4j	&	Elasticsearch Consultant	@	GraphAware
• Belgium
@ikwattro
What	we’ll	talk	about
• Challenges	of	a	recommendation	engine
• Quick	intro	to	graph	databases	(Neo4j)
• Reco4PHP
• Build	your	own	(demo)
Challenges	
of	a
Recommendation	
Engine
Recommendations	:	Overview
• News	you	should	read	(blogs)
• Books	you	should	buy	(e-commerce)
• Persons	you	may	know	(social)
• Which	talk	to	attend	(knowledge)
• Which	service	is	best	suited	to	handle	a	given	failure	(impact	analysis)
• Finding	the	right	candidates	for	medical	proofs	(healthcare)
• …
Main	types	of	recommendations
• Content	based	(features)
• Collaborative	filtering	(relations	user	<->	item,	knn,	matrix	factorization)
• Knowledge	based
Combination	of	those	by	completely	crazy	scientists	+	secret	sauce
Business	Challenges
• Finding	things	to	recommend
• Serve	the	most	relevant	recommendations
• Measuring	the	quality	of	recommendations
• Time	to	market	/	cost	of	development
Business	challenges
Imagine	that	you	should	implement
the	feature
“People	you	may	know”
on	LinkedIn
Business	challenges	:	Finding	candidates
• Common	contacts
• Facebook	friends	in	common
• Mobile/email	contacts	in	common
• All	contacts	of	your	contacts
• Worked	for	same	company
• Studied	at	same	school
• Share	same	interests
• Lives	in	same	city
Business	challenges
It’s	just	the	beginning	!
Let’s	revisit	…
Business	challenges	:	most	relevant
More	contacts	in	common:	
More	relevant	?
Business	challenges	:	most	relevant
Same	city,	worked	for	same	company,	
studied	at	same	school
Business	challenges	:	most	relevant
What	to	do	with	emails	that
actually	don’t	represent	a	person?
Should	we	recommend info@coolblue.nl?
Business	challenges	:	most	relevant
What	should	we	do	with	contacts
pending?
rejected?
always	ignored?
Technical	Challenges
• Simplicity
• Performance	(Real-time	or	near	real-time)
• Flexibility
Intro	to	
Graph	Databases
(	Neo4j	)
is	a
graph	database.
Nodes
A	node	can	have	one	or	multiple	labels
Person, Male
A	node	can	one	or	more	properties
Person, Male
name : Mark
Nodes	are	connected	by	relationships
Relationships	have	a	type
Relationships	have	a	direction
Relationships	can	have	properties
Cypher	:	the	query	language	for	graphs
Declarative	pattern	matching	language
Describe	
what	you	want	
to	retrieve	from	the	database,	
not	
how	to	get	it.
Declarative	pattern	matching	language
MATCH	(you:Person {name:”You”})-[:ATTENDED]->(talk),
(talk)<-[:ATTENDED]-(other)
WHERE	(you)-[:LIVES_IN]->(city)<-[:LIVES_IN]-(other)
AND	you <>	other
RETURN	other AS	maybeFriend
Why	graphs	?
Technical	Challenges
• Simplicity
• Performance	(Real-time	or	near	real-time)
• Flexibility
Simplicity
Features as	well	as	relationshipsare	
naturally	represented	in	
a	graph database.
Simplicity
The	recommendation logic	can	be	
represented	in	a	graph	traversal.
Technical	Challenges
• Simplicity
• Performance	(Real-time)
• Flexibility
Performance
• Relationships	are	first	class	citizens	(	no	joins	!	)
• Traversing	relationships	in	Neo4j	is	cheap
(	4	millions	traversals	per	second	per	core	)
Reco4PHP	
Open	Source
Recommendation	Engine	
Skeleton	
atop	Neo4j
Reco4PHP
• Standalone	PHP	library
• Opinionated	architecture
• Flexible
• Handle	all	the	glue
Architecture
• Input	->	Discovery	Engines	->	Recommendations
• Scores	and	Score	builders
• Blacklists	and	Filters
• Post	Processors
• Context	(how	many,		meteo,	all	k/v	external	factors..)
Design	decisions
• DiscoveryEngine per	recommendation	“reason”	(core	logic)
• DiscoveryEngine executes	a	graph	traversal
(Cypher	query)
• Items	discovered	multiple	times	are	more	relevant
Technical	Challenges
• Simplicity
• Performance	(Real-time	or	near	real-time)
• Flexibility
Let’s	build	one
In	5	minutes,	we’ll	build	a	simple	engine	that
recommends	repositories	you	might
be	interested	in	on	Github.
composer	require	graphaware/reco4php
0.	The	Model
:STARS
:CONTRIBUTED_TO
:OWNS
:FOLLOWS
:FOLLOWS
:WRITTEN_IN
1.	Discover
Find	repositories	starred	by	the	people	you	follow	or	following	
you.
2.	Serve	only	relevant	recommendations
We’ll	create	a	blacklist	that	will	exclude	the	repositories	I	am	
the	owner	of.
3.	Post	process
We’ll	reward	the	recommended	repositories	by	let’s	say	5	
points	if	they	are	written	in	PHP.
4.	Assemble
5.	Tadaaaaaam
Thank	you	!
Questions	?
graphaware.com
github.com/graphaware
@ikwattro

Your own recommendation engine with neo4j and reco4php - DPC16