Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions asciidoc/courses/cypher-fundamentals/course.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
:video: https://www.youtube.com/embed/Se_Zwiew90Q
:translations: jp-cypher-fundamentals,cn-cypher-fundamentals
:key-points: Cypher patterns, Reading data from a graph, Writing data to a graph
:slides: true

//https://youtu.be/Se_Zwiew90Q

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
= Introduction to Cypher
:type: video
:order: 1
:slides: true

[.video]
video::jEIE_b1MzAE[youtube,width=560,height=315]
Expand All @@ -22,6 +23,7 @@ https://docs.google.com/document/d/1u5OO6OOUnD0JVdnsGjj5Zs1DLPzUiqFKVO7W4tmBh9k/
// so that I can *find out more about him*

[.transcript]
[.slide.discrete]
== What is Cypher?
Cypher is a query language designed for graphs.

Expand All @@ -31,6 +33,9 @@ In this example, the entities are people and movies. We have _Person_ and _Movie

image::images/whiteboard.jpg[Whiteboard-image,width=600,align=center]

[.slide]
== Cypher Pattern Syntax

Just as we would draw circles and arrows on a whiteboard, we write out the pattern in Cypher:

* Nodes are represented by parentheses `()`.
Expand All @@ -41,6 +46,9 @@ Just as we would draw circles and arrows on a whiteboard, we write out the patte
* Properties drawn in a _speech bubble_ are specified in a JSON like syntax.
** Properties in Neo4j are key/value pairs, for example `{name: 'Tom Hanks'}`.

[.slide]
== Example Cypher Pattern

For example, a Cypher pattern in the graph could be:

[source,Partial,role=nocopy noplay]
Expand All @@ -55,7 +63,8 @@ The specific _Movie_ node in this pattern is filtered by the _title_ property wi
So this pattern represents all people in the graph who acted in the movie, _Cloud Atlas_.


=== How Cypher works
[.slide]
== How Cypher works

Cypher works by matching patterns in the data.
We retrieve data from the graph using the `MATCH` keyword.
Expand All @@ -69,15 +78,14 @@ MATCH (:Person)
// incomplete MATCH clause because we need to return something
----

[.slide]
== Returning Results with Variables

Suppose we want to retrieve all _Person_ nodes from the graph. We can assign a variable by placing a value before the colon.
Let's use the variable `p`. Now that `p` represents all _Person_ nodes retrieved from the graph, we can return them using the `RETURN` clause.

Run this Cypher code:

[TIP]
.Running Queries in Sandbox
Click the *Run in Sandbox* button to the top right of the code sample to open the Sandbox to the right and run the query.

[source,cypher]
----
MATCH (p:Person)
Expand All @@ -88,6 +96,14 @@ This query returns all nodes in the graph with the _Person_ label.
You can view the results returned using the graph view or the table view.
When you select the table view, you can also see the properties for the nodes returned.

[TIP]
.Running Queries in Sandbox
Click the *Run in Sandbox* button to the top right of the code sample to open the Sandbox to the right and run the query.


[.slide]
== Filtering by Property

Now, say we want to find the node which represents the _Person_ who's name is _Tom Hanks_.
Our _Person_ nodes all have a _name_ property.
We can use the __braces__ `{..}` to specify the key/value pair of _name_ and _Tom Hanks_ as the filter.
Expand All @@ -103,6 +119,9 @@ This query returns a single node that represents _Tom Hanks_.
In the graph view of Neo4j Browser, the node is visualized as a bubble.
You can also view the results returned in table view where you can view the properties of the node.

[.slide]
== Accessing Properties

In our Cypher statement, we can access properties using a _dot notation_.
For example, to return the _name_ property value using its property key `p.name`.

Expand All @@ -115,6 +134,9 @@ RETURN p.born

This query returns the value of the _born_ property of the _Tom Hanks_ node.

[.slide]
== Case Sensitivity

[IMPORTANT]
.Understanding Case Sensitivity
--
Expand All @@ -128,6 +150,9 @@ Neo4j best practices include:
* Use **UPPERCASE** for Cypher keywords.
--

[.slide]
== Using the WHERE Clause

Another way that you can filter queries is by using the `WHERE` clause, rather than specifying the property value inline with braces.

