Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 13 additions & 2 deletions modules/ROOT/pages/clauses/where.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ CREATE (andy:Swedish:Person {name: 'Andy', age: 36}),
== Basic filtering

.Filter on a node label
// tag::clauses_where_node_label[]
[source, cypher]
----
MATCH (n)
WHERE n:Swedish
RETURN n.name AS name
----
// end::clauses_where_in_match_clause[]
// end::clauses_where_node_label[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand All @@ -62,12 +63,14 @@ RETURN n.name AS name
|===

.Filter on a node property
// tag::clauses_where_node_property[]
[source, cypher]
----
MATCH (n:Person)
WHERE n.age < 35
RETURN n.name AS name, n.age AS age
----
// end::clauses_where_node_property[]

.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand All @@ -80,12 +83,14 @@ RETURN n.name AS name, n.age AS age
|===

.Filter on a relationship property
// tag::clauses_where_relationship_property[]
[source, cypher]
----
MATCH (:Person {name:'Andy'})-[k:KNOWS]->(f)
WHERE k.since < 2000
RETURN f.name AS oldFriend
----
// end::clauses_where_relationship_property[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand All @@ -112,12 +117,14 @@ To filter on a property using a dynamically computed name, use square brackets `
----

.Filter on a dynamically computed node property
// tag::clauses_where_dynamic[]
[source, cypher]
----
MATCH (n:Person)
WHERE n[$propname] > 40
RETURN n.name AS name, n.age AS age
----
// end::clauses_where_dynamic[]

.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand Down Expand Up @@ -180,12 +187,14 @@ In this case, `name` is the only variable in scope for the `RETURN` clause.
=== Fixed-length patterns

.`WHERE` inside a node pattern
// tag::clauses_where_node_pattern[]
[source, cypher]
----
WITH 35 AS minAge
MATCH (a:Person WHERE a.name = 'Andy')-[:KNOWS]->(b:Person WHERE b.age > minAge)
RETURN b.name AS name
----
// end::clauses_where_node_pattern[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand Down Expand Up @@ -264,13 +273,15 @@ RETURN [(a)-[r:KNOWS WHERE r.since < minYear]->(b:Person) | r.since] AS years
If matching for variable length patterns, `WHERE` can only be used together with the xref:patterns/variable-length-patterns.adoc#quantified-path-patterns[quantified path pattern] or xref:patterns/variable-length-patterns.adoc#quantified-relationships[quantified relationships] syntax.

.Allowed - `WHERE` predicate inside a quantified relationship
// tag::clauses_where_var_length[]
[source, cypher]
----
MATCH p = (a:Person {name: "Andy"})-[r:KNOWS WHERE r.since < 2011]->{1,4}(:Person)
RETURN [n IN nodes(p) | n.name] AS paths
----
// end::clauses_where_var_length[]

Note that any path´s including `Timothy` and `Susan` are excluded by the `WHERE` predicate, since their incoming `KNOWS` relationships both have a `since` value that is higher than `2011.`
Note that any paths including `Timothy` and `Susan` are excluded by the `WHERE` predicate, since their incoming `KNOWS` relationships both have a `since` value that is higher than `2011.`

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand Down
9 changes: 9 additions & 0 deletions modules/ROOT/pages/expressions/conditional-expressions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ END
[[case-simple-examples]]
=== Example

// tag::expressions_conditional_expressions_simple[]
[source, cypher]
----
MATCH (n:Person)
Expand All @@ -85,6 +86,8 @@ CASE n.eyes
ELSE 3
END AS result, n.eyes
----
// end::expressions_conditional_expressions_simple[]


[role="queryresult",options="header,footer",cols="2*<m"]
|===
Expand Down Expand Up @@ -148,6 +151,7 @@ END
[[case-extended-simple-examples]]
=== Example

// tag::expressions_conditional_expressions_extended_simple[]
[source, cypher]
----
MATCH (n:Person)
Expand All @@ -162,6 +166,7 @@ CASE n.age
ELSE "Adult"
END AS result
----
// end::expressions_conditional_expressions_extended_simple[]

[role="queryresult",options="header,footer",cols="2*<m"]
|===
Expand Down Expand Up @@ -212,6 +217,7 @@ In this case, the predicate is tested to find a valid alternative.
[[case-generic-examples]]
=== Example

// tag::expressions_conditional_expressions_generic[]
[source, cypher]
----
MATCH (n:Person)
Expand All @@ -222,6 +228,7 @@ CASE
ELSE 3
END AS result, n.eyes, n.age
----
// end::expressions_conditional_expressions_generic[]

[role="queryresult",options="header,footer",cols="3*<m"]
|===
Expand Down Expand Up @@ -299,6 +306,7 @@ For more information about `null`, see xref:values-and-types/working-with-null.a

