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

Update remaining queries using shortestPath() #1022

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
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
39 changes: 25 additions & 14 deletions modules/ROOT/pages/queries/basic.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -795,32 +795,43 @@ LIMIT 5
|===

[NOTE]
====
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.
Before that, the only way in Cypher to match paths of a variable length was with a variable-length relationship.
This syntax is still available in Cypher.
Read more about it xref::patterns/reference.adoc#variable-length-relationships[here].
====
This syntax is still available in Cypher, but it is not xref:appendix/gql-conformance/index.adoc[GQL conformant].
For more information, see xref::patterns/reference.adoc#variable-length-relationships[Patterns -> Syntax and semantics -> Variable length relationships].

To find the shortest possible path between two nodes, use the `shortestPath` algorithm.
For example, this query matches the shorest path in the graph between the two nodes `Tom Hanks` and `Keanu Reeves`:
The xref:patterns/shortest-paths.adoc[`SHORTEST`] keyword can be used to find a variation of the shortest paths between two nodes.
In this example, `ALL SHORTEST` paths between the two nodes `Keanu Reeves` and `Tom Cruise` are found.
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.

.Query
[source, cypher]
----
MATCH p=shortestPath(
(:Person {name:"Keanu Reeves"})-[*]-(:Person {name:"Tom Hanks"})
)
RETURN p
MATCH p = ALL SHORTEST (:Person {name:"Keanu Reeves"})--+(:Person {name:"Tom Cruise"})
RETURN count(p) AS pathCount, length(p) AS pathLength
----

This is the returned path:
The results show that 2 different paths are tied for the shortest length.

image::introduction_example2.svg[width="500",role="middle]
.Result
[role="queryresult",options="header,footer",cols="2*<m"]
|===
| pathCount
| pathLength

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`.
| 2
| 4

2+d|Rows: 1

|===

[NOTE]
The `SHORTEST` keyword was introduced in Neo4j 5.21, and functionally replaces and extendes the `shortestPath()` and `allShortestPaths()` functions.
Both functions can still be used, but they are not xref:appendix/gql-conformance/index.adoc[GQL conformant].
For more information, see xref:patterns/reference.adoc#shortest-functions[Patterns -> Syntax and semantics -> The `shortestPath()` and `allShortestPaths()` functions].

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

[[find-recommendations]]
== Finding recommendations
Expand Down
6 changes: 3 additions & 3 deletions modules/ROOT/pages/queries/concepts.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ This example uses a xref:patterns/variable-length-patterns.adoc#quantified-relat
The xref:syntax/operators.adoc#syntax-using-the-distinct-operator[DISTINCT] operator is used to ensure that the `RETURN` clause only returns unique nodes.

Paths can also be assigned variables.
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`.
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`.
In this case, the `RETURN` clause returns the full path between the two nodes.

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

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