This query returns the same data as the previous query.
Expand All @@ -139,6 +164,9 @@ WHERE p.name = 'Tom Hanks'
RETURN p.born
----

[.slide]
== Adding more logic to your queries

As you gain more experience with Cypher, you will find that using `WHERE` to filter your queries is very powerful because you can add more logic to your `WHERE` clause.
Here is an example where we filter by two values for _name_.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
= Finding Relationships
:type: video
:order: 3
:slides: true

[.video]
video::aQ5hKd2DD0A[youtube,width=560,height=315]
Expand All @@ -17,6 +18,7 @@ https://docs.google.com/document/d/1EGqqe-05pSBVL5TTkRv_MoSkkhqQQBSjyS7pv9URyKE/


[.transcript]
[.slide.discrete]
== Finding relationships
[TIP]
.Our Goal
Expand All @@ -32,6 +34,10 @@ In the previous lesson, we used the `MATCH` clause to find the node in our datab
MATCH (p:Person {name: 'Tom Hanks'})
RETURN p
----

[.slide]
== Traversing Relationships

We can extend the pattern in the `MATCH` clause to _traverse_ through all relationships with a type of _ACTED_IN_ to any node.
Our domain model shows that the _ACTED_IN_ relationship goes in an outgoing direction from the _Person_ node so we can add the direction in our pattern.
We often refer to this as a *traversal*.
Expand All @@ -42,6 +48,9 @@ We often refer to this as a *traversal*.
MATCH (p:Person {name: 'Tom Hanks'})-[:ACTED_IN]->()
----

[.slide]
== Returning Related Nodes

Our data model dictates that the node at the other end of that relationship will be _Movie_ node, so we don't necessarily need to specify the _:Movie_ label in the node - instead we will use the variable _m_.

[source,cypher]
Expand All @@ -52,6 +61,9 @@ RETURN m.title

This code returns the titles of all movies that _Tom Hanks_ acted in.

[.slide]
== Specifying Node Labels

If our graph had different labels, for example Television and _Movie_ nodes this query would have returned all _Television_ and _Movie_ nodes that Tom Hanks acted in.
That is, if we had multiple _types_ of nodes at the end of the _ACTED_IN_ relationships in our graph, we could make sure that we only return movies.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
= Filtering Queries
:type: video
:order: 6
:slides: true

[.video]
video::ZBbH5p-5Gt8[youtube,width=560,height=315]
Expand All @@ -18,6 +19,7 @@ https://docs.google.com/document/d/1mZ6afZ6CfN0XADSc80NQS3kXxFN9hT_5qAYVVUeKr5c/


[.transcript]
[.slide.discrete]
== Filtering queries

Earlier, you learned that the `WHERE` clause is used to tell the query engine to filter what nodes are retrieved from the graph.
Expand All @@ -34,7 +36,8 @@ WHERE m.released = 2008 OR m.released = 2009
RETURN p, m
----

=== Filtering by node labels
[.slide]
== Filtering by node labels

You have already seen this type of query. It returns the names of all people who acted in the movie, _The Matrix_.

Expand All @@ -56,7 +59,8 @@ RETURN p.name

Both queries execute the same way, but you may want to use one style of filtering over another in your code.

=== Filtering using ranges
[.slide]
== Filtering using ranges

You can specify a range for filtering a query.
Here we want to retrieve _Person_ nodes of people who acted in movies released between _2000_ and _2003_:
Expand All @@ -68,7 +72,8 @@ WHERE 2000 <= m.released <= 2003
RETURN p.name, m.title, m.released
----

=== Filtering by existence of a property
[.slide]
== Filtering by existence of a property

Recall that by default, there is no requirement that a node or relationship has a given property.
Here is an example of a query where we only want to return _Movie_ nodes where _Jack Nicholson_ acted in the movie, and the movie has the _tagline_ property.
Expand All @@ -80,7 +85,8 @@ WHERE p.name='Jack Nicholson' AND m.tagline IS NOT NULL
RETURN m.title, m.tagline
----

=== Filtering by partial strings
[.slide]
== Filtering by partial strings

