Skip to content

Commit 8569bee

Browse files
Cheat sheet tags for patterns and label expressions (#1125)
Cheat Sheet PR: neo4j/docs-cheat-sheet#203
1 parent e56cde5 commit 8569bee

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

modules/ROOT/pages/clauses/match.adoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,14 @@ RETURN movie.title
9494
=== MATCH using node label expressions
9595

9696
.Node pattern using the `OR` (`|`) label expression
97+
// tag::clauses_match_label_expression_or[]
9798
[source, cypher]
9899
----
99100
MATCH (n:Movie|Person)
100101
RETURN n.name AS name, n.title AS title
101102
----
103+
// end::clauses_match_label_expression_or[]
104+
102105

103106
.Result
104107
[role="queryresult",options="header,footer",cols="2*<m"]
@@ -115,11 +118,14 @@ RETURN n.name AS name, n.title AS title
115118
|===
116119

117120
.Node pattern using negation (`!`) label expression
121+
// tag::clauses_match_label_expression_negation[]
118122
[source, cypher]
119123
----
120124
MATCH (n:!Movie)
121125
RETURN labels(n) AS label, count(n) AS labelCount
122126
----
127+
// end::clauses_match_label_expression_negation[]
128+
123129

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

270276
.Relationship pattern including either `ACTED_IN` or `DIRECTED` relationship types
277+
// tag::clauses_match_type_expression_or[]
271278
[source, cypher]
272279
----
273280
MATCH (:Movie {title: 'Wall Street'})<-[:ACTED_IN|DIRECTED]-(person:Person)
274281
RETURN person.name AS person
275282
----
283+
// end::clauses_match_type_expression_or[]
284+
276285

277286
.Result
278287
[role="queryresult",options="header,footer",cols="1*<m"]

modules/ROOT/pages/patterns/fixed-length-patterns.adoc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ MATCH (n { mode: 'Rail' })
5757
More general predicates can be expressed with a `WHERE` clause.
5858
The following matches nodes whose name property starts with `Preston`:
5959

60-
[source, role=noheader]
60+
// tag::patterns_fixed_length_patterns_node_pattern[]
61+
[source, cypher]
6162
----
6263
MATCH (n:Station WHERE n.name STARTS WITH 'Preston')
64+
RETURN n
6365
----
66+
// end::patterns_fixed_length_patterns_node_pattern[]
67+
6468

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

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

206210
.Query
211+
// tag::patterns_fixed_length_patterns_path_pattern[]
207212
[source, cypher]
208213
----
209214
MATCH (s:Stop)-[:CALLS_AT]->(:Station {name: 'Denmark Hill'})
210215
RETURN s.departs AS departureTime
211216
----
217+
// end::patterns_fixed_length_patterns_path_pattern[]
212218

213219
.Result
214220
[role="queryresult",options="header,footer",cols="1*<m"]

modules/ROOT/pages/patterns/non-linear-patterns.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ image::patterns_equijoins_motif2.svg[width="700",role="middle"]
6262
Putting this path pattern with an equijoin in a query, the times of the outbound and return journeys can be returned:
6363

6464
.Query
65+
// tag::patterns_non_linear_patterns_equijoin[]
6566
[source, cypher]
6667
----
6768
MATCH (n:Station {name: 'London Euston'})<-[:CALLS_AT]-(s1:Stop)
@@ -70,6 +71,8 @@ MATCH (n:Station {name: 'London Euston'})<-[:CALLS_AT]-(s1:Stop)
7071
RETURN s1.departs+'-'+s2.departs AS outbound,
7172
s3.departs+'-'+s4.departs AS `return`
7273
----
74+
// end::patterns_non_linear_patterns_equijoin[]
75+
7376

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

183186
.Query
187+
// tag::patterns_non_linear_patterns_graph_pattern[]
184188
[source, cypher]
185189
----
186190
MATCH (:Station {name: 'Starbeck'})<-[:CALLS_AT]-
@@ -197,6 +201,8 @@ RETURN a.departs AS departs,
197201
y.arrives AS arrives
198202
ORDER BY y.arrives LIMIT 1
199203
----
204+
// end::patterns_non_linear_patterns_graph_pattern[]
205+
200206

201207
.Result
202208
[role="queryresult",options="header,footer",cols="4*<m"]

modules/ROOT/pages/patterns/shortest-paths.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ The paths matched by a xref:patterns/fixed-length-patterns.adoc#path-patterns[pa
6868
For example, the following example uses `SHORTEST 1` to return the length of the shortest path between `Worcester Shrub Hill` and `Bromsgrove`:
6969

7070
.Query
71+
// tag::patterns_shortest_paths_shortest_k[]
7172
[source, cypher]
7273
----
7374
MATCH p = SHORTEST 1 (wos:Station)-[:LINK]-+(bmv:Station)
7475
WHERE wos.name = "Worcester Shrub Hill" AND bmv.name = "Bromsgrove"
7576
RETURN length(p) AS result
7677
----
78+
// end::patterns_shortest_paths_shortest_k[]
7779

7880
[NOTE]
7981
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 `+`.
@@ -160,12 +162,14 @@ If there had been only four possible paths between the two Stations, then only t
160162
To return all paths that are tied for shortest length, use the keywords `ALL SHORTEST`:
161163

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

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

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

197203
.Result
198204
[role="queryresult",options="header,footer",cols="2m,m"]
@@ -243,12 +249,14 @@ It returns the same as `SHORTEST 1`, but by using the `ANY` keyword the intent o
243249
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:
244250

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

253261
.Result
254262
[role="queryresult",options="header,footer",cols="m"]

modules/ROOT/pages/patterns/variable-length-patterns.adoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,16 @@ Translating the union of fixed-length path patterns into a quantified path patte
172172
The following query adds a `RETURN` clause that yields the departure and arrival times of the two services:
173173

174174
.Query
175+
// tag::patterns_variable_length_patterns_qpp[]
175176
[source, cypher]
176177
----
177178
MATCH (:Station { name: 'Denmark Hill' })<-[:CALLS_AT]-(d:Stop)
178179
((:Stop)-[:NEXT]->(:Stop)){1,3}
179180
(a:Stop)-[:CALLS_AT]->(:Station { name: 'Clapham Junction' })
180181
RETURN d.departs AS departureTime, a.arrives AS arrivalTime
181182
----
183+
// end::patterns_variable_length_patterns_qpp[]
184+
182185

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

229232
.Query
233+
// tag::patterns_variable_length_patterns_quantified_relationships[]
230234
[source, cypher]
231235
----
232236
MATCH (d:Station { name: 'Denmark Hill' })<-[:CALLS_AT]-
@@ -235,6 +239,8 @@ MATCH (d:Station { name: 'Denmark Hill' })<-[:CALLS_AT]-
235239
WHERE m.arrives < time('17:18')
236240
RETURN n.departs AS departureTime
237241
----
242+
// end::patterns_variable_length_patterns_quantified_relationships[]
243+
238244

239245
The scope of the quantifier `{1,10}` is the relationship pattern `-[:NEXT]\->` and not the node patterns abutting it.
240246
More generally, where a path pattern contained in a quantified path pattern has the following form:
@@ -523,6 +529,7 @@ In this example, an inline predicate can be added that takes advantage of the ge
523529
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`:
524530

525531
.Query
532+
// tag::patterns_variable_length_patterns_predicates_in_qpp[]
526533
[source,cypher]
527534
----
528535
MATCH (bfr:Station {name: "London Blackfriars"}),
@@ -534,6 +541,8 @@ MATCH p = (bfr)
534541
RETURN reduce(acc = 0, r in relationships(p) | round(acc + r.distance, 2))
535542
AS distance
536543
----
544+
// end::patterns_variable_length_patterns_predicates_in_qpp[]
545+
537546

538547
.Result
539548
[role="queryresult",options="header,footer",cols="m"]

0 commit comments

Comments
 (0)