Skip to main content

The aggregate endpoint

Everything the Insights app shows (tables, charts, maps, number tiles, dashboards) is produced by one call:

POST/api/v2/source/{sourceId}/aggregateinsights-service
const { status, result } = await sdk.asAdmin().Insights().Source('Orders').aggregate(request)

This reference explains every part of that request, exactly as the Insights app builds it, so you can produce the same results from your own code with the SDK or plain REST.

PageCovers
FiltersWhich documents to include
SortingOrder of documents and groups
GroupingBuckets: by value, by date, by region, by your own rules
MetricsCount, sum, average, formulas, … per group
Modes and pagingaggregation versus composite, response rows, paging
RecipesBar chart, time series, pie, number, table, map, ratio, boolean
From a chart to codeTurn a saved chart or table into a request, with a ready-made helper
Limits and gotchasSizes, time zones, empty values, errors

The request​

{
"size": 100,
"from": 0,
"fields": ["orderId", "total"],
"filter": [ /* Elasticsearch clauses */ ],
"sort": [ /* Elasticsearch sort clauses */ ],
"aggs": [ /* Scenid aggregation stages */ ],
"aggMode": "aggregation",
"includeFieldTypes": false,
"raw": false
}
PropertyTypeDescription
sizeintegerDocuments to return, or buckets per page in composite mode. The Insights app uses 10000 for charts and 100 for tables
fromintegerOffset for plain document lists (not with pageKey). from + size may not exceed 10,000
fieldsstring[]Fields to return per document (_source). Also required if you use includeFieldTypes
filterarrayArray of Elasticsearch query clauses. All must match. See Filters
sortarrayArray of Elasticsearch sort clauses. See Sorting
aggsarrayGrouping and metric stages. See Grouping and Metrics
aggMode'aggregation' or 'composite'Required whenever aggs is set. See Modes and paging
pageKeyarray or objectThe nextPageKey of the previous response
includeFieldTypesbooleanInclude the field definitions of fields in the response
rawbooleanQuery the raw index (all values as text)

filter and sort must be arrays. The SDK throws sdk/aggregate-filter-must-be-array / sdk/aggregate-sort-must-be-array otherwise, and the REST API answers with an error.

Three ways to call it​

You sendYou get
No aggsA document list: rows are documents. Page with pageKey, or from up to 10,000
aggs and aggMode: 'aggregation'Aggregated rows in one response. One row per group, with your metrics. No paging
aggs and aggMode: 'composite'Documents per group, with cursor paging. Used for tables that group rows

If you send aggs without aggMode, or an empty aggs list, the API fails with a server error. Always set both.

The response​

{
"rows": [ ],
"aggs": [ ],
"aggMode": "aggregation",
"nextPageKey": [ ],
"filteredDocCount": { "value": 41, "relation": "eq" },
"totalDocCount": { "value": 60, "relation": "eq" },
"fieldTypes": { }
}
PropertyDescription
rowsDocuments, or aggregation rows, see Modes and paging
nextPageKeyCursor for the next page. Pass it as pageKey
totalDocCountDocuments in the whole source (not filtered)
filteredDocCountDocuments matching filter. Only present when a filter was sent. Counts above 10,000 are reported as { value: 10000, relation: 'gte' }
fieldTypesOnly if includeFieldTypes is set

Field names: the rule that trips up everyone​

Fields are named the way they were defined on the source. Which name you use in a request depends on what you are doing with it, because text fields are indexed in several forms:

You want to…Text (string) fieldNumber, date, boolean field
Group by, exact match (term, terms), sortfield.keywordfield
Contains / does not containfield.ngramn/a
Starts with / ends withfield.wildcardn/a
range, exists, geo queriesfieldfield
Metrics value_count, cardinality, collect_termsfield.keywordfield
fields (returned values)fieldfield

The safest way to know is to read the field definitions of the source. Each carries the names to use:

const { result: source } = await sdk.asAdmin().Insights().Source('Orders').get()

console.log(source.fieldTypes.category)
// { field: 'category', format: 'string', keywordField: 'category.keyword', … }

Customers (customers-entities) fields are prefixed: data.city, data.city.keyword. See Customers as a source.

Where to start​

New to this? Read Recipes first, then come back for the details. If you want to reproduce what a chart in the app does, go to From a chart to code.