Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Aggregations can be requested when querying items. They are used to calculate summarized views and statistics about the data in search results. This allows you to build data drill-downs along the structured part of items as well as visualizations over time or other data dimensions.

Table of Contents

Table of Contents
outlinetrue
excludeTable of Contents

Specification

Request format

The request format is an url encoded JSON object of the following format:

...

Examples

This section contains examples for the various calculations that can be requested with aggregations.

Group by field

Simple one dimensional value faceting over a single field. This will return a result count for each value in the given field.

Code Block
languagejs
"language": {
    "fields": "language"
}

This can also be shortened, as the label can also serve as the field:

Code Block
languagejs
"language": {}

Set number of results

 

By default 10 results are returned for each field. This can be changed by setting the size parameter. For example to retrieve the top 3 languages from the result set:

Code Block
languagejs
"language": {
    "size": 3
}

Select multiple fields

To return the result counts for all values in multiple fields, simply list the fields:

Code Block
languagejs
"multi": {
    "fields": ["provider", "language"]
}

Group by multiple fields

It's possible to add nested dimensions in an aggregation. The example below groups the result first by client and then within each client add up the revenue column: 


Code Block
languagejs
{
    "outer_agg_name": {
        "fields": "client",
        "aggregation": {
            "fields": "revenue",
            "method": "sum"
        }
    }
}

Group by date

The histogram aggregation method is recommended for dates, because it groups values by hour, day, week, etc.

To count the results for \item's creation dates, use the following aggregation:

Code Block
languagejs
"$item_created_at": {
    "method": "histogram"
}

This can again be nested, to for example aggregate the languages of items over time:

Code Block
languagejs
"$item_created_at": {
    "method": "histogram",
    "aggregation": {
       "fields": ["language"],
    }
} 


Significant Terms Aggregation

This can be used to do the Significant Terms aggregation. Please note that this requires sending a query also to make this effective.

Code Block
languagejs
"aggregation_name": {
    "method": "significant_terms",
	"fields": ["provider", "language"]
}

Sample Python script

Below is a sample python script that can be used to authenticate against a Squirro Cluster and request these aggregations.

Code Block
languagejs
"from squirro_client import SquirroClient

s = SquirroClient(None, None, cluster="https://unstable.squirro.net")

s.authenticate(refresh_token="FILL_IN_TOKEN")

aggregations = {
    "client_agg": {
        "fields": "client",
        "aggregation": {"fields": "money", "method": "sum"},
    }
}

res = s.query(project_id="PROJECT_ID", aggregations=aggregations)
print(res)