Cookies help us deliver our services. By using our services, you agree to our use of cookies. More information

Neo4j:Basics - Introduction

From NoSQLZoo
Jump to: navigation, search

Working With the greek_gods Family Tree Graph

This tutorial introduces NoSQL using Neo4j. We will be using the RETURN, MATCH, and WHERE clauses on the greek_gods collection which contains a family tree of around 130 individuals with varying roles and significance in Greek mythology.

For anyone wishing to use this data-set on their own machines, the Cypher file will be available here at a later date.

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 it is a collection of nodes and relationships respectively. In Neo4j graphs follow the "Property Graph Model".

Here 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 or who have used JavaScript Object Notation (JSON) will be familiar with this concept already.

A node is a simple entity that contains A node representing a person might include things like their name, date of birth, or hometown, etc.
As well as containing properties, nodes can be labelled as belonging to a certain group for query purposes. For example, in a database of celebrities Frank Ocean could be labelled as an :Musician. There's no limit to the amount of labels that can apply to a node, the celebrity Hugh Laurie could be simultaneously labelled 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 (CQL) 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

The MATCH clause is used to search for data that meets a pattern you define.
Pattern matching forms the basis of most queries in Neo4j. Patterns are ASCII descriptions of "shapes" that can be found in a graph, nodes are represented by pairs of parenthesis: ().
Relations represented by hyphens, chevrons, and square brackets: --, <--, --> -[]-, <-[]-, -[]->. Neo4j also allows you to name patterns, e.g. (a) so that you may refer to them later.

These exercises will introduce you to pattern matching, but those interested can also view the official documentation here.

MATCH Everything

To query everything simply assign a name to a node then return that name.

Return all nodes in the collection.

MATCH (n)
RETURN n;

WHERE

The WHERE clause is used to restrict MATCH patterns.

Find the node(s), n, with the label ':God' and a name property equal to 'Zeus'.

MATCH (n)
WHERE n:God AND n.name = 'Hera'
RETURN n;
MATCH (n) WHERE n:God AND n.name = 'Zeus' RETURN n; 

Combining MATCH and WHERE

In most scenarios, WHERE and MATCH can be combined. e.g. using the previous example:

MATCH (g:God {name: 'Zeus'})
RETURN g;

Query by ID

It is possible to retrieve nodes and relations by id. The id is based on the order in which they were created and starts at 0.

Find the node with ID 2.

MATCH (n)
WHERE id(n) = 2
RETURN n;

Query by Attributes

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;

IN

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;

NOT _ IN

Predictably, 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;

ORDER BY

ORDER BY is used to order results and can be done using pattern names or their attributes.

Return the Olympians in alphabetical order.

MATCH (o:Olympian)
RETURN o.name
ORDER BY o.name;

LIMIT

The LIMIT clause is used to set a maximum number of results.

First n Results.

Return the first two nodes in the collection.

MATCH (n)
RETURN n
LIMIT 2;

Last n Results.

The last n rows of a result can be found by reversing the list with ORDER BY _ DESC then using LIMIT.

Return the last two nodes in the collection.

MATCH (n)
RETURN n
ORDER BY n DESC
LIMIT 2;

SKIP

Skip n Results. / Find nth Result.

Nodes can be ignored with the SKIP keyword. Combining SKIP and LIMIT makes it possible to return a range of nodes based on their position, or alternatively a single specific node.

Return the name fields of nodes 11 to 13.

MATCH (n)
RETURN n.name
SKIP 10
LIMIT 4;

Once comfortable with these questions, move onto Neo4j Basics - Relationships.