Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tags for READ query section #1133

Merged
merged 8 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 11 additions & 1 deletion modules/ROOT/pages/clauses/match.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
:description: The `MATCH` clause is used to search for the pattern described in it.

Check failure on line 1 in modules/ROOT/pages/clauses/match.adoc

View workflow job for this annotation

GitHub Actions / docs-verify-pr / log-report

modules/ROOT/pages/clauses/match.adoc

target of image not found: graph_clauses_match_clause.svg
include::https://raw.githubusercontent.com/neo4j-graphacademy/courses/main/asciidoc/ads/data-analysis.adoc[]

= MATCH
Expand All @@ -11,7 +11,7 @@

The following graph is used for the examples below:

image::graph_match_clause.svg[width="500",role="middle"]
image::graph_clauses_match_clause.svg[width="500",role="middle"]
rsill-neo4j marked this conversation as resolved.
Show resolved Hide resolved

To recreate the graph, run the following query against an empty Neo4j database:

Expand Down Expand Up @@ -45,11 +45,13 @@
By specifying a pattern with a single node and no labels, all nodes in the graph will be returned.

.Find all nodes in a graph
// tag::clauses_match_all_nodes[]
[source, cypher]
----
MATCH (n)
RETURN n
----
// end::clauses_match_all_nodes[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand All @@ -70,11 +72,13 @@
=== Find nodes with a specific label

.Find all nodes with the `Movie` label
// tag::clauses_match_label[]
[source, cypher]
----
MATCH (movie:Movie)
RETURN movie.title
----
// end::clauses_match_label[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand Down Expand Up @@ -187,11 +191,13 @@
It is possible to introduce a variable to a pattern, either for filtering on relationship properties or to return a relationship.

.Find the types of an aliased relationship
// tag::clauses_match_relationship_types[]
[source, cypher]
----
MATCH (:Person {name: 'Oliver Stone'})-[r]->()
RETURN type(r) AS relType
----
// end::clauses_match_relationship_types[]

[NOTE]
The above query uses the xref:functions/scalar.adoc#functions-type[`type()` function].
Expand Down Expand Up @@ -236,11 +242,13 @@
It is possible to specify the type of a relationship in a relationship pattern by using a colon (`:`) before the relationship type.

.Relationship pattern filtering on the `ACTED_IN` relationship type
// tag::clauses_match_relationship[]
[source, cypher]
----
MATCH (:Movie {title: 'Wall Street'})<-[:ACTED_IN]-(actor:Person)
RETURN actor.name AS actor
----
// end::clauses_match_relationship[]

.Result
[source, role="queryresult",options="header,footer",cols="1*<m"]
Expand Down Expand Up @@ -389,11 +397,13 @@
The `MATCH` clause can also be used to bind whole paths to variables.

