Custom aggregators
Some of the most common aggregators in the SQL world are count
, sum
, min
, or max
. They are used in conjunction with the GROUP BY
instruction. Here is an example of aggregator usage in Cypher:
MATCH (p:Person)-[IS_NAMED]-(ln:LastName) RETURN ln.lastName, count(p)
Since version 3.2, Neo4j allows you to create your own aggregators, which is fairly simple. To illustrate, we will see how to code a random count: a count aggregator that returns a random value. The Java code is as follows:
package learningneo4j; import org.neo4j.procedure.*; public class RandomCount { @UserAggregationFunction("learningneo4j.randomCount") @Description( "learningneo4j.randomCount - mostly returns a wrong value " ) public RandomAggregator randomAggregator() { return new RandomAggregator(); } public static class RandomAggregator { private long count; @UserAggregationUpdate public void repeated(@Name("string") Object obj ){ // parameter given as example...