Neo4j:Basics - Introduction
Contents
Working with the greek_gods
family tree graph
This tutorial introduces NoSQL using Neo4j. We will be using the RETURN
, MATCH
, and WHERE
commands on the greek_gods collection which contains a family tree of around 130 individuals.
For anyone wishing to use this data-set on their own machines, the Cypher file used to build it is available here: *TBA*.
Introduction to Graphs and the 'Graph Property Model'
You've likely heard of graphs before. In mathematics a graph is the term for a set of vertices and edges, here the terms nodes and relationships are used instead.
In Neo4j graphs follow the "Property Graph Model". A property is just a key-value pair, such as name: 'Alice'
or age: 27
. Users who have used document-oriented databases such as MongoDB will find this concept very similar.
A node is a simple entity that contains any amount of properties, and they can be labeled as belonging to a certain set. For example, in a database of celebrities Stephen Fry
could be labeled as a :Actor
. There's no limit to the amount of labels that can apply to a node, the celebrity Hugh Laurie
could be simultaneously labeled as an :Actor
, :Musician
, and :Author
.
In the property graph model, a relationship is a connection between exactly two nodes, though there's not limit to the amount of relationships a node made have. A relationship always has both a name and a direction (start node and end node), and like a node it can also have properties. Bidirectional relationships are a little trickier, and are out-width the scope of this tutorial: Modelling Bidirectional Relationships
This remainder of this tutorial will use the Cypher query language to introduce some basic concepts.
RETURN
The RETURN clause determines what is included in the query result set. The following operation returns some numbers, strings, and lists added together.
RETURN 2 + 2, "A" + "B", [1, 2, 3] + [4];
MATCH and WHERE
The MATCH clause is used to search for data that meets a pattern you define.
The WHERE clause is used to restrict these patterns.
Find a node, n, with the label ':God' and a name property equal to 'Zeus'.
MATCH (n) WHERE n:God AND n.name = 'Zeus' RETURN n;
MATCH (g) WHERE g:God AND g.name = 'Zeus' RETURN g;
In most scenarios, WHERE and MATCH can be combined. e.g. using the previous example:
MATCH (g:God {name: 'Zeus'}) RETURN g;
This query currently gets Zeus' names.
Find Hera's English and Greek names.
MATCH (g:God {name: "Zeus"}) RETURN g.name, g.greek;
MATCH (g:God {name: "Zeus"}) RETURN g.name, g.greek;
WHERE _ IN can be used to check against a list of values.
Find the Greek names of the 'Big Three' ('Zeus', 'Poseidon', 'Hades')
MATCH (g:God) WHERE g.name IN ['Hera', 'Demeter'] RETURN g.name, g.greek;
MATCH (g:God) WHERE g.name IN ['Zeus', 'Poseidon', 'Hades'] RETURN g.name, g.greek;
Predictably, WHERE NOT _ IN performs the inverse of IN
Find the names of the Olympians, excluding Apollo and Hestia
MATCH (o:Olympian) RETURN o.name;
MATCH (o:Olympian) WHERE NOT o.name IN ['Apollo', 'Hestia'] RETURN o.name;
Further examples can be found here: MATCH Examples.
Once comfortable with these questions, move onto MATCH Basics - Relationships.