Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cheat sheet tags for patterns and label expressions #1125

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Changes from all 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
9 changes: 9 additions & 0 deletions modules/ROOT/pages/clauses/match.adoc
Original file line number Diff line number Diff line change
@@ -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"]
@@ -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.
@@ -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"]
8 changes: 7 additions & 1 deletion modules/ROOT/pages/patterns/fixed-length-patterns.adoc
Original file line number Diff line number Diff line change
@@ -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.

@@ -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"]
6 changes: 6 additions & 0 deletions modules/ROOT/pages/patterns/non-linear-patterns.adoc
Original file line number Diff line number Diff line change
@@ -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)
@@ -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"]
@@ -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]-
@@ -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"]
8 changes: 8 additions & 0 deletions modules/ROOT/pages/patterns/shortest-paths.adoc
Original file line number Diff line number Diff line change
@@ -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].
@@ -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"]
@@ -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"]
@@ -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"]
9 changes: 9 additions & 0 deletions modules/ROOT/pages/patterns/variable-length-patterns.adoc
Original file line number Diff line number Diff line change
@@ -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"]
@@ -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]-
@@ -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:
@@ -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"}),
@@ -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"]
Loading