Skip to content

Commit 440f67a

Browse files
clarify config setting and possible consequence of post-filter
1 parent 3920527 commit 440f67a

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

modules/ROOT/pages/patterns/shortest-paths.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ RETURN p
883883

884884
If the predicate requires inspecting the entire path after it has been matched (such as checking whether the path length exceeds a certain value), the planner cannot exclude paths early.
885885
In such cases, a slower, exhaustive search-algorithm is used.
886+
Exhaustive searches may be very time consuming in certain cases, such as when there is no shortest path between two nodes (to disallow exhaustive searches, set link:{neo4j-docs-base-uri}/operations-manual/current/configuration/configuration-settings#config_dbms.cypher.forbid_exhaustive_shortestpath[`dbms.cypher.forbid_exhaustive_shortestpath`] to `true`).
886887

887888
.Exhaustive search-algorithm
888889
[source, cypher]
@@ -895,12 +896,13 @@ RETURN p
895896

896897
For queries that would otherwise trigger an exhaustive search, a practical workaround is to first bind the matched path and then filter it using a xref:clauses/filter.adoc[`FILTER`] clause (`FILTER` is a separate clause that performs a post-match filter, unlike xref:clauses/where.adoc[`WHERE`] which adds constraints to the pattern matched by the xref:clauses/match.adoc[`MATCH`] clause).
897898
This allows the planner to use a fast search-algorithm while finding the shortest path, and only afterwards apply the filter.
899+
Note that, because the filter is applied after the fast algorithm runs, it may eliminate all candidate paths and return no results.
898900

899901
.Query rewritten to use fast search-algorithm
900902
[source, cypher]
901903
----
902904
MATCH (start:N {level: 1}), (end:N {level: 5})
903905
MATCH p = shortestPath((start)-[*]-(end))
904-
FILTER length(p) > 1
906+
FILTER length(p) > 3
905907
RETURN p
906908
----

0 commit comments

Comments
 (0)