A compute query executes a Pregel algorithm to determine information about the knowledge base in parallel. Called within the Graql shell, the general syntax is:
compute [algorithm] (arguments) (in subgraph); (modifiers;)
algorithmcan be any of the available statistics or graph algorithms.
argumentsare different depending on the specific algorithm.
subgraphis a comma separated list of types to be visited by the Pregel algorithm.
modifierssome algorithms can have their output modified.
The specific algorithms fall into two main categories and more information is given in the sections below.
The simplest algorithm
count can be executed using this query:
The subgraph syntax is provided to control the types that a chosen algorithm operates upon.
By default, the compute algorithms include instances of every type in the calculation.
in keyword followed by a comma separated list of types will restrict the calculations to instances of those types only.
compute count in person;
will return just the number of instances of the concept type person. Subgraphs can be applied to all compute queries and therefore are different to arguments and modifiers.
Available Statistics Algorithms
The following algorithms are available to perform simple statistics compuations, and we aim to add to these as demand dictates. Please get in touch on our discussion page to request any features that are of particular interest to you. A summary of the statistics algorithms is given in the table below.
||Count the number of instances.|
||Compute the maximum value of an attribute.|
||Compute the mean value of an attribute.|
||Compute the median value of an attribute.|
||Compute the minimum value of an attribute.|
||Compute the standard deviation of an attribute.|
||Compute the sum of an attribute.|
For further information see the individual sections below.
The default behaviour of count is to return a single value that gives the number of instances present in the graph. It is possible to also count subsets of the instances in the graph using the subgraph syntax, as described above.
compute count in person;
Computes the mean value of a given attribute. This algorithm requires the subgraph syntax to be used. For example,
compute mean of age in person;
would compute the mean value of
age across all instances of the type
It is also possible to provide a set of resources.
compute mean of attribute-a, attribute-b in person;
which would compute the mean of the union of the instances of the two resources.
Computes the median value of a given attribute. This algorithm requires the subgraph syntax to be used. For example,
compute median of age in person;
would compute the median of the value persisted in instances of the attribute
Computes the minimum value of a given attribute, similar to mean.
compute min of age in person;
Computes the maximum value of a given attribute, similar to mean.
compute max of age in person;
Computes the standard deviation of a given attribute, similar to mean.
compute std of age in person;
Computes the sum of a given attribute, similar to mean.
compute sum of age in person;
Available Graph Algorithms
The following algorithms all compute values based on the structure of the graph. A summary of the graph algorithms is given in the table below.
||Find connected clusters of instances.|
||Find the number of related instances to each instance in the graph.|
||Find the shortest path between two instances.|
When to Use
aggregate and When to Use
Aggregate queries are computationally light and run single-threaded on a single machine, but are more flexible than the equivalent compute queries described above.
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 name 'Bob'; aggregate count;
Compute queries are computationally intensive and run in parallel on a cluster (so are good for big data).
compute count of person;
Compute queries can be used to calculate the number of people in the graph very fast, but you can’t filter the results to determine the number of people with a certain name.