Graql Aggregate Queries
Edit me

An aggregate query applies an operation onto a match, to return information about the results (e.g. a count). To follow along, or experiment further, with the examples given below, please load the basic-genealogy.gql file, which can be found in the examples directory of the Grakn installation zip, or on Github.

./grakn server start 
./graql console -f <relative-path-to-Grakn>/examples/basic-genealogy.gql

Aggregate Functions

Ask

Whether the given match has any results.

match divorce sub relationship; aggregate ask;
match marriage sub relationship; aggregate ask;
qb.match(label("divorce").sub("relationship")).aggregate(ask()).execute();
qb.match(label("marriage").sub("relationship")).aggregate(ask()).execute();

Count

Count the number of results of the match or aggregate result.

match $x isa person; aggregate count;
qb.match(var("x").isa("person")).aggregate(count());

Sum

Sum the given attribute variable.

match $x isa person, has age $a; aggregate sum $a;
qb.match(
    var("x").isa("person").has("age", var("a"))
).aggregate(sum("a"));

Max

Find the maximum of the given attribute variable.

match $x isa person, has age $a; aggregate max $a;
qb.match(
    var("x").isa("person").has("age", var("a"))
).aggregate(max("a"));

Min

Find the minimum of the given attribute variable.

match $x isa person, has firstname $n; aggregate min $n;
qb.match(
    var("x").isa("person").has("firstname", var("n"))
).aggregate(min("n"));

Mean

Find the mean (average) of the given attribute variable.

match $x isa person, has age $a; aggregate mean $a;
qb.match(
    var("x").isa("person").has("age", var("a"))
).aggregate(mean("a"));

Median

Find the median of the given attribute variable.

match $x isa person, has age $a; aggregate median $a;

qb.match(
    var("x").isa("person").has("age", var("a"))
).aggregate(median("a"));

Group

Group the results by the given variable.

The group aggregate can optionally accept a second argument which is another aggregate operation, e.g. count.

match $x isa person; $y isa person; (parent: $x, child: $y) isa parentship; aggregate group $x;
qb.match(
    var("x").isa("person"),
    var("y").isa("person"),
    var().rel("parent", "x").rel("child", "y").isa("parentship")
).aggregate(group("x"));

Select

Select and name multiple aggregates.

match $x isa person, has age $a, has gender $g; aggregate (min $a as minAge, max $g as maxGender);
qb.match(
    var("x").isa("person").has("age", var("a")).has("gender", var("g")),
).aggregate(select(min("a").as("minAge"), max("g").as("maxGender")));

When to Use aggregate and When to Use compute

Aggregate queries are computationally light and run single-threaded on a single machine, but are more flexible than the equivalent compute query.

For example, you can use an aggregate query to filter results by attribute. The following aggregate query, allows you to match the number of people of a particular name:

match $x has identifier contains "Elizabeth"; aggregate count;

Compute queries are computationally intensive and run in parallel on a cluster (so are good for big data).

compute count in person;

Can be used to calculate the number of people in the knowledge base very fast, but you can’t filter the results to determine the number of people with a certain name.

Tags: graql