Difference between revisions of "Neo4j:Basics - Introduction"
(init unfinished) |
m (40166222 moved page Neo4j:Basics - MATCH to Neo4j:Basics - Introduction without leaving a redirect) |
||
(14 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | ==Working | + | ==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> | + | 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 | + | 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 | + | 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 /> |
− | + | 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 | + | 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 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: [[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 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"> | ||
+ | To query everything simply assign a name to a node then return that name. | ||
+ | <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"> | <div class="q" data-lang="neo4j"> | ||
− | The ''' | + | The '''WHERE''' clause is used to restrict '''MATCH''' patterns. |
− | |||
<p class="strong"> | <p class="strong"> | ||
− | Find | + | 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 = ' | + | 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 ( | ||
</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: | ||
<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=== | ||
+ | <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 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 49: | 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'''=== | ||
<div class="q" data-lang="neo4j"> | <div class="q" data-lang="neo4j"> | ||
+ | '''WHERE _ IN''' can be used to check against a list of values. | ||
<p class="strong">Find the Greek names of the 'Big Three' ('Zeus', 'Poseidon', 'Hades')</p> | <p class="strong">Find the Greek names of the 'Big Three' ('Zeus', 'Poseidon', 'Hades')</p> | ||
<pre class="def"><nowiki> | <pre class="def"><nowiki> | ||
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> | ||
+ | ==='''NOT _ IN'''=== | ||
+ | <div class="q" data-lang="neo4j"> | ||
+ | Predictably, '''NOT _ IN''' performs the inverse of '''IN''' | ||
+ | <p class="strong">Find the names of the Olympians, excluding Apollo and Hestia</p> | ||
+ | <pre class="def"><nowiki> | ||
+ | MATCH (o:Olympian) | ||
+ | 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> | ||
+ | |||
+ | =='''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> | ||
− | + | Once comfortable with these questions, move onto [[Neo4j:Basics_-_Relationships| Neo4j Basics - Relationships]]. |
Latest revision as of 10:28, 15 October 2018
Contents
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.