knaxuslearn · build · ship

Projects / People you may know / Phase 1

Phase 1 of 4 · about 2.5 hours

People and friendships

You end with twelve people, fourteen friendships and a rule that refuses a second person with the same id.

A graph has two kinds of things. Nodes are the things themselves, here people. Relationships connect two nodes, here friendships. Both can carry properties, which are named values such as name or since. A label such as Person says what kind of node it is.

Step 1.1: Your first two people (about 20 minutes)

In Cypher you draw what you want with parentheses for nodes. CREATE (:Person {name: 'Asha'}) reads as "create a node labelled Person with these properties":

CREATE (:Person {id: 'p1', name: 'Asha', city: 'Pune', born: 1996});
CREATE (:Person {id: 'p2', name: 'Ben', city: 'Pune', born: 1994});
MATCH (p:Person) RETURN p.id AS id, p.name AS name, p.city AS city;

MATCH finds things that fit a pattern, and RETURN says what to show. Together they are the SELECT of Cypher.

Step 1.2: Ten more at once (about 20 minutes)

UNWIND turns a list into one row per item, and then CREATE runs once per row. It lets us load many nodes in one statement:

UNWIND [
  {id: 'p3',  name: 'Chitra', city: 'Mumbai',    born: 1992},
  {id: 'p4',  name: 'Dev',    city: 'Mumbai',    born: 1998},
  {id: 'p5',  name: 'Esha',   city: 'Delhi',     born: 1995},
  {id: 'p6',  name: 'Farid',  city: 'Delhi',     born: 1990},
  {id: 'p7',  name: 'Gita',   city: 'Bengaluru', born: 1997},
  {id: 'p8',  name: 'Hari',   city: 'Bengaluru', born: 1993},
  {id: 'p9',  name: 'Isha',   city: 'Pune',      born: 1999},
  {id: 'p10', name: 'Jay',    city: 'Chennai',   born: 1991},
  {id: 'p11', name: 'Kiran',  city: 'Chennai',   born: 1996},
  {id: 'p12', name: 'Lata',   city: 'Mumbai',    born: 1994}
] AS row
CREATE (:Person {id: row.id, name: row.name, city: row.city, born: row.born});
MATCH (p:Person) RETURN count(p) AS people;
MATCH (p:Person {city: 'Pune'}) RETURN p.name AS name ORDER BY name;

The last query puts a value inside the braces of the pattern. That is a quick way to say "only nodes where city is Pune".

Step 1.3: A rule for unique ids (about 20 minutes)

Nothing stops us from creating a second person with id p1. A constraint is a rule the database enforces. This one says no two Person nodes may share an id:

CREATE CONSTRAINT person_id_unique IF NOT EXISTS FOR (p:Person) REQUIRE p.id IS UNIQUE;
SHOW CONSTRAINTS YIELD name, type, labelsOrTypes, properties;

A uniqueness constraint also creates an index behind it, so looking up a person by id will be fast. We will rely on that in Phase 4.

Step 1.4: Try to break it (about 15 minutes)

Create a second person with an id that exists:

CREATE (:Person {id: 'p1', name: 'Another Asha', city: 'Goa', born: 2000});

The database refuses with a constraint error and creates nothing.

Step 1.5: Friendships (about 40 minutes)

A relationship has a type, a direction and, if we want, properties. We write it as an arrow: (a)-[:FRIENDS_WITH {since: 2019}]->(b). This statement finds each pair of people by id and joins them:

UNWIND [
  ['p1','p2',2019], ['p1','p3',2020], ['p1','p9',2021],
  ['p2','p3',2018], ['p2','p4',2022],
  ['p3','p5',2017], ['p3','p12',2020],
  ['p4','p12',2023], ['p5','p6',2016], ['p6','p8',2019],
  ['p7','p8',2021], ['p7','p9',2022], ['p8','p10',2020],
  ['p10','p11',2018]
] AS e
MATCH (a:Person {id: e[0]}), (b:Person {id: e[1]})
CREATE (a)-[:FRIENDS_WITH {since: e[2]}]->(b);
MATCH ()-[r:FRIENDS_WITH]->() RETURN count(r) AS friendships;

Fourteen friendships. Notice that we stored each friendship once, as one arrow from one person to the other. We will come back to why that matters.

People are nodes and friendships are relationships. Colour is the city.

Step 1.6: Direction (about 25 minutes)

Every relationship in Neo4j has a direction, even when the real-world idea has none. A friendship is mutual, but our data records who added whom. Ask in three ways:

MATCH (a:Person {name: 'Asha'})-[r:FRIENDS_WITH]-(b:Person)
RETURN b.name AS friend, r.since AS since
ORDER BY since;
MATCH (a:Person {name: 'Asha'})-[r:FRIENDS_WITH]->(b:Person)
RETURN b.name AS friend_i_added;
MATCH (a:Person {name: 'Ben'})-[r:FRIENDS_WITH]->(b:Person)
RETURN b.name AS friend_ben_added;

Without an arrowhead, the pattern (a)-[r]-(b) matches either direction, so Asha sees every friendship she is part of. With an arrowhead, only those she added. Ben added two friends and was added by Asha, so the last query misses Asha.

Pitfall: when a relationship is really two-way, store one arrow and query it without a direction. Store two arrows and every count doubles. Step 2.5 shows it.

Phase 1 done

You have a graph, a rule that keeps ids unique, and a feel for patterns drawn with parentheses and arrows. Now we ask it graph questions.