.Find all paths matching a pattern
// tag::clauses_match_path[]
[source, cypher]
----
MATCH path = ()-[:ACTED_IN]->(movie:Movie)
RETURN path
----
// end::clauses_match_path[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand Down
2 changes: 2 additions & 0 deletions modules/ROOT/pages/clauses/optional-match.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ This is because the second `MATCH` clause returns no data (there are no `DIRECTE
However, replacing the second `MATCH` clause with `OPTIONAL MATCH` does return results.
This is because, unlike `MATCH`, `OPTIONAL MATCH` enables the value `null` to be passed between clauses.

// tag::clauses_optional_match[]
[source, cypher]
----
MATCH (p:Person {name: 'Martin Sheen'})
OPTIONAL MATCH (p)-[r:DIRECTED]->()
RETURN p.name, r
----
// end::clauses_optional_match[]

.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand Down
12 changes: 12 additions & 0 deletions modules/ROOT/pages/clauses/return.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ CREATE
To return a node, list it in the `RETURN` clause:

.Query
// tag::clauses_return_node[]
[source, cypher]
----
MATCH (p:Person {name: 'Keanu Reeves'})
RETURN p
----
// end::clauses_return_node[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand All @@ -53,11 +55,13 @@ d|Rows: 1
To return a relationship type, list it in the `RETURN` clause:

.Query
// tag::clauses_return_relationship_type[]
[source, cypher]
----
MATCH (p:Person {name: 'Keanu Reeves'})-[r:ACTED_IN]->(m)
RETURN type(r)
----
// end::clauses_return_relationship_type[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand All @@ -74,11 +78,13 @@ d|Rows: 1
To return a specific property, use the dot separator:

.Query
// tag::clauses_return_property[]
[source, cypher]
----
MATCH (p:Person {name: 'Keanu Reeves'})
RETURN p.bornIn
----
// end::clauses_return_property[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand All @@ -101,11 +107,13 @@ This will improve performance.
To return all nodes, relationships and paths found in a query, use the `*` symbol:

.Query
// tag::clauses_return_all_elements[]
[source, cypher]
----
MATCH p = (keanu:Person {name: 'Keanu Reeves'})-[r]->(m)
RETURN *
----
// end::clauses_return_all_elements[]

This returns the two nodes, and the two possible paths between them.

Expand Down Expand Up @@ -149,11 +157,13 @@ d|Rows: 1
Names of returned columns can be renamed using the `AS` operator:

.Query
// tag::clauses_return_with_column_alias[]
[source, cypher]
----
MATCH (p:Person {name: 'Keanu Reeves'})
RETURN p.nationality AS citizenship
----
// end::clauses_return_with_column_alias[]

Returns the `nationality` property of `'Keanu Reeves'`, but the column is renamed to `citizenship`.

Expand Down Expand Up @@ -220,11 +230,13 @@ Returns a predicate, a literal and function call with a pattern expression param
`DISTINCT` retrieves only unique rows for the columns that have been selected for output.

.Query
// tag::clauses_return_distinct[]
[source, cypher]
----
MATCH (p:Person {name: 'Keanu Reeves'})-->(m)
RETURN DISTINCT m
----
// end::clauses_return_distinct[]

The `Movie` node `'Man of Tai Chi'` is returned by the query, but only once (without the `DISTINCT` operator it would have been returned twice because there are two relationships going to it from `'Keanu Reeves'`):

Expand Down
6 changes: 6 additions & 0 deletions modules/ROOT/pages/clauses/union.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ CREATE (johnny:Actor {name: 'Johnny Depp'}),
Combining the results from two queries is done using `UNION ALL`.

.Query
// tag::clauses_union_all[]
[source, cypher]
----
MATCH (n:Actor)
Expand All @@ -53,6 +54,7 @@ UNION ALL
MATCH (n:Movie)
RETURN n.title AS name
----
// end::clauses_union_all[]

The combined result is returned, including duplicates.

Expand All @@ -74,6 +76,7 @@ The combined result is returned, including duplicates.
By not including `ALL` in the `UNION`, duplicates are removed from the combined result set.

.Query
// tag::clauses_union[]
[source, cypher]
----
MATCH (n:Actor)
Expand All @@ -82,6 +85,7 @@ UNION
MATCH (n:Movie)
RETURN n.title AS name
----
// end::clauses_union[]

The combined result is returned, without duplicates.

Expand All @@ -103,6 +107,7 @@ Removal of duplicates can also be accomplished by explicitly including `DISTINCT
The `UNION DISTINCT` keyword was introduced as part of Cypher's xref:appendix/gql-conformance/index.adoc[], and using it is functionally the same as using simple `UNION`.

.Query
// tag::clauses_union_distinct[]
[source, cypher]
----
MATCH (n:Actor)
Expand All @@ -111,6 +116,7 @@ UNION DISTINCT
MATCH (n:Movie)
RETURN n.title AS name
----
// end::clauses_union_distinct[]

The combined result is returned, without duplicates.

Expand Down
20 changes: 20 additions & 0 deletions modules/ROOT/pages/clauses/where.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ CREATE
`WHERE` can appear inside a node pattern in a `MATCH` clause or a pattern comprehension:

.Query
// tag::clauses_where_in_match_clause[]
[source, cypher]
----
WITH 30 AS minAge
MATCH (a:Person WHERE a.name = 'Andy')-[:KNOWS]->(b:Person WHERE b.age > minAge)
RETURN b.name
----
// end::clauses_where_in_match_clause[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand All @@ -66,11 +68,13 @@ When used this way, predicates in `WHERE` can reference the node variable that t
The same rule applies to pattern comprehensions:

.Query
// tag::clauses_where_pattern_comprehension[]
[source, cypher]
----
MATCH (a:Person {name: 'Andy'})
RETURN [(a)-->(b WHERE b:Person) | b.name] AS friends
----
// end::clauses_where_pattern_comprehension[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand All @@ -87,6 +91,7 @@ The following boolean operators can be used with the `WHERE` clause: `AND`, `OR`
For more information on how operators work with `null`, see the chapter on xref::values-and-types/working-with-null.adoc[Working with null].

.Query
// tag::clauses_where_boolean_operations[]
[source, cypher]
----
MATCH (n:Person)
Expand All @@ -96,6 +101,7 @@ RETURN
n.age AS age
ORDER BY name
----
// end::clauses_where_boolean_operations[]

.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand All @@ -114,12 +120,14 @@ ORDER BY name
To filter nodes by label, write a label predicate after the `WHERE` keyword using `WHERE n:foo`:

.Query
// tag::clauses_where_node_label[]
[source, cypher]
----
MATCH (n)
WHERE n:Swedish
RETURN n.name, n.age
----
// end::clauses_where_node_label[]

The `name` and `age` values for `Andy` are returned:

Expand All @@ -138,12 +146,14 @@ The `name` and `age` values for `Andy` are returned:
To filter on a node property, write your clause after the `WHERE` keyword:

.Query
// tag::clauses_where_node_property[]
[source, cypher]
----
MATCH (n:Person)
WHERE n.age < 30
RETURN n.name, n.age
----
// end::clauses_where_node_property[]

The `name` and `age` values for `Timothy` are returned because he is less than 30 years of age:

Expand All @@ -162,12 +172,14 @@ The `name` and `age` values for `Timothy` are returned because he is less than 3
To filter on a relationship property, write your clause after the `WHERE` keyword:

.Query
// tag::clauses_where_relationship_property[]
[source, cypher]
----
MATCH (n:Person)-[k:KNOWS]->(f)
WHERE k.since < 2000
RETURN f.name, f.age, f.email
----
// end::clauses_where_relationship_property[]

The `name`, `age` and `email` values for `Peter` are returned because `Andy` has known him since before 2000:

Expand Down Expand Up @@ -211,12 +223,14 @@ The `name` and `age` values for `Timothy` are returned because he is less than 3
Use the `IS NOT NULL` predicate to only include nodes or relationships in which a property exists:

.Query
// tag::clauses_where_property_existence[]
[source, cypher]
----
MATCH (n:Person)
WHERE n.belt IS NOT NULL
RETURN n.name, n.belt
----
// end::clauses_where_property_existence[]

The `name` and `belt` values for `Andy` are returned because he is the only one with a `belt` property:

Expand All @@ -235,13 +249,15 @@ The `name` and `belt` values for `Andy` are returned because he is the only one
As `WHERE` is not considered a clause in its own right, its scope is not limited by a `WITH` directly before it.

.Query
// tag::clauses_where_with[]
[source, cypher]
----
MATCH (n:Person)
WITH n.name as name
WHERE n.age = 25
RETURN name
----
// end::clauses_where_with[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand Down Expand Up @@ -531,6 +547,7 @@ The following sections will demonstrate how to use path pattern expressions in a
=== Filter on patterns

.Query
// tag::clauses_where_patterns[]
[source, cypher]
----
MATCH
Expand All @@ -539,6 +556,7 @@ MATCH
WHERE (other)-->(timothy)
RETURN other.name, other.age
----
// end::clauses_where_patterns[]

The `name` and `age` values for nodes that have an outgoing relationship to `Timothy` are returned:

Expand Down Expand Up @@ -611,12 +629,14 @@ To check if an element exists in a list, use the `IN` operator.
The below query checks whether a property exists in a literal list:

.Query
// tag::clauses_where_lists[]
[source, cypher]
----
MATCH (a:Person)
WHERE a.name IN ['Peter', 'Timothy']
RETURN a.name, a.age
----
// end::clauses_where_lists[]

.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand Down
Loading
Loading