Skip to content

Commit

Permalink
all but non-linear
Browse files Browse the repository at this point in the history
  • Loading branch information
JPryce-Aklundh committed Nov 26, 2024
1 parent e5de1b6 commit 57b1d4d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 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`:

// tag::patterns_fixed_length_patterns_node_pattern[]
[source, role=noheader]
----
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
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_shortest_k[]
[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_shortest_k[]

.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 57b1d4d

Please sign in to comment.