Cypher has a set of string-related keywords that you can use in your `WHERE` clauses to test string property values.
You can specify `STARTS WITH`, `ENDS WITH`, and `CONTAINS`.
Expand All @@ -104,7 +110,8 @@ WHERE toLower(p.name) STARTS WITH 'michael'
RETURN p.name
----

=== Filtering by patterns in the graph
[.slide]
== Filtering by patterns in the graph

Suppose you wanted to find all people who wrote a movie but did not direct that same movie.
Here is how you would perform the query:
Expand All @@ -116,15 +123,14 @@ WHERE NOT exists( (p)-[:DIRECTED]->(m) )
RETURN p.name, m.title
----

=== Filtering using lists
[.slide]
== Filtering using lists

If you have a set of values you want to test with, you can place them in a list or you can test with an existing list in the graph.
A Cypher list is a comma-separated set of values within square brackets.

You can define the list in the `WHERE` clause.
During the query, the graph engine will compare each property with the values `IN` the list.
You can place either numeric or string values in the list, but typically, elements of the list are of the same type of data.
If you are testing with a property of a string type, then all the elements of the list will be strings.

In this example, we only want to retrieve _Person_ nodes of people born in _1965_, _1970_, or _1975_:

Expand All @@ -135,6 +141,9 @@ WHERE p.born IN [1965, 1970, 1975]
RETURN p.name, p.born
----

[.slide]
== Filtering with existing lists in the graph

You can also compare a value to an existing list in the graph.

We know that the _:ACTED_IN_ relationship has a property, _roles_ that contains the list of roles an actor had in a particular movie they acted in.
Expand All @@ -147,7 +156,8 @@ WHERE 'Neo' IN r.roles AND m.title='The Matrix'
RETURN p.name, r.roles
----

=== What properties does a node or relationship have?
[.slide]
== What properties does a node or relationship have?

The properties for a node with a given label need not be the same.
One way you can discover the properties for a node is to use the `keys()` function.
Expand All @@ -164,7 +174,8 @@ RETURN p.name, keys(p)
The results returned for each row include the name of the person, followed by the list of property keys for that node.
If you scroll down in the result pane, you will notice that some Person nodes do not have a born property.

=== What properties exist in the graph?
[.slide]
== What properties exist in the graph?

More generally, you can run this code to return all the property keys defined in the graph.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
= Reading Data from Neo4j
:order: 1
:slides: true

[.slide.discrete]
== Introduction

In this module you will learn how to write Cypher code to retrieve data from the graph.

[.slide]
== What You Will Learn

You will learn how to:

* Retrieve nodes from the graph.
Expand All @@ -15,6 +21,7 @@ You will learn how to:

Using the Movies example dataset, you will create and execute Cypher code to find actors and movies in our graph.

[.slide]
== Domain model for this course

Here is the data model used in this course.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
= Creating Nodes
:type: video
:order: 1
:slides: true

[.video]
video::fgoz4eMQHOk[youtube,width=560,height=315]


[.transcript]
[.slide.discrete]
== Creating nodes

[NOTE]
.Video Transcript Correction
The actor's name for Michael Caine is misspelled in the video. It has been corrected in the transcript.

[.transcript]
== Creating nodes
In this lesson you will learn how to write Cypher code to create nodes in the graph.

Using the Movies data model, you will create and execute Cypher code to create actors and movies in our graph.
Expand All @@ -20,6 +24,9 @@ We use the `MERGE` keyword to create a _pattern_ in the database.
After the `MERGE` keyword, we specify the pattern that we want to create.
Usually this will be a single node or a relationship between two nodes.

[.slide]
== Creating a node

Suppose we want to create a node to represent _Michael Caine_.
Run this Cypher code to create the node.

Expand All @@ -41,7 +48,8 @@ RETURN p

// browser::MATCH (n) RETURN count(n)[]

=== Executing multiple Cypher clauses
[.slide]
== Executing multiple Cypher clauses

We can also chain multiple `MERGE` clauses together within a single Cypher code block.

Expand All @@ -55,6 +63,7 @@ RETURN p, m
This code creates two nodes, each with a primary key property.
Because we have specified the variables _p_ and _m_, we can use them in the code to return the created nodes.

[.slide]
== Using `CREATE` instead of `MERGE` to create nodes

Cypher has a `CREATE` clause you can use for creating nodes.
Expand Down
Loading