Skip to content

Commit

Permalink
tags for clause section (#1138)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsill-neo4j authored Jan 14, 2025
1 parent 09ee930 commit d20cb4b
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 4 deletions.
4 changes: 3 additions & 1 deletion modules/ROOT/pages/clauses/finish.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
[[query-finish]]
= FINISH

A query ending in `FINISH` instead of `RETURN` has no result but executes all its side effects.
A query ending in `FINISH` -- instead of `RETURN` -- has no result but executes all its side effects.
`FINISH` was introduced as part of Cypher's xref:appendix/gql-conformance/index.adoc[].

The following read query successfully executes but has no results:

.Query
// tag::clauses_finish_match[]
[source, cypher]
----
MATCH (p:Person)
FINISH
----
// end::clauses_finish_match[]

The following query has no result but creates one node with the label `Person`:

Expand Down
48 changes: 47 additions & 1 deletion modules/ROOT/pages/clauses/foreach.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ CREATE
[[foreach-mark-all-nodes-along-a-path]]
== Mark all nodes along a path

This query will set the property `marked` to `true` on all nodes along a path.
This query sets the property `marked` to `true` on all nodes along a path.

.Query
// tag::clauses_foreach_node[]
[source, cypher, indent=0]
----
MATCH p=(start)-[*]->(finish)
WHERE start.name = 'A' AND finish.name = 'D'
FOREACH (n IN nodes(p) | SET n.marked = true)
----
// end::clauses_foreach_node[]

.Result
[role="queryresult",options="footer",cols="1*<m"]
Expand All @@ -51,3 +53,47 @@ d|Rows: 0 +
Properties set: 4
|===


[[foreach-mark-all-relationships-along-a-path]]
== Mark all relationships along a path

This query sets the property `marked` to `true` on all relationships along a path.

// tag::clauses_foreach_relationship[]
[source, cypher, indent=0]
----
MATCH p=(start)-[*]->(finish)
WHERE start.name = 'A' AND finish.name = 'D'
FOREACH ( r IN relationships(p) | SET r.marked = true )
----
// end::clauses_foreach_relationship[]

.Result
[role="queryresult",options="footer",cols="1*<m"]
|===
|(empty result)
d|Rows: 0 +
Properties set: 3
|===

[[foreach-create-new-nodes-form-a-list]]
== Create new nodes from a list of name labels

This query creates a new node for each label in a list.

.Query
// tag::clauses_foreach_create[]
[source, cypher, indent=0]
----
WITH ['E', 'F', 'G'] AS names
FOREACH ( value IN names | CREATE (:Person {name: value}) )
----
// end::clauses_foreach_create[]

.Result
[role="queryresult",options="footer",cols="1*<m"]
|===
1+|(empty result)
1+d|Rows: 0 +
Nodes created: 3
|===
4 changes: 4 additions & 0 deletions modules/ROOT/pages/clauses/limit.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,14 @@ Properties set: 1
`LIMIT` can be used as a standalone clause, or in conjunction with xref:clauses/order-by.adoc[`ORDER BY`] or xref:clauses/skip.adoc[`SKIP`]/xref:clauses/skip.adoc#offset-synonym[`OFFSET`].

.Standalone use of `LIMIT`
// tag::clauses_limit_standalone[]
[source, cypher]
----
MATCH (n)
LIMIT 2
RETURN collect(n.name) AS names
----
// end::clauses_limit_standalone[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand All @@ -185,6 +187,7 @@ The following query orders all nodes by `name` descending, skips the two first r
It then xref:functions/aggregating.adoc#functions-collect[collects] the results in a list.

.`LIMIT` used in conjunction with `ORDER BY` and `SKIP`
// tag::clauses_limit[]
[source, cypher]
----
MATCH (n)
Expand All @@ -193,6 +196,7 @@ SKIP 2
LIMIT 2
RETURN collect(n.name) AS names
----
// end::clauses_limit[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand Down
16 changes: 15 additions & 1 deletion modules/ROOT/pages/clauses/load-csv.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ By default, paths are resolved relative to the Neo4j import directory.
----
.Query
// tag::clauses_load_csv_local_files[]
[source, cypher]
----
LOAD CSV FROM 'file:///artists.csv' AS row
MERGE (a:Artist {name: row[1], year: toInteger(row[2])})
RETURN a.name, a.year
----
// end::clauses_load_csv_local_files[]
.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand Down Expand Up @@ -112,12 +114,14 @@ However, this means that insecure URLs on virtual hosts will not function unless
----
.Query
// tag::clauses_load_csv_remote_locations[]
[source, cypher]
----
LOAD CSV FROM 'https://data.neo4j.com/bands/artists.csv' AS row
MERGE (a:Artist {name: row[1], year: toInteger(row[2])})
RETURN a.name, a.year
----
// end::clauses_load_csv_remote_locations[]
.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand Down Expand Up @@ -705,10 +709,11 @@ person_tmdbId,bio,born,bornIn,died,person_imdbId,name,person_poster,person_url
----
[NOTE]
The below query uses a xref:subqueries/call-subquery.adoc#variable-scope-clause[variable scope clause] (introduced in Neo4j 5.23) to import variables into the `CALL` subquery.
The query below uses a xref:subqueries/call-subquery.adoc#variable-scope-clause[variable scope clause] (introduced in Neo4j 5.23) to import variables into the `CALL` subquery.
If you are using an older version of Neo4j, use an xref:subqueries/call-subquery.adoc#importing-with[importing `WITH` clause] instead.
.Query
// tag::clauses_load_csv_transactions[]
[source, cypher]
----
LOAD CSV WITH HEADERS FROM 'https://data.neo4j.com/importing-cypher/persons.csv' AS row
Expand All @@ -717,6 +722,7 @@ CALL (row) {
SET p.name = row.name, p.born = row.born
} IN TRANSACTIONS OF 200 ROWS
----
// end::clauses_load_csv_transactions[]
.Result
[source, role="queryresult"]
Expand Down Expand Up @@ -751,11 +757,13 @@ A common use case for this function is to generate sequential unique IDs for CSV
----
.Query
// tag::clauses_load_csv_linenumber[]
[source, cypher]
----
LOAD CSV FROM 'file:///artists.csv' AS row
RETURN linenumber() AS number, row
----
// end::clauses_load_csv_linenumber[]
.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand Down Expand Up @@ -786,11 +794,13 @@ The xref:functions/load-csv.adoc#functions-file[`file()`] function provides the
----
.Query
// tag::clauses_load_csv_file[]
[source, cypher, role=test-result-skip]
----
LOAD CSV FROM 'file:///artists.csv' AS row
RETURN DISTINCT file() AS path
----
// end::clauses_load_csv_file[]
.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand Down Expand Up @@ -837,6 +847,7 @@ Id,Name,Year
----
.Query
// tag::clauses_load_csv_headers[]
[source, cypher]
----
LOAD CSV WITH HEADERS FROM 'file:///artists-with-headers.csv' AS row
Expand All @@ -845,6 +856,7 @@ RETURN
a.name AS name,
a.year AS year
----
// end::clauses_load_csv_headers[]
.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand Down Expand Up @@ -880,11 +892,13 @@ If you try to import a file that doesn't use `,` as field delimiter and you also
----
.Query
// tag::clauses_load_csv_field_terminator[]
[source, cypher]
----
LOAD CSV FROM 'file:///artists-fieldterminator.csv' AS row FIELDTERMINATOR ';'
MERGE (:Artist {name: row[1], year: toInteger(row[2])})
----
// end::clauses_load_csv_field_terminator[]
.Result
[source, role="queryresult"]
Expand Down
8 changes: 8 additions & 0 deletions modules/ROOT/pages/clauses/order-by.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ CREATE
`ORDER BY` is used to sort the output.

.Query
// tag::clauses_order_by[]
[source, cypher]
----
MATCH (n)
RETURN n.name, n.age
ORDER BY n.name
----
// end::clauses_order_by[]

The nodes are returned, sorted by their name.

Expand All @@ -67,12 +69,14 @@ You can order by multiple properties by stating each variable in the `ORDER BY`
Cypher will sort the result by the first variable listed, and for equals values, go to the next property in the `ORDER BY` clause, and so on.

.Query
// tag::clauses_order_by_multiple[]
[source, cypher]
----
MATCH (n)
RETURN n.name, n.age
ORDER BY n.age, n.name
----
// end::clauses_order_by_multiple[]

This returns the nodes, sorted first by their age, and then by their name.

Expand Down Expand Up @@ -246,12 +250,14 @@ Read more about this capability in xref::indexes/search-performance-indexes/usin


.Standalone use of `ORDER BY`
// tag::clauses_order_by_standalone[]
[source, cypher]
----
MATCH (n)
ORDER BY n.name
RETURN collect(n.name) AS names
----
// end::clauses_order_by_standalone[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand All @@ -264,6 +270,7 @@ RETURN collect(n.name) AS names
The following query orders all nodes by `name` descending, skips the first row and limits the results to one row.

.`ORDER BY` used in conjunction with `SKIP` and `LIMIT`
// tag::clauses_order_by_descending[]
[source, cypher]
----
MATCH (n)
Expand All @@ -272,6 +279,7 @@ SKIP 1
LIMIT 1
RETURN n.name AS name
----
// end::clauses_order_by_descending[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand Down
6 changes: 6 additions & 0 deletions modules/ROOT/pages/clauses/skip.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ d|Rows: 2
The following query returns the middle two rows, with `SKIP` skipping the first and xref:clauses/limit.adoc[`LIMIT`] removing the final two.
.Query
// tag::clauses_skip[]
[source, cypher]
----
MATCH (n)
Expand All @@ -77,6 +78,7 @@ ORDER BY n.name
SKIP 1
LIMIT 2
----
// end::clauses_skip[]
.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand Down Expand Up @@ -126,12 +128,14 @@ d|Rows: 4
`SKIP` can be used as a standalone clause, or in conjunction with xref:clauses/order-by.adoc[`ORDER BY`] or xref:clauses/limit.adoc[`LIMIT`].

.Standalone use of `SKIP`
// tag::clauses_skip_standalone[]
[source, cypher]
----
MATCH (n)
SKIP 2
RETURN collect(n.name) AS names
----
// end::clauses_skip_standalone[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand Down Expand Up @@ -169,6 +173,7 @@ RETURN collect(n.name) AS names
`OFFSET` was introduced as part of Cypher's xref:appendix/gql-conformance/index.adoc[] and can be used as a synonym to `SKIP`.

.Query
// tag::clauses_skip_offset[]
[source, cypher]
----
MATCH (n)
Expand All @@ -177,6 +182,7 @@ OFFSET 2
LIMIT 2
RETURN collect(n.name) AS names
----
// end::clauses_skip_offset[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand Down
6 changes: 6 additions & 0 deletions modules/ROOT/pages/clauses/unwind.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ The `UNWIND` clause requires you to specify a new name for the inner values.
We want to transform the literal list into rows named `x` and return them.

.Query
// tag::clauses_unwind_list[]
[source, cypher]
----
UNWIND [1, 2, 3, null] AS x
RETURN x, 'val' AS y
----
// end::clauses_unwind_list[]

Each value of the original list -- including `null` -- is returned as an individual row.

Expand Down Expand Up @@ -109,13 +111,15 @@ The two lists -- _a_ and _b_ -- are concatenated to form a new list, which is th
Multiple `UNWIND` clauses can be chained to unwind nested list elements.

.Query
// tag::clauses_unwind_nested_list[]
[source, cypher]
----
WITH [[1, 2], [3, 4], 5] AS nested
UNWIND nested AS x
UNWIND x AS y
RETURN y
----
// end::clauses_unwind_nested_list[]

The first `UNWIND` results in three rows for `x`, each of which contains an element of the original list (two of which are also lists); namely, `[1, 2]`, `[3, 4]`, and `5`.
The second `UNWIND` then operates on each of these rows in turn, resulting in five rows for `y`.
Expand Down Expand Up @@ -212,13 +216,15 @@ Create a number of nodes and relationships from a parameter-list without using `
----

.Query
// tag::clauses_unwind_create_nodes[]
[source, cypher]
----
UNWIND $events AS event
MERGE (y:Year {year: event.year})
MERGE (y)<-[:IN]-(e:Event {id: event.id})
RETURN e.id AS x ORDER BY x
----
// end::clauses_unwind_create_nodes[]

Each value of the original list is unwound and passed through `MERGE` to find or create the nodes and relationships.

Expand Down
Loading

0 comments on commit d20cb4b

Please sign in to comment.