Skip to content

Commit db22543

Browse files
Update remaining queries using shortestPath() (#1022)
1 parent 57b98d5 commit db22543

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

modules/ROOT/pages/queries/basic.adoc

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -795,32 +795,43 @@ LIMIT 5
795795
|===
796796

797797
[NOTE]
798-
====
799798
The quantifier used in the above two examples was introduced with the release of xref::patterns/variable-length-patterns.adoc#quantified-path-patterns[quantified path patterns] in Neo4j 5.9.
800799
Before that, the only way in Cypher to match paths of a variable length was with a variable-length relationship.
801-
This syntax is still available in Cypher.
802-
Read more about it xref::patterns/reference.adoc#variable-length-relationships[here].
803-
====
800+
This syntax is still available in Cypher, but it is not xref:appendix/gql-conformance/index.adoc[GQL conformant].
801+
For more information, see xref::patterns/reference.adoc#variable-length-relationships[Patterns -> Syntax and semantics -> Variable length relationships].
804802

805-
To find the shortest possible path between two nodes, use the `shortestPath` algorithm.
806-
For example, this query matches the shorest path in the graph between the two nodes `Tom Hanks` and `Keanu Reeves`:
803+
The xref:patterns/shortest-paths.adoc[`SHORTEST`] keyword can be used to find a variation of the shortest paths between two nodes.
804+
In this example, `ALL SHORTEST` paths between the two nodes `Keanu Reeves` and `Tom Cruise` are found.
805+
The xref:functions/aggregating.adoc#functions-count[`count()`] function calculates the number of these shortest paths while the xref:functions/scalar.adoc#functions-length[`length()`] function calculates the length of each path in terms of traversed relationships.
807806

808807
.Query
809808
[source, cypher]
810809
----
811-
MATCH p=shortestPath(
812-
(:Person {name:"Keanu Reeves"})-[*]-(:Person {name:"Tom Hanks"})
813-
)
814-
RETURN p
810+
MATCH p = ALL SHORTEST (:Person {name:"Keanu Reeves"})--+(:Person {name:"Tom Cruise"})
811+
RETURN count(p) AS pathCount, length(p) AS pathLength
815812
----
816813

817-
This is the returned path:
814+
The results show that 2 different paths are tied for the shortest length.
818815

819-
image::introduction_example2.svg[width="500",role="middle]
816+
.Result
817+
[role="queryresult",options="header,footer",cols="2*<m"]
818+
|===
819+
| pathCount
820+
| pathLength
820821

821-
It shows that `Keanu Reeves` `ACTED_IN` the `Movie` `The Replacements`, which was `REVIEWED` by the movie critic `Jessica Thompson`, who also `REVIEWED` the `Movie` `The Da Vinci Code` which `Tom Hanks` `ACTED_IN`.
822+
| 2
823+
| 4
824+
825+
2+d|Rows: 1
826+
827+
|===
828+
829+
[NOTE]
830+
The `SHORTEST` keyword was introduced in Neo4j 5.21, and functionally replaces and extendes the `shortestPath()` and `allShortestPaths()` functions.
831+
Both functions can still be used, but they are not xref:appendix/gql-conformance/index.adoc[GQL conformant].
832+
For more information, see xref:patterns/reference.adoc#shortest-functions[Patterns -> Syntax and semantics -> The `shortestPath()` and `allShortestPaths()` functions].
822833

823-
More information can be found in the section on xref::patterns/index.adoc[Patterns].
834+
For more information about graph pattern matching, see xref::patterns/index.adoc[Patterns].
824835

825836
[[find-recommendations]]
826837
== Finding recommendations

modules/ROOT/pages/queries/concepts.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ This example uses a xref:patterns/variable-length-patterns.adoc#quantified-relat
7171
The xref:syntax/operators.adoc#syntax-using-the-distinct-operator[DISTINCT] operator is used to ensure that the `RETURN` clause only returns unique nodes.
7272

7373
Paths can also be assigned variables.
74-
For example, the below query binds a whole path pattern, which matches the xref:patterns/reference.adoc#shortest-functions[shortest path] from `Anna` to another `Person` node in the graph up to `10` hops away with the `nationality` property set to `Canadian`.
74+
For example, the below query binds a whole path pattern, which matches the xref:patterns/shortest-paths.adoc[`SHORTEST`] path from `Anna` to another `Person` node in the graph with a `nationality` property set to `Canadian`.
7575
In this case, the `RETURN` clause returns the full path between the two nodes.
7676

7777
[source, cypher]
7878
----
79-
MATCH p=shortestPath((:Person {name: 'Anna'})-[:KNOWS*1..10]-(:Person {nationality: 'Canadian'}))
79+
MATCH p = SHORTEST 1 (:Person {name: 'Anna'})-[:KNOWS]-+(:Person {nationality: 'Canadian'})
8080
RETURN p
8181
----
8282

83-
For more information about graph patterns, see the section on xref:patterns/index.adoc[].
83+
For more information about graph pattern matching, see xref:patterns/index.adoc[].

0 commit comments

Comments
 (0)