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
m (40166222 moved page Neo4j:Basics - MATCH to Neo4j:Basics - Introduction without leaving a redirect)
 
(10 intermediate revisions by the same user not shown)
Line 5: Line 5:
  
 
==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 it is a collection of nodes and relationships respectively.
+
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'''". <br />
  
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.
+
Here 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] or who have used JavaScript Object Notation (JSON) will be familiar with this concept already.<br/>
  
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.
+
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. <br />
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>.  
+
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 <code>Frank Ocean</code> could be labelled 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 labelled as an <code>:Actor</code>, <code>:Musician</code>, and <code>:Author</code>. <br/>
  
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]] <br/>
  
This remainder of this tutorial will use the [https://neo4j.com/developer/cypher-query-language/ Cypher] query language (CQL) 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'''==
Line 33: Line 33:
 
<pre class="def"><nowiki>
 
<pre class="def"><nowiki>
 
MATCH (n)
 
MATCH (n)
RETURN n;
+
RETURN n;</nowiki></pre>
</nowiki></pre>
 
 
</div>
 
</div>
 
=='''WHERE'''==
 
=='''WHERE'''==
Line 40: Line 39:
 
The '''WHERE''' clause is used to restrict '''MATCH''' patterns.
 
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 the node(s), n, with the label ':God' and a name property equal to 'Zeus'.
 
</p>
 
</p>
 
<pre class="def"><nowiki>
 
<pre class="def"><nowiki>
 
MATCH (n)
 
MATCH (n)
WHERE n:God AND n.name = 'Zeus'
+
WHERE n:God AND n.name = 'Hera'
RETURN n;
+
RETURN n;</nowiki></pre>
</nowiki></pre>
+
<pre class="ans"><nowiki>MATCH (n) WHERE n:God AND n.name = 'Zeus' RETURN n; </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'''===
 
===Combining '''MATCH''' and '''WHERE'''===
Line 54: Line 52:
 
<pre class="def"><nowiki>
 
<pre class="def"><nowiki>
 
MATCH (g:God {name: 'Zeus'})
 
MATCH (g:God {name: 'Zeus'})
RETURN g;
+
RETURN g;</nowiki></pre>
</nowiki></pre>
 
 
</div>
 
</div>
 
===Query by ID===
 
===Query by ID===
Line 64: Line 61:
 
MATCH (n)
 
MATCH (n)
 
WHERE id(n) = 2
 
WHERE id(n) = 2
RETURN n;
+
RETURN n;</nowiki></pre>
</nowiki></pre>
 
 
</div>
 
</div>
===Query by Atributes===
+
===Query by Attributes===
 
<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 74: Line 70:
 
MATCH (g:God {name: "Zeus"})  
 
MATCH (g:God {name: "Zeus"})  
 
RETURN g.name, g.greek;</nowiki></pre>
 
RETURN g.name, g.greek;</nowiki></pre>
<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'''===
 
==='''IN'''===
Line 83: Line 79:
 
MATCH (g:God)  
 
MATCH (g:God)  
 
WHERE g.name IN ['Hera', 'Demeter']  
 
WHERE g.name IN ['Hera', 'Demeter']  
RETURN g.name, g.greek;
+
RETURN g.name, g.greek;</nowiki></pre>
</nowiki></pre>
 
 
<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>
Line 94: Line 89:
 
<pre class="def"><nowiki>
 
<pre class="def"><nowiki>
 
MATCH (o:Olympian)
 
MATCH (o:Olympian)
RETURN o.name;
+
RETURN o.name;</nowiki></pre>
</nowiki></pre>
 
 
<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'''==
 
=='''ORDER BY'''==
 
<div class="q" data-lang="neo4j">
 
<div class="q" data-lang="neo4j">
Line 105: Line 100:
 
MATCH (o:Olympian)
 
MATCH (o:Olympian)
 
RETURN o.name
 
RETURN o.name
ORDER BY o.name;
+
ORDER BY o.name;</nowiki></pre>
</nowiki></pre>
 
 
</div>
 
</div>
 
=='''LIMIT'''==
 
=='''LIMIT'''==
The '''LIMIT'' clause is used to set a maximum number of results.
+
The '''LIMIT''' clause is used to set a maximum number of results.
 
===First '''n''' Results.===
 
===First '''n''' Results.===
 
<div class="q" data-lang="neo4j">
 
<div class="q" data-lang="neo4j">
Line 116: Line 110:
 
MATCH (n)
 
MATCH (n)
 
RETURN n
 
RETURN n
LIMIT 2;
+
LIMIT 2;</nowiki></pre>
</nowiki></pre>
 
 
</div>
 
</div>
 
===Last '''n''' Results.===
 
===Last '''n''' Results.===
Line 127: Line 120:
 
RETURN n
 
RETURN n
 
ORDER BY n DESC
 
ORDER BY n DESC
LIMIT 2;
+
LIMIT 2;</nowiki></pre>
</nowiki></pre>
 
 
</div>
 
</div>
 +
 
=='''SKIP'''==
 
=='''SKIP'''==
 
===Skip '''n''' Results. / Find '''n'''th Result.===
 
===Skip '''n''' Results. / Find '''n'''th Result.===
Line 139: Line 132:
 
RETURN n.name
 
RETURN n.name
 
SKIP 10
 
SKIP 10
LIMIT 4;
+
LIMIT 4;</nowiki></pre>
</nowiki></pre>
 
 
</div>
 
</div>
  
Further examples can be found here: [[Neo4j:MATCH_Examples| MATCH Examples]].<br/>
+
Once comfortable with these questions, move onto [[Neo4j:Basics_-_Relationships| Neo4j Basics - Relationships]].
Once comfortable with these questions, move onto [[Neo4j:MATCH_Basics_-_Relationships| MATCH Basics - Relationships]].
 

Latest revision as of 10:28, 15 October 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".

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.