Skip to content

Commit

Permalink
Cheat sheet tags for patterns and label expressions (#1125)
Browse files Browse the repository at this point in the history
  • Loading branch information
JPryce-Aklundh authored Dec 3, 2024
1 parent 76cc351 commit 0513358
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
9 changes: 9 additions & 0 deletions modules/ROOT/pages/clauses/match.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,14 @@ RETURN movie.title
=== MATCH using node label expressions

.Node pattern using the `OR` (`|`) label expression
// tag::clauses_match_label_expression_or[]
[source, cypher]
----
MATCH (n:Movie|Person)
RETURN n.name AS name, n.title AS title
----
// end::clauses_match_label_expression_or[]


.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand All @@ -111,11 +114,14 @@ RETURN n.name AS name, n.title AS title
|===

.Node pattern using negation (`!`) label expression
// tag::clauses_match_label_expression_negation[]
[source, cypher]
----
MATCH (n:!Movie)
RETURN labels(n) AS label, count(n) AS labelCount
----
// end::clauses_match_label_expression_negation[]


[NOTE]
The above query uses the xref:functions/list.adoc#functions-labels[`labels()`] and xref:functions/aggregating.adoc#functions-count[`count()`] functions.
Expand Down Expand Up @@ -260,11 +266,14 @@ RETURN actor.name AS actor
It is possible to match a pattern containing one of several relationship types using the `OR` symbol, `|`.

.Relationship pattern including either `ACTED_IN` or `DIRECTED` relationship types
// tag::clauses_match_type_expression_or[]
[source, cypher]
----
MATCH (:Movie {title: 'Wall Street'})<-[:ACTED_IN|DIRECTED]-(person:Person)
RETURN person.name AS person
----
// end::clauses_match_type_expression_or[]


.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand Down
8 changes: 7 additions & 1 deletion modules/ROOT/pages/patterns/fixed-length-patterns.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ MATCH (n { mode: 'Rail' })
More general predicates can be expressed with a `WHERE` clause.
The following matches nodes whose name property starts with `Preston`:

[source, role=noheader]
// tag::patterns_fixed_length_patterns_node_pattern[]
[source, cypher]
----
MATCH (n:Station WHERE n.name STARTS WITH 'Preston')
RETURN n
----
// end::patterns_fixed_length_patterns_node_pattern[]


See the xref:patterns/reference.adoc#node-patterns[node patterns] reference section for more details.

Expand Down Expand Up @@ -204,11 +208,13 @@ In order to return the name of each `Stop` that calls at a `Station`, declare a
The results will then have a row containing the departs value of each `Stop` for each match shown above:

.Query
// tag::patterns_fixed_length_patterns_path_pattern[]
[source, cypher]
----
MATCH (s:Stop)-[:CALLS_AT]->(:Station {name: 'Denmark Hill'})
RETURN s.departs AS departureTime
----
// end::patterns_fixed_length_patterns_path_pattern[]

.Result
[role="queryresult",options="header,footer",cols="1*<m"]
Expand Down
6 changes: 6 additions & 0 deletions modules/ROOT/pages/patterns/non-linear-patterns.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ image::patterns_equijoins_motif2.svg[width="700",role="middle"]
Putting this path pattern with an equijoin in a query, the times of the outbound and return journeys can be returned:

.Query
// tag::patterns_non_linear_patterns_equijoin[]
[source, cypher]
----
MATCH (n:Station {name: 'London Euston'})<-[:CALLS_AT]-(s1:Stop)
Expand All @@ -70,6 +71,8 @@ MATCH (n:Station {name: 'London Euston'})<-[:CALLS_AT]-(s1:Stop)
RETURN s1.departs+'-'+s2.departs AS outbound,
s3.departs+'-'+s4.departs AS `return`
----
// end::patterns_non_linear_patterns_equijoin[]


.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand Down Expand Up @@ -181,6 +184,7 @@ The other node variables are for the `WHERE` clause or for returning data.
Putting this together, the resulting query returns the earliest arrival time achieved by switching to an express service:

.Query
// tag::patterns_non_linear_patterns_graph_pattern[]
[source, cypher]
----
MATCH (:Station {name: 'Starbeck'})<-[:CALLS_AT]-
Expand All @@ -197,6 +201,8 @@ RETURN a.departs AS departs,
y.arrives AS arrives
ORDER BY y.arrives LIMIT 1
----
// end::patterns_non_linear_patterns_graph_pattern[]


.Result
[role="queryresult",options="header,footer",cols="4*<m"]
Expand Down
8 changes: 8 additions & 0 deletions modules/ROOT/pages/patterns/shortest-paths.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ The paths matched by a xref:patterns/fixed-length-patterns.adoc#path-patterns[pa
For example, the following example uses `SHORTEST 1` to return the length of the shortest path between `Worcester Shrub Hill` and `Bromsgrove`:

.Query
// tag::patterns_shortest_paths_shortest_k[]
[source, cypher]
----
MATCH p = SHORTEST 1 (wos:Station)-[:LINK]-+(bmv:Station)
WHERE wos.name = "Worcester Shrub Hill" AND bmv.name = "Bromsgrove"
RETURN length(p) AS result
----
// end::patterns_shortest_paths_shortest_k[]

[TIP]
Note that this and the following examples in this section use a quantified relationship `-[:LINK]-+`, which is composed of a relationship pattern `-[:LINK]-` and a postfix quantifier `+`. The relationship pattern is only concerned with following relationships with type `LINK`, and will otherwise traverse any node along the way. There is no arrowhead `<` or `>` on the relationship pattern, allowing the pattern to match relationships going in either direction. This represents the fact that trains can go in both directions along the `LINK` relationships between Stations. The `+` quantifier means that one or more relationships should be matched. For more information, see xref:patterns/reference.adoc#quantified-relationships[Syntax and semantics - quantified relationships].
Expand Down Expand Up @@ -157,12 +159,14 @@ If there had been only four possible paths between the two Stations, then only t
To return all paths that are tied for shortest length, use the keywords `ALL SHORTEST`:

.Query
// tag::patterns_shortest_paths_all_shortest[]
[source,cypher]
----
MATCH p = ALL SHORTEST (wos:Station)-[:LINK]-+(bmv:Station)
WHERE wos.name = "Worcester Shrub Hill" AND bmv.name = "Bromsgrove"
RETURN [n in nodes(p) | n.name] AS stops
----
// end::patterns_shortest_paths_all_shortest[]

.Result
[role="queryresult",options="header,footer",cols="m"]
Expand All @@ -184,12 +188,14 @@ To return all paths that are tied for first, second, and so on up to the kth sho
For example, the following returns the first and second shortest length paths between `Worcester Shrub Hill` and `Bromsgrove`:

.Query
// tag::patterns_shortest_paths_shortest_k_groups[]
[source,cypher]
----
MATCH p = SHORTEST 2 GROUPS (wos:Station)-[:LINK]-+(bmv:Station)
WHERE wos.name = "Worcester Shrub Hill" AND bmv.name = "Bromsgrove"
RETURN [n in nodes(p) | n.name] AS stops, length(p) AS pathLength
----
// end::patterns_shortest_paths_shortest_k_groups[]

.Result
[role="queryresult",options="header,footer",cols="2m,m"]
Expand Down Expand Up @@ -240,12 +246,14 @@ It returns the same as `SHORTEST 1`, but by using the `ANY` keyword the intent o
For example, the following query shows that there exists a route from `Pershore` to `Bromsgrove` where the distance between each pair of stations is less than 10 miles:

.Query
// tag::patterns_shortest_paths_any[]
[source,cypher]
----
MATCH path = ANY
(:Station {name: 'Pershore'})-[l:LINK WHERE l.distance < 10]-+(b:Station {name: 'Bromsgrove'})
RETURN [r IN relationships(path) | r.distance] AS distances
----
// end::patterns_shortest_paths_any[]

.Result
[role="queryresult",options="header,footer",cols="m"]
Expand Down
9 changes: 9 additions & 0 deletions modules/ROOT/pages/patterns/variable-length-patterns.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,16 @@ Translating the union of fixed-length path patterns into a quantified path patte
The following query adds a `RETURN` clause that yields the departure and arrival times of the two services:

.Query
// tag::patterns_variable_length_patterns_qpp[]
[source, cypher]
----
MATCH (:Station { name: 'Denmark Hill' })<-[:CALLS_AT]-(d:Stop)
((:Stop)-[:NEXT]->(:Stop)){1,3}
(a:Stop)-[:CALLS_AT]->(:Station { name: 'Clapham Junction' })
RETURN d.departs AS departureTime, a.arrives AS arrivalTime
----
// end::patterns_variable_length_patterns_qpp[]


.Result
[role="queryresult",options="header,footer",cols="2*<m"]
Expand Down Expand Up @@ -227,6 +230,7 @@ A quantified relationship is a relationship pattern with a postfix quantifier.
Below is the previous query rewritten with a quantified relationship:

.Query
// tag::patterns_variable_length_patterns_quantified_relationships[]
[source, cypher]
----
MATCH (d:Station { name: 'Denmark Hill' })<-[:CALLS_AT]-
Expand All @@ -235,6 +239,8 @@ MATCH (d:Station { name: 'Denmark Hill' })<-[:CALLS_AT]-
WHERE m.arrives < time('17:18')
RETURN n.departs AS departureTime
----
// end::patterns_variable_length_patterns_quantified_relationships[]


The scope of the quantifier `{1,10}` is the relationship pattern `-[:NEXT]\->` and not the node patterns abutting it.
More generally, where a path pattern contained in a quantified path pattern has the following form:
Expand Down Expand Up @@ -523,6 +529,7 @@ In this example, an inline predicate can be added that takes advantage of the ge
To compose the predicate, the xref:functions/spatial.adoc#functions-distance[point.distance()] function is used to compare the distance between the left-hand `Station` (`a`) and the right-hand `Station` (`b`) for each node-pair along the path to the destination `North Dulwich`:

.Query
// tag::patterns_variable_length_patterns_predicates_in_qpp[]
[source,cypher]
----
MATCH (bfr:Station {name: "London Blackfriars"}),
Expand All @@ -534,6 +541,8 @@ MATCH p = (bfr)
RETURN reduce(acc = 0, r in relationships(p) | round(acc + r.distance, 2))
AS distance
----
// end::patterns_variable_length_patterns_predicates_in_qpp[]


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

0 comments on commit 0513358

Please sign in to comment.