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

Difference between revisions of "Neo4j:Basics - Introduction"

From NoSQLZoo
Jump to: navigation, search
Line 1: Line 1:
==Working with the <code>greek_gods</code> family tree graph==
+
==Working With the <code>greek_gods</code> Family Tree Graph==
This tutorial introduces NoSQL using Neo4j. We will be using the <code>RETURN</code>, <code>MATCH</code>, and <code>WHERE</code> commands on the '''greek_gods''' collection which contains a family tree of around 130 individuals.
+
This tutorial introduces NoSQL using Neo4j. We will be using the <code>RETURN</code>, <code>MATCH</code>, and <code>WHERE</code> 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 used to build it is available here: *TBA*.
+
For anyone wishing to use this data-set on their own machines, the Cypher file will be available [[greek_gods_build_script | here]] at a later date.
  
 
==Introduction to Graphs and the 'Graph Property Model'==
 
==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.
+
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". A property is just a key-value pair, such as <code>name: 'Alice'</code> or <code>age: 27</code>. Users who have used document-oriented databases such as [https://no.sqlzoo.net/wiki/Main_Page MongoDB] will find this concept very similar.
+
In Neo4j graphs follow the "Property Graph Model". A property is just a key-value pair, such as <code>name: 'Alice'</code>, <code>age: 27</code>, pet_dogs. Users who have used document-oriented databases such as [https://no.sqlzoo.net/wiki/Main_Page MongoDB] or who have used JavaScript Object Notation (JSON) will be familiar with this concept already.
  
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 <code>Stephen Fry</code> could be labeled as a <code>:Actor</code>. There's no limit to the amount of labels that can apply to a node, the celebrity <code>Hugh Laurie</code> could be simultaneously labeled as an <code>:Actor</code>, <code>:Musician</code>, and <code>:Author</code>.  
+
A node is a simple entity that contains any amount of properties. 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 labeled as belonging to a certain group for query purposes. For example, in a database of celebrities <code>Barry Manilow</code> could be labeled as an <code>:Musician</code>. There's no limit to the amount of labels that can apply to a node, the celebrity <code>Hugh Laurie</code> could be simultaneously labeled as an <code>:Actor</code>, <code>:Musician</code>, and <code>:Author</code>.  
  
 
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: [[Neo4j:Modelling Bidirectional Relationships | Modelling Bidirectional Relationships]]
 
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: [[Neo4j:Modelling Bidirectional Relationships | Modelling Bidirectional Relationships]]
  
This remainder of this tutorial will use the [https://neo4j.com/developer/cypher-query-language/ Cypher] query language to introduce some basic concepts.  
+
This remainder of this tutorial will use the [https://neo4j.com/developer/cypher-query-language/ Cypher] query language (CQL) to introduce some basic concepts.  
  
 
+
=='''RETURN'''==
==RETURN==
 
 
<div class="q" data-lang="neo4j">
 
<div class="q" data-lang="neo4j">
 
The '''RETURN''' clause determines what is included in the query result set.
 
The '''RETURN''' clause determines what is included in the query result set.
Line 22: Line 22:
 
<pre class="def"><nowiki>RETURN 2 + 2, "A" + "B", [1, 2, 3] + [4];</nowiki></pre>
 
<pre class="def"><nowiki>RETURN 2 + 2, "A" + "B", [1, 2, 3] + [4];</nowiki></pre>
 
</div>
 
</div>
==MATCH and WHERE==
+
=='''MATCH'''==
 +
The '''MATCH''' clause is used to search for data that meets a pattern you define.<br/>
 +
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: <code>()</code>.<br/>
 +
Relations represented by hyphens, chevrons, and square brackets: <code> --, <--, --> -[]-, <-[]-, -[]-></code>. Neo4j also allows you to name patterns, e.g. <code>(a)</code> so that you may refer to them later.<br/><br/>
 +
These exercises will introduce you to pattern matching, but those interested can also view the official documentation [https://neo4j.com/docs/developer-manual/current/cypher/syntax/patterns/ here.]<br />
 +
==='''MATCH''' Everything===
 
<div class="q" data-lang="neo4j">
 
<div class="q" data-lang="neo4j">
The '''MATCH''' clause is used to search for data that meets a pattern you define.<br />
+
To query everything simply assign a name to a node then return that name.
The '''WHERE''' clause is used to restrict these patterns.
+
<p class="strong">Return all nodes in the collection.</p>
 +
<pre class="def"><nowiki>
 +
MATCH (n)
 +
RETURN n;
 +
</nowiki></pre>
 +
</div>
 +
=='''WHERE'''==
 +
<div class="q" data-lang="neo4j">
 +
The '''WHERE''' clause is used to restrict '''MATCH''' patterns.
 
<p class="strong">
 
<p class="strong">
 
Find a node, n, with the label ':God' and a name property equal to 'Zeus'.
 
Find a node, n, with the label ':God' and a name property equal to 'Zeus'.
Line 36: Line 49:
 
<pre class="ans"><nowiki>MATCH (g) WHERE g:God AND g.name = 'Zeus' RETURN g; </nowiki></pre>
 
<pre class="ans"><nowiki>MATCH (g) WHERE g:God AND g.name = 'Zeus' RETURN g; </nowiki></pre>
 
</div>
 
</div>
 +
===Combining '''MATCH''' and '''WHERE'''===
 
<div class="q" data-lang="neo4j">
 
<div class="q" data-lang="neo4j">
 
In most scenarios, '''WHERE''' and '''MATCH''' can be combined. e.g. using the previous example:
 
In most scenarios, '''WHERE''' and '''MATCH''' can be combined. e.g. using the previous example:
Line 43: Line 57:
 
</nowiki></pre>
 
</nowiki></pre>
 
</div>
 
</div>
 +
===Query by ID===
 +
<div class="q" data-lang="neo4j">
 +
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.
 +
<p class="strong">Find the node with ID 2.</p>
 +
<pre class="def"><nowiki>
 +
MATCH (n)
 +
WHERE id(n) = 2
 +
RETURN n;
 +
</nowiki></pre>
 +
</div>
 +
===Query by Atributes===
 
<div class="q" data-lang="neo4j">
 
<div class="q" data-lang="neo4j">
 
This query currently gets Zeus' names.
 
This query currently gets Zeus' names.
Line 51: Line 76:
 
<pre class="ans"><nowiki>MATCH (g:God {name: "Zeus"}) RETURN g.name, g.greek;</nowiki></pre>
 
<pre class="ans"><nowiki>MATCH (g:God {name: "Zeus"}) RETURN g.name, g.greek;</nowiki></pre>
 
</div>
 
</div>
 +
==='''IN'''===
 
<div class="q" data-lang="neo4j">
 
<div class="q" data-lang="neo4j">
 
'''WHERE _ IN''' can be used to check against a list of values.
 
'''WHERE _ IN''' can be used to check against a list of values.
Line 61: Line 87:
 
<pre class="ans"><nowiki>MATCH (g:God) WHERE g.name IN ['Zeus', 'Poseidon', 'Hades'] RETURN g.name, g.greek;</nowiki></pre>
 
<pre class="ans"><nowiki>MATCH (g:God) WHERE g.name IN ['Zeus', 'Poseidon', 'Hades'] RETURN g.name, g.greek;</nowiki></pre>
 
</div>
 
</div>
 +
 +
==='''NOT _ IN'''===
 
<div class="q" data-lang="neo4j">
 
<div class="q" data-lang="neo4j">
Predictably, '''WHERE NOT _ IN''' performs the inverse of '''IN'''
+
Predictably, '''NOT _ IN''' performs the inverse of '''IN'''
 
<p class="strong">Find the names of the Olympians, excluding Apollo and Hestia</p>
 
<p class="strong">Find the names of the Olympians, excluding Apollo and Hestia</p>
 
<pre class="def"><nowiki>
 
<pre class="def"><nowiki>
Line 70: Line 98:
 
<pre class="ans"><nowiki>MATCH (o:Olympian) WHERE NOT o.name IN ['Apollo', 'Hestia'] RETURN o.name;</nowiki></pre>
 
<pre class="ans"><nowiki>MATCH (o:Olympian) WHERE NOT o.name IN ['Apollo', 'Hestia'] RETURN o.name;</nowiki></pre>
 
</div>
 
</div>
 +
=='''ORDER BY'''==
 +
<div class="q" data-lang="neo4j">
 +
'''ORDER BY''' is used to order results and can be done using pattern names or their attributes.
 +
<p class="strong">Return the Olympians in alphabetical order.</p>
 +
<pre class="def"><nowiki>
 +
MATCH (o:Olympian)
 +
RETURN o.name
 +
ORDER BY o.name;
 +
</nowiki></pre>
 +
</div>
 +
=='''LIMIT'''==
 +
The '''LIMIT'' clause is used to set a maximum number of results.
 +
===First '''n''' Results.===
 +
<div class="q" data-lang="neo4j">
 +
<p class="strong">Return the first two nodes in the collection.</p>
 +
<pre class="def"><nowiki>
 +
MATCH (n)
 +
RETURN n
 +
LIMIT 2;
 +
</nowiki></pre>
 +
</div>
 +
===Last '''n''' Results.===
 +
<div class="q" data-lang="neo4j">
 +
The last '''n''' rows of a result can be found by reversing the list with '''ORDER BY _ DESC''' then using '''LIMIT'''.
 +
<p class="strong">Return the last two nodes in the collection.</p>
 +
<pre class="def"><nowiki>
 +
MATCH (n)
 +
RETURN n
 +
ORDER BY n DESC
 +
LIMIT 2;
 +
</nowiki></pre>
 +
</div>
 +
=='''SKIP'''==
 +
===Skip '''n''' Results. / Find '''n'''th Result.===
 +
<div class="q" data-lang="neo4j">
 +
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.
 +
<p class="strong">Return the name fields of nodes 11 to 13.</p>
 +
<pre class="def"><nowiki>
 +
MATCH (n)
 +
RETURN n.name
 +
SKIP 10
 +
LIMIT 4;
 +
</nowiki></pre>
 +
</div>
 +
 
Further examples can be found here: [[Neo4j:MATCH_Examples| MATCH Examples]].<br/>
 
Further examples can be found here: [[Neo4j:MATCH_Examples| MATCH Examples]].<br/>
 
Once comfortable with these questions, move onto [[Neo4j:MATCH_Basics_-_Relationships| MATCH Basics - Relationships]].
 
Once comfortable with these questions, move onto [[Neo4j:MATCH_Basics_-_Relationships| MATCH Basics - Relationships]].

Revision as of 16:31, 14 June 2018

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". A property is just a key-value pair, such as name: 'Alice', age: 27, pet_dogs. 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 any amount of properties. 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 labeled as belonging to a certain group for query purposes. For example, in a database of celebrities Barry Manilow could be labeled 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 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 (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 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; 

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 Atributes

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;

Further examples can be found here: MATCH Examples.
Once comfortable with these questions, move onto MATCH Basics - Relationships.