The aggregate endpoint
Everything the Insights app shows (tables, charts, maps, number tiles, dashboards) is produced by one call:
/api/v2/source/{sourceId}/aggregateinsights-serviceconst { 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.
| Page | Covers |
|---|---|
| Filters | Which documents to include |
| Sorting | Order of documents and groups |
| Grouping | Buckets: by value, by date, by region, by your own rules |
| Metrics | Count, sum, average, formulas, … per group |
| Modes and paging | aggregation versus composite, response rows, paging |
| Recipes | Bar chart, time series, pie, number, table, map, ratio, boolean |
| From a chart to code | Turn a saved chart or table into a request, with a ready-made helper |
| Limits and gotchas | Sizes, 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
}
| Property | Type | Description |
|---|---|---|
size | integer | Documents to return, or buckets per page in composite mode. The Insights app uses 10000 for charts and 100 for tables |
from | integer | Offset for plain document lists (not with pageKey). from + size may not exceed 10,000 |
fields | string[] | Fields to return per document (_source). Also required if you use includeFieldTypes |
filter | array | Array of Elasticsearch query clauses. All must match. See Filters |
sort | array | Array of Elasticsearch sort clauses. See Sorting |
aggs | array | Grouping and metric stages. See Grouping and Metrics |
aggMode | 'aggregation' or 'composite' | Required whenever aggs is set. See Modes and paging |
pageKey | array or object | The nextPageKey of the previous response |
includeFieldTypes | boolean | Include the field definitions of fields in the response |
raw | boolean | Query 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 send | You get |
|---|---|
No aggs | A 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": { }
}
| Property | Description |
|---|---|
rows | Documents, or aggregation rows, see Modes and paging |
nextPageKey | Cursor for the next page. Pass it as pageKey |
totalDocCount | Documents in the whole source (not filtered) |
filteredDocCount | Documents matching filter. Only present when a filter was sent. Counts above 10,000 are reported as { value: 10000, relation: 'gte' } |
fieldTypes | Only 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) field | Number, date, boolean field |
|---|---|---|
Group by, exact match (term, terms), sort | field.keyword | field |
| Contains / does not contain | field.ngram | n/a |
| Starts with / ends with | field.wildcard | n/a |
range, exists, geo queries | field | field |
Metrics value_count, cardinality, collect_terms | field.keyword | field |
fields (returned values) | field | field |
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.