knaxuslearn · build · ship

Projects / People you may know / Phase 3

Phase 3 of 4 · about 2.5 hours

Ranked suggestions

You end with a top five list for Asha ranked by a score, and a plan that shows what an index changed.

Mutual friends are a good signal. Shared interests are another. A graph lets us mix them without reshaping anything: a new kind of node and a new relationship type are enough.

Step 3.1: Interests (about 30 minutes)

We add Interest nodes and an INTERESTED_IN relationship from people to them:

UNWIND ['hiking', 'chess', 'film', 'cooking', 'cricket', 'photography'] AS name
CREATE (:Interest {name: name});
UNWIND [
  ['p1','hiking'], ['p1','photography'], ['p1','film'],
  ['p2','chess'], ['p2','film'],
  ['p3','cooking'], ['p3','film'],
  ['p4','cricket'], ['p4','hiking'],
  ['p5','photography'], ['p5','cooking'],
  ['p6','chess'], ['p7','hiking'], ['p7','photography'],
  ['p8','cricket'], ['p9','hiking'], ['p9','film'],
  ['p10','cricket'], ['p11','chess'], ['p12','film'], ['p12','hiking']
] AS pair
MATCH (p:Person {id: pair[0]}), (i:Interest {name: pair[1]})
CREATE (p)-[:INTERESTED_IN]->(i);
MATCH (:Person)-[r:INTERESTED_IN]->(:Interest) RETURN count(r) AS interests;

Step 3.2: People who share interests (about 30 minutes)

A pattern can go through a node and back out: me, to an interest, and from there to someone else. That is the same trick as friends of friends, with a different middle:

MATCH (me:Person {name: 'Asha'})-[:INTERESTED_IN]->(i:Interest)<-[:INTERESTED_IN]-(other:Person)
WHERE other <> me AND NOT (me)-[:FRIENDS_WITH]-(other)
RETURN other.name AS person, collect(i.name) AS shared, count(i) AS shared_count
ORDER BY shared_count DESC, person;

Step 3.3: One score (about 50 minutes)

Now we combine both signals. For every possible person we count the distinct mutual friends and the distinct shared interests, then add them with weights: three points for each mutual friend, two for each shared interest, and one if they live in the same city. OPTIONAL MATCH is like a left join: it keeps the person even when there are no mutual friends, with a count of zero. WITH passes values to the next part of the query.

MATCH (me:Person {name: 'Asha'}), (c:Person)
WHERE c <> me AND NOT (me)-[:FRIENDS_WITH]-(c)
OPTIONAL MATCH (me)-[:FRIENDS_WITH]-(m:Person)-[:FRIENDS_WITH]-(c)
WITH me, c, count(DISTINCT m) AS mutual
OPTIONAL MATCH (me)-[:INTERESTED_IN]->(i:Interest)<-[:INTERESTED_IN]-(c)
WITH c, mutual, count(DISTINCT i) AS shared
WITH c, mutual, shared, mutual * 3 + shared * 2 + CASE WHEN c.city = 'Pune' THEN 1 ELSE 0 END AS score
WHERE score > 0
RETURN c.name AS suggestion, c.city AS city, mutual, shared, score
ORDER BY score DESC, suggestion
LIMIT 5;

The weights are our own choice. They are a starting point to tune, and a real system would test them against whether people accepted the suggestions.

Three signals, three weights, one score to sort by.

Step 3.4: Read the plan (about 25 minutes)

PROFILE runs the query and shows how it ran. The numbers to look at are DB Hits, which count the units of work the database did, and the operator names. Let's profile a lookup by city, which has no index yet:

PROFILE
MATCH (p:Person {city: 'Pune'})
RETURN p.name AS name;

If you see NodeByLabelScan, the database looked at every Person node and checked the city on each. With twelve people nobody cares.

Step 3.5: Add an index (about 20 minutes)

An index on city lets the database jump to the right people:

CREATE INDEX person_city IF NOT EXISTS FOR (p:Person) ON (p.city);
CALL db.awaitIndexes(60);
PROFILE
MATCH (p:Person {city: 'Pune'})
RETURN p.name AS name;

Look for NodeIndexSeek and compare the DB Hits. The index is built in the background, so the awaitIndexes call waits until it is online before we measure.

Phase 3 done

The suggestions now use two kinds of evidence and a plan you can read. Next we make the graph large and see what the work looks like.