diff --git a/modules/ROOT/pages/clauses/where.adoc b/modules/ROOT/pages/clauses/where.adoc index d9b3a7302..1ec5a6bfd 100644 --- a/modules/ROOT/pages/clauses/where.adoc +++ b/modules/ROOT/pages/clauses/where.adoc @@ -479,25 +479,27 @@ The `name` and `age` for `Andy` are returned because his name starts with 'AND' 2+|Rows: 1 |=== +[[path-pattern-expressions]] +== Path pattern expressions -[[query-where-patterns]] -== Using path patterns in `WHERE` +Similar to xref::subqueries/existential.adoc[existential subqueries], path pattern expressions can be used to assert whether a specified path exists at least once in a graph. +While existential subqueries are more powerful and capable of performing anything achievable with path pattern expressions, path pattern expressions are more concise. -[[filter-on-patterns]] -=== Filter on patterns +Path pattern expressions have the following restrictions (use cases that require extended functionality should consider using xref::subqueries/existential.adoc[existential subqueries] instead): + +* Path pattern expressions may only use a subset of xref::patterns/reference.adoc#graph-patterns[graph pattern] semantics. + +* A path pattern expression must be a xref::patterns/reference.adoc#path-patterns[path pattern] of length greater than zero. +In other words, it must contain at least one xref::patterns/reference.adoc#relationship-patterns[relationship] or xref::patterns/reference.adoc#variable-length-relationships[variable-length relationship]. -Patterns are expressions in Cypher, expressions that return a list of paths. -List expressions are also predicates; an empty list represents `false`, and a non-empty list represents `true`. +* Path pattern expressions may not declare new variables. +They can only reference existing variables. -Patterns are not only expressions, they are also predicates. -The only limitation to a pattern is that it must be able to express it in a single path. -Unlike `MATCH`, it is not possible to use commas between multiple paths. -To combine multiple patterns using `WHERE`, instead use `AND`. -Note that new variables cannot be introduced. -Although it might look very similar to the `MATCH` patterns, the `WHERE` clause is used to eliminate matched paths. -`MATCH (a)-[*]->(b)` is very different from `WHERE (a)-[*]->(b)`. -The first will produce a path for every path it can find between `a` and `b`, whereas the latter will eliminate any matched paths where `a` and `b` do not have a directed relationship chain between them. +* Path pattern expressions may only be used in positions where a xref:queries/expressions.adoc#boolean[boolean expression] is expected. +The following sections will demonstrate how to use path pattern expressions in a `WHERE` clause. +[[filter-on-patterns]] +=== Filter on patterns .Query [source, cypher] @@ -505,7 +507,7 @@ The first will produce a path for every path it can find between `a` and `b`, wh MATCH (timothy:Person {name: 'Timothy'}), (other:Person) -WHERE other.name IN ['Andy', 'Peter'] AND (other)-->(timothy) +WHERE (other)-->(timothy) RETURN other.name, other.age ---- @@ -529,10 +531,10 @@ The `NOT` operator can be used to exclude a pattern: [source, cypher] ---- MATCH - (person:Person), - (peter:Person {name: 'Peter'}) -WHERE NOT (person)-->(peter) -RETURN person.name, person.age + (peter:Person {name: 'Peter'}), + (other:Person) +WHERE NOT (other)-->(peter) +RETURN other.name, other.age ---- The `name` and `age` values for nodes that do not have an outgoing relationship to `Peter` are returned: @@ -540,7 +542,7 @@ The `name` and `age` values for nodes that do not have an outgoing relationship .Result [role="queryresult",options="header,footer",cols="2*() -WHERE n.name='Andy' AND type(r) =~ 'K.*' -RETURN type(r), r.since ----- - -This returns all relationships having a type whose name starts with 'K': - -.Result -[role="queryresult",options="header,footer",cols="2*