The results of a `CASE` expression can be used to set properties on a node or relationship.

// tag::expressions_conditional_expressions_properties[]
[source, cypher]
----
MATCH (n:Person)
Expand All @@ -311,6 +319,7 @@ END AS colorCode
SET n.colorCode = colorCode
RETURN n.name, n.colorCode
----
// end::expressions_conditional_expressions_properties[]

[role="queryresult",options="header,footer",cols="2*<m"]
|===
Expand Down
30 changes: 30 additions & 0 deletions modules/ROOT/pages/expressions/list-expressions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ The subscript operator, `[]`, can be used to access specific elements in a `LIST
`[-1]` refers to the last element in a `LIST`, `[-2]` to the penultimate element, and so on.

.Access individual elements in a `LIST`
// tag::expressions_list_element_access[]
[source, cypher]
----
WITH [1, 2, 3, 4] AS list
RETURN list[0] AS firstElement,
list[2] AS thirdElement,
list[-1] AS finalElement
----
// end::expressions_list_element_access[]

.Result
[role="queryresult",options="header,footer",cols="3*<m"]
Expand Down Expand Up @@ -87,11 +89,13 @@ RETURN list[$myIndex] AS secondElement
|===

.Access a `LIST` within a nested `LIST`
// tag::expressions_list_nested_list_access[]
[source, cypher]
----
WITH [[1, 2], [3, 4], [5, 6]] AS nestedList
RETURN nestedList[1] AS secondList
----
// end::expressions_list_nested_list_access[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand All @@ -104,12 +108,14 @@ RETURN nestedList[1] AS secondList
|===

.Access specific elements in a nested `LIST`
// tag::expressions_list_nested_list_specific_element_access[]
[source, cypher]
----
WITH [[1, 2], [3, 4], [5, 6]] AS nestedList
RETURN nestedList[1] AS secondList,
nestedList[1][0] AS firstElementOfSecondList
----
// end::expressions_list_nested_list_specific_element_access[]

.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand Down Expand Up @@ -189,13 +195,15 @@ This allows for extracting a subset of a `LIST` rather than a single element.
List slicing is inclusive at the start of the range, but exclusive at the end (e.g. `list[start..end]` includes `start`, but excludes `end`).

.Slice operations on a `LIST`
// tag::expressions_list_slicing[]
[source, cypher]
----
WITH [1, 2, 3, 4, 5, 6] AS list
RETURN list[2..4] AS middleElements,
list[..2] AS noLowerBound,
list[2..] AS noUpperBound
----
// end::expressions_list_slicing[]

.Result
[role="queryresult",options="header,footer",cols="3*<m"]
Expand All @@ -210,13 +218,15 @@ RETURN list[2..4] AS middleElements,
Negative indexing in list slicing references elements from the end of the `LIST`; `..-1` excludes the last element, `..-2` excludes the last two elements, and so on.

.Negative indexing and list slicing
// tag::expressions_list_negative_slicing[]
[source, cypher]
----
WITH [1, 2, 3, 4, 5, 6] AS list
RETURN list[..-1] AS finalElementRemoved,
list[..-2] AS finalTwoElementsRemoved,
list[-3..-1] AS removedFirstThreeAndLast
----
// end::expressions_list_negative_slicing[]

.Result
[role="queryresult",options="header,footer",cols="3*<m"]
Expand Down Expand Up @@ -251,11 +261,13 @@ RETURN nestedList[0..2] AS slicedNestedList
Slicing inner `LIST` values require two `[]` operators; the first `[]` accesses elements from the outer `LIST`, while the second slices or accesses elements from the inner `LIST`.

.Slice inner `LIST`
// tag::expressions_list_slice_inner_list[]
[source, cypher]
----
WITH [[1, 2, 3], [4, 5, 6], [7, 8, 9]] AS nestedList
RETURN nestedList[1][0..2] AS slicedInnerList
----
// end::expressions_list_slice_inner_list[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand Down Expand Up @@ -293,11 +305,13 @@ Cypher contains two list concatenation operators: `||` and `+`.
`||` is xref:appendix/gql-conformance/index.adoc[GQL conformant], while `+` is not.

.List concatenation using `||` and `+`
// tag::expressions_list_concatenation[]
[source, cypher]
----
RETURN [1,2] || [3,4] AS list1,
[1,2] + [3,4] AS list2
----
// end::expressions_list_concatenation[]

.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand Down Expand Up @@ -353,12 +367,14 @@ The `+` operator can add elements to the beginning or end of a `LIST` value.
This is not possible using the `||` operator.

.Add elements to the beginning and end of a `LIST`
// tag::expressions_list_add_elements[]
[source, cypher]
----
WITH [1, 2, 3, 4] AS list
RETURN 0 + list AS newBeginning,
list + 5 AS newEnd
----
// end::expressions_list_add_elements[]

.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand Down Expand Up @@ -458,11 +474,13 @@ RETURN [x IN range(0,5) | x * 10] AS result


.List comprehension with both an expression and filtering
// tag::expressions_list_comprehension[]
[source, cypher]
----
WITH [1, 2, 3, 4, 5] AS list
RETURN [n IN list WHERE n > 2 | n] AS filteredList
----
// end::expressions_list_comprehension[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand Down Expand Up @@ -498,13 +516,15 @@ RETURN [listIndex IN range(0, size(list)-1) | toString(listIndex) || ': ' || toS
The below query iterates over the `skills` property of each `Person` node and creates a new `LIST` by xref:expressions/string-operators.adoc[concatenating the `STRING`] `" expert"` to each element in `skills`.

.Modify `LIST` properties using list comprehension
// tag::expressions_list_comprehension_properties[]
[source, cypher]
----
MATCH (p:Person) WHERE p.skills IS NOT NULL
ORDER BY p.name
RETURN p.name AS name,
[skill IN p.skills | skill + " expert"] AS modifiedSkills
----
// end::expressions_list_comprehension_properties[]

.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand All @@ -524,11 +544,13 @@ RETURN p.name AS name,
The next query uses the xref:functions/aggregating.adoc#functions-collect[`collect()`] function to gather all `Person` nodes into a `LIST`, and the `WHERE 'Python' IN person.skills` predicate filters that list to include only those nodes whose `skills` property contains `Python`.

.List comprehension with a `WHERE` predicate
// tag::expressions_list_comprehension_where[]
[source, cypher]
----
MATCH (p:Person)
RETURN [person IN collect(p) WHERE 'Python' IN person.skills | person.name] AS pythonExperts
----
// end::expressions_list_comprehension_where[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand All @@ -543,10 +565,12 @@ RETURN [person IN collect(p) WHERE 'Python' IN person.skills | person.name] AS p
List comprehension can be used to remove any unknown `NULL` values when concatenating `LIST` values.

.List comprehension to remove `NULL` values during list concatenation
// tag::expressions_list_comprehension_concatenation_remove_null[]
[source, cypher]
----
RETURN [x IN ([1, null, 3] || [null, 5, null]) WHERE x IS NOT NULL] AS listWithoutNull
----
// end::expressions_list_comprehension_concatenation_remove_null[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand All @@ -572,11 +596,13 @@ Pattern comprehension is used to create new `LIST` values by matching graph patt
The below query retrieves a list of names of people who work for `Alice` by using pattern comprehension extract the names of `employees` into a `LIST`.

.Pattern comprehension on a fixed-length pattern
// tag::expressions_list_pattern_comprehension[]
[source, cypher]
----
MATCH (alice:Person {name: 'Alice'})
RETURN [(employee:Person)-[:WORKS_FOR]->(alice) | employee.name] AS employees
----
// end::expressions_list_pattern_comprehension[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand All @@ -591,11 +617,13 @@ RETURN [(employee:Person)-[:WORKS_FOR]->(alice) | employee.name] AS employees
Pattern comprehensions can include `WHERE` predicates.

.Pattern comprehension including a `WHERE` predicate
// tag::expressions_list_pattern_comprehension_where[]
[source, cypher]
----
MATCH (alice:Person {name: 'Alice'})
RETURN [(employee:Person)-[:WORKS_FOR]->(alice) WHERE employee.age > 30 | employee.name || ', ' || toString(employee.age)] AS employeesAbove30
----
// end::expressions_list_pattern_comprehension_where[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand All @@ -622,6 +650,7 @@ The below query uses a pattern comprehension to collect the skills of all superi
The xref:functions/list.adoc#functions-reduce[`reduce()`] function concatenates these skills into a single `LIST`, and xref:clauses/unwind.adoc[`UNWIND`] is used to flatten this `LIST` before returning the distinct skills in a new `LIST`.

.Allowed: variable-length pattern comprehension using variable-length relationship syntax
// tag::expressions_list_pattern_comprehension_var_length[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one and contains lines that are so long that they don't render nicely in the cheat sheet 🤔
it's a minor thing (formatting)

[source, cypher]
----
MATCH (cecil:Person {name: 'Cecil'})
Expand All @@ -630,6 +659,7 @@ WITH reduce(accumulatedSkills = [], superiorSkills IN allSuperiorsSkills | accum
UNWIND allSkills AS superiorsSkills
RETURN collect(DISTINCT superiorsSkills) AS distinctSuperiorsSkills
----
// end::expressions_list_pattern_comprehension_var_length[]

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