Skip to content

Commit 0513358

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

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
@@ -90,11 +90,14 @@ RETURN movie.title
9090
=== MATCH using node label expressions
9191

9292
.Node pattern using the `OR` (`|`) label expression
93+
// tag::clauses_match_label_expression_or[]
9394
[source, cypher]
9495
----
9596
MATCH (n:Movie|Person)
9697
RETURN n.name AS name, n.title AS title
9798
----
99+
// end::clauses_match_label_expression_or[]
100+
98101

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

113116
.Node pattern using negation (`!`) label expression
117+
// tag::clauses_match_label_expression_negation[]
114118
[source, cypher]
115119
----
116120
MATCH (n:!Movie)
117121
RETURN labels(n) AS label, count(n) AS labelCount
118122
----
123+
// end::clauses_match_label_expression_negation[]
124+
119125

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

262268
.Relationship pattern including either `ACTED_IN` or `DIRECTED` relationship types
269+
// tag::clauses_match_type_expression_or[]
263270
[source, cypher]
264271
----
265272
MATCH (:Movie {title: 'Wall Street'})<-[:ACTED_IN|DIRECTED]-(person:Person)
266273
RETURN person.name AS person
267274
----
275+
// end::clauses_match_type_expression_or[]
276+
268277

269278
.Result
270279
[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
@@ -69,12 +69,14 @@ The paths matched by a xref:patterns/fixed-length-patterns.adoc#path-patterns[pa
6969
For example, the following example uses `SHORTEST 1` to return the length of the shortest path between `Worcester Shrub Hill` and `Bromsgrove`:
7070

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

7981
[TIP]
8082
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].
@@ -157,12 +159,14 @@ If there had been only four possible paths between the two Stations, then only t
157159
To return all paths that are tied for shortest length, use the keywords `ALL SHORTEST`:
158160

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

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

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

194200
.Result
195201
[role="queryresult",options="header,footer",cols="2m,m"]
@@ -240,12 +246,14 @@ It returns the same as `SHORTEST 1`, but by using the `ANY` keyword the intent o
240246
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:
241247

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

250258
.Result
251259
[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)