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
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ RETURN p
[.slide]
== Challenges

Complete the queries to anwser the following questions:
Complete the queries to answer the following questions:

. What is the lowest rated movie.
+
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
= Using Parameters
:type: lesson
:order: 7
:sandbox: true
:slides: true

[.slide]
== Parameters

Cypher statements will often include literal values such as names, dates, or numbers.

[source,cypher]
.A query with a literal value
----
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE p.name = "Tom Hanks"
RETURN m.title
----

[.slide.discrete]
== Parameters in Cypher

You can use parameters to represent these literal values in your Cypher statements.

[source,cypher]
.Parameterized query
----
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE p.name = $actorName
RETURN m.title
----

[NOTE]
.$ prefix
Parameters are prefixed with the `$` symbol in the query.

[.slide]
== Setting parameters

You can set the value for a single parameter using the `:param` command:

[source,cypher]
.Set the parameter actorName to "Tom Hanks"
----
:param actorName => "Tom Hanks"
----

The parameter can then be used in subsequent queries:

[source,cypher]
.Use the parameter in a query
----
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE p.name = $actorName
RETURN m.title
----

[.transcript-only]
====
[NOTE]
.Scope of parameters
=====
Parameters set using the `:param` command are scoped to your Neo4j Browser session.
Parameters persist until you close the Neo4j Browser or explicitly remove them.
=====
====

[.slide]
== Setting multiple parameters

You can also use the JSON-style syntax to set _all_ of the parameters in your Neo4j Browser session.

[source,cypher]
.Set two parameters
----
:params {actorName: "Tom Cruise", minimumRevenue: 100000000}
----

All the parameters you set using this approach will replace any previously set parameters:

[source,cypher]
.Use the parameters in a query
----
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE
p.name = $actorName AND
m.revenue > $minimumRevenue
RETURN m.title
----

[.transcript-only]
====
[NOTE]
.Specific types
=====
The parameters types you can specify in using this approach are numbers, strings, and booleans.
=====
====

[.slide]
== Viewing parameters

You can view the parameters currently set in your Neo4j Browser session by using the `:params` command.

[source,cypher]
.View parameters
----
:params
----

The command will return a JSON object showing all the parameters and their current values.

[source, json]
----
{
"actorName": "Tom Cruise",
"minimumRevenue": 100000000.0
}
----

[.slide]
== Performance benefits

Using parameters in your Cypher queries can improve your query performance:

* Neo4j will cache execution plans for queries that use parameters.
* The same query with different parameter values will not require a new execution plan to be generated.


[.slide]
== Challenges

Create parameters to ensure the following queries run successfully.

. Find the movies directed by [copy]#"Buster Keaton"# .
+
[.transcript-only]
====
[source, cypher]
----
MATCH (p:Person)-[:DIRECTED]->(m:Movie)
WHERE p.name = $directorName
RETURN m.title
----
====

. Find movies spoken in [copy]#["French"]# with an minimum average rating of [copy]#4# .
+
[.transcript-only]
====
[source, cypher]
----
MATCH (m:Movie)<-[r:RATED]-()
WITH m, avg(r.rating) as avgRating
WHERE
$language in m.languages AND
avgRating > $minimumRating
RETURN m.title, avgRating
----
====

[.transcript-only]
====
[%collapsible]
.Click to reveal the answers
=====
. Find the movies directed by "Buster Keaton".
+
[source, cypher]
----
:param directorName => "Buster Keaton"
----
+
[source, cypher]
----
MATCH (p:Person)-[:DIRECTED]->(m:Movie)
WHERE p.name = $directorName
RETURN m.title
----

. Find movies spoken in "French" with an minimum average rating of 4.
+
[source, cypher]
----
:params {language: "French", minimumRating: 4 }
----
+
[source, cypher]
----
MATCH (m:Movie)<-[r:RATED]-()
WITH m, avg(r.rating) as avgRating
WHERE
$language in m.languages AND
avgRating > $minimumRating
RETURN m.title, avgRating
----

=====
====


[next.discrete]
== Next

read::Continue[]

[.summary]
== Summary

In this lesson, you learned how to set parameter values and to use them in queries.