You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: modules/ROOT/pages/functions/predicate.adoc
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -110,7 +110,7 @@ RETURN all(i in emptyList WHERE true) as allTrue, all(i in emptyList WHERE false
110
110
|===
111
111
| *Syntax* 3+| `allReduce(accumulator = initial, stepVariable IN list \| reductionFunction, predicate)`
112
112
| *Description* 3+| Returns true if, during the stepwise evaluation of a value across the elements in a given `LIST<ANY>`, the accumulated result satisfies a specified predicate at every step.
113
-
Where that list is a xref:patterns/variable-length-patterns.adoc#group-variables[group variable] defined in a xref:patterns/variable-length-patterns.adoc#quantified-path-patterns[quantified path pattern], it allows for the early pruning of paths that do not satisfy the predicate.
113
+
Where that list is a xref:patterns/variable-length-patterns.adoc#group-variables[group variable] defined in a xref:patterns/variable-length-patterns.adoc#quantified-path-patterns[quantified path pattern] (not within a xref:patterns/shortest-paths.adoc[shortest path pattern]), it allows for the early pruning of paths that do not satisfy the predicate.
Copy file name to clipboardExpand all lines: modules/ROOT/pages/patterns/variable-length-patterns.adoc
+25Lines changed: 25 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -380,6 +380,10 @@ For example, all relationships in the path must be of type `EMPLOYED_BY`.
380
380
* Nodes or relationships must have properties satisfying some condition.
381
381
For example, all relationships must have the property `distance > 10`.
382
382
383
+
* For every iteration of the QPP, an aggregated value over the constructed path so far should satisfy a predicate.
384
+
For example, the sum of the property `distance` of the relationships in the path must be less than 50 for every step in the construction of the path.
385
+
See xref::functions/predicate.adoc#functions-allreduce[allReduce] for more information about this predicate.
386
+
383
387
To demonstrate the utility of predicates in quantified path patterns, this section considers an example of finding the shortest path by physical distance and compares that to the results yielded by using the xref:patterns/shortest-paths.adoc[`SHORTEST`] keyword.
384
388
The graph in this example continues with `Station` nodes, but adds both a geospatial `location` property to the `Stations`, as well as `LINK` relationships with a `distance` property representing the distance between pairs of `Stations`:
385
389
@@ -558,6 +562,27 @@ This query avoids having to find all possible paths and then imposing a `LIMIT 1
558
562
It also shows that there is only one path to solving the query (a number that remains constant even if the data from the rest of the UK railway network was included).
559
563
Using inline predicates or making quantified path patterns more specific where possible can thus greatly improve query performance.
560
564
565
+
An alternative is to use the information that we want to find paths with a total distance less than `6.05`.
566
+
From the `ALL SHORTEST` query, we know that there exists a path with a total distance of `6.04`.
567
+
Once the path exceeds this limit, we can prune it and continue searching other paths.
568
+
The xref::functions/predicate.adoc#functions-allreduce[allReduce] predicate function expresses this as follows:
569
+
570
+
.Query
571
+
[source,cypher]
572
+
----
573
+
MATCH (bfr:Station {name: "London Blackfriars"}),
574
+
(ndl:Station {name: "North Dulwich"})
575
+
MATCH p = (bfr) ((a)-[l:LINK]-(b:Station))+ (ndl)
576
+
WHERE allReduce(
577
+
pathLength = 0,
578
+
link IN l | pathLength + link.distance,
579
+
pathLength < 6.05
580
+
)
581
+
RETURN reduce(acc = 0, r in relationships(p) | round(acc + r.distance, 2))
0 commit comments