Recipes
Ready-to-use requests for what the Insights app draws. All examples use the test data sources Orders and Sensor readings, and the SDK. The body is the same over REST.
const orders = sdk.asAdmin().Insights().Source('Orders')
const sensors = sdk.asAdmin().Insights().Source('Sensor readings')
Fields used: category, product, country, status (text), total, quantity (numbers), orderedAt (date), newCustomer (boolean), and in the sensor data site, temperature (number), alarm (boolean), location (geo-point).
Bar chart: revenue per category
const { result } = await orders.aggregate({
size: 10000,
aggMode: 'aggregation',
aggs: [{
id: 'category',
field: 'category.keyword',
aggregation: 'terms',
options: { size: 10 },
aggs: [
{ id: 'revenue', field: 'total', aggregation: 'sum' },
{ id: 'orders', field: 'orderId.keyword', aggregation: 'value_count' }
]
}]
})
const series = result.rows.map(row => row.aggregation.fields)
// [{ doc_count: 24, category: 'Coffee', revenue: 1043.9, orders: 24 }, …]
Time series: revenue per month
await orders.aggregate({
size: 10000,
aggMode: 'aggregation',
aggs: [{
id: 'month',
field: 'orderedAt',
aggregation: 'datetime-interval',
options: {
calendarInterval: 'month',
includeMissingIntervals: true,
customBoundsMethod: 'datemath',
customBounds: {
min: { anchor: 'now', op: 'minus', n: 12, unit: 'M' },
max: { anchor: 'now', op: 'minus', n: 0, unit: 'M' }
}
},
aggs: [{ id: 'revenue', field: 'total', aggregation: 'sum' }]
}]
})
Rows: fields.month is epoch milliseconds (UTC). Convert with new Date(fields.month). Empty months are included because of includeMissingIntervals.
Pie: orders per status
await orders.aggregate({
size: 10000,
aggMode: 'aggregation',
aggs: [{ id: 'status', field: 'status.keyword', aggregation: 'terms', options: { size: 10 } }]
})
fields.doc_count is the value of each slice. No metric is needed.
Number tile: total revenue of paid orders
const { result } = await orders.aggregate({
size: 10000,
aggMode: 'aggregation',
filter: [{ term: { 'status.keyword': 'paid' } }],
aggs: [{
id: 'all',
aggregation: 'match_all',
aggs: [{ id: 'total', field: 'total', aggregation: 'sum' }]
}]
})
const revenue = result.rows[0].aggregation.fields.total
const orderCount = result.rows[0].aggregation.path[0].doc_count
Two levels: revenue per category and country
const metrics = [{ id: 'revenue', field: 'total', aggregation: 'sum' }]
const { result } = await orders.aggregate({
size: 10000,
aggMode: 'aggregation',
aggs: [
{ id: 'category', field: 'category.keyword', aggregation: 'terms', options: { size: 10 }, aggs: metrics },
{ id: 'country', field: 'country.keyword', aggregation: 'terms', options: { size: 10 }, aggs: metrics }
]
})
const leaves = result.rows
.filter(row => row.aggregation.depth === 2)
.map(row => row.aggregation.fields)
// [{ category: 'Coffee', country: 'DE', revenue: 512.3, doc_count: 12 }, …]
Top 5 categories by revenue
The API cannot sort groups by a metric. Ask for all groups and sort in code:
const { result } = await orders.aggregate({
size: 10000,
aggMode: 'aggregation',
aggs: [{
id: 'category', field: 'category.keyword', aggregation: 'terms', options: { size: 1000 },
aggs: [{ id: 'revenue', field: 'total', aggregation: 'sum' }]
}]
})
const top5 = result.rows
.map(row => row.aggregation.fields)
.sort((a, b) => b.revenue - a.revenue)
.slice(0, 5)
Table: documents under groups, paged
const request = {
size: 100,
aggMode: 'composite',
fields: ['category', 'product', 'total'],
sort: [{ 'category.keyword': 'asc' }],
filter: [{ bool: { must: [{ range: { orderedAt: { gte: '2026-08-01T00:00:00Z' } } }], must_not: [] } }],
aggs: [
{ id: 'category', field: 'category.keyword', aggregation: 'terms', options: { includeMissing: true } },
{ id: 'docs', aggregation: 'top_hits', options: { size: 10 } }
]
}
const first = await orders.aggregate(request)
const next = await orders.aggregate({ ...request, pageKey: first.result.nextPageKey })
Map: points and regions
Points, one per document:
await sensors.aggregate({
size: 10000,
aggMode: 'composite',
fields: ['location', 'deviceId'],
filter: [{ exists: { field: 'location' } }]
})
The composite call above returns documents. For plain point lists you can omit aggs and aggMode altogether.
Values per region (choropleth):
await sensors.aggregate({
size: 10000,
aggMode: 'aggregation',
aggs: [{
id: 'region',
field: 'location',
aggregation: 'geo-region',
options: {
regions: [
{ id: '02', name: 'Hamburg', regionType: 'bundesland' },
{ id: '09', name: 'Bayern', regionType: 'bundesland' },
{ id: '05', name: 'Nordrhein-Westfalen', regionType: 'bundesland' }
],
sortOrder: { Hamburg: 0, Bayern: 1, 'Nordrhein-Westfalen': 2 }
},
aggs: [{ id: 'readings', field: 'deviceId.keyword', aggregation: 'value_count' }]
}]
})
Ratio: revenue per unit, with a guard
await orders.aggregate({
size: 10000,
aggMode: 'aggregation',
aggs: [{
id: 'category', field: 'category.keyword', aggregation: 'terms', options: { size: 10 },
aggs: [
{ id: 'revenue', field: 'total', aggregation: 'sum' },
{ id: 'units', field: 'quantity', aggregation: 'sum' },
{
id: 'perUnit',
aggregation: 'formular',
options: {
params: { rev: 'revenue', qty: 'units' },
formular: "params['qty'] == 0 ? 0 : params['rev'] / params['qty']"
}
}
]
}]
})
Boolean: new versus returning customers
await orders.aggregate({
size: 10000,
aggMode: 'aggregation',
aggs: [{
id: 'newCustomer',
field: 'newCustomer',
aggregation: 'boolean',
options: { missingAsFalse: true },
aggs: [{ id: 'revenue', field: 'total', aggregation: 'sum' }]
}]
})
Keys are 'true' and 'false' (strings).
Alarms per site with a filter
await sensors.aggregate({
size: 10000,
aggMode: 'aggregation',
filter: [{ term: { alarm: true } }],
aggs: [{ id: 'site', field: 'site.keyword', aggregation: 'terms', options: { size: 20 } }]
})
Your own buckets: order size bands
await orders.aggregate({
size: 10000,
aggMode: 'aggregation',
aggs: [{
id: 'band',
aggregation: 'filters',
options: {
filters: [
{ small: { range: { total: { lt: 50 } } } },
{ medium: { range: { total: { gte: 50, lt: 200 } } } },
{ large: { range: { total: { gte: 200 } } } }
],
sortOrder: { small: 0, medium: 1, large: 2 }
},
aggs: [{ id: 'orders', field: 'orderId.keyword', aggregation: 'value_count' }]
}]
})
The same request over REST
- curl
- Python
- R
curl -s https://insights-service.scenid.cloud/api/v2/source/$SOURCE_ID/aggregate \
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
-d '{
"size": 10000,
"aggMode": "aggregation",
"aggs": [{
"id": "category", "field": "category.keyword", "aggregation": "terms", "options": { "size": 10 },
"aggs": [{ "id": "revenue", "field": "total", "aggregation": "sum" }]
}]
}'
import requests
response = requests.post(
f"https://insights-service.scenid.cloud/api/v2/source/{source_id}/aggregate",
headers={"Authorization": f"Bearer {token}"},
json={
"size": 10000,
"aggMode": "aggregation",
"aggs": [{
"id": "category", "field": "category.keyword", "aggregation": "terms", "options": {"size": 10},
"aggs": [{"id": "revenue", "field": "total", "aggregation": "sum"}],
}],
},
)
rows = [row["aggregation"]["fields"] for row in response.json()["rows"]]
library(httr2)
response <- request(paste0("https://insights-service.scenid.cloud/api/v2/source/", source_id, "/aggregate")) |>
req_auth_bearer_token(token) |>
req_body_json(list(
size = 10000,
aggMode = "aggregation",
aggs = list(list(
id = "category", field = "category.keyword", aggregation = "terms", options = list(size = 10),
aggs = list(list(id = "revenue", field = "total", aggregation = "sum"))
))
)) |>
req_perform()
rows <- lapply(resp_body_json(response)$rows, function(row) row$aggregation$fields)