Skip to main content

Aggregations

Execute complex data grouping and aggregation operations.

POST /api/datasets/{id}/_search-aggregates

Execute a search with aggregations in visual format.

Authentication: Required

Path Parameters:

ParameterTypeDescription
idstringDataset ID or slug

Request Body:

FieldTypeDescription
queryobjectElasticsearch query DSL
aggregationsarrayArray of aggregation strings (function:field)
_sourcearrayFields to return
sortarraySort configuration
fromintegerOffset
sizeintegerResult count
reportstringReport ID for field selection
searchTypestringElasticsearch search type
aliasstringSnapshot alias

Example Request:

{
"query": {
"range": {
"order_date": {
"gte": "2024-01-01"
}
}
},
"aggregations": [
"sum:amount",
"avg:amount",
"count:order_id",
"min:amount",
"max:amount"
]
}

Response:

{
"aggregations": {
"sum_amount": {
"value": 487500.50
},
"avg_amount": {
"value": 3125.65
},
"count_order_id": {
"value": 156
},
"min_amount": {
"value": 50.00
},
"max_amount": {
"value": 25000.00
}
},
"hits": {
"total": { "value": 156 },
"hits": []
}
}

Supported Aggregation Functions:

  • sum:field - Sum of values
  • avg:field - Average of values
  • min:field - Minimum value
  • max:field - Maximum value
  • count:field - Count of records
  • cardinality:field - Unique value count

POST /api/datasets/{id}/group

Group data with hierarchical aggregation.

Authentication: Required

Path Parameters:

ParameterTypeDescription
idstringDataset ID or slug

Request Body:

FieldTypeDescription
groupsarrayArray of grouping fields
aggsobjectAggregation configuration
bodyobjectQuery body (filter criteria)
parentsarrayParent group values (for drill-down)
levelintegerCurrent grouping level
timezonestringTimezone for date grouping

Example Request (single-level grouping):

{
"groups": ["region"],
"aggs": {
"total_amount": {
"sum": { "field": "amount" }
},
"avg_amount": {
"avg": { "field": "amount" }
}
},
"body": {
"query": {
"range": {
"order_date": { "gte": "2024-01-01" }
}
}
}
}

Response:

{
"total": 4,
"buckets": [
{
"key": "West",
"doc_count": 89,
"total_amount": { "value": 178500.00 },
"avg_amount": { "value": 2005.62 }
},
{
"key": "East",
"doc_count": 67,
"total_amount": { "value": 165300.50 },
"avg_amount": { "value": 2467.17 }
}
]
}

Example Request (hierarchical grouping):

{
"groups": ["region", "category"],
"level": 0,
"parents": [],
"aggs": {
"revenue": {
"sum": { "field": "amount" }
}
}
}

Hierarchical Response:

{
"buckets": [
{
"key": "West",
"doc_count": 89,
"revenue": { "value": 178500.00 },
"children": [
{
"key": "Electronics",
"doc_count": 45,
"revenue": { "value": 95000.00 }
},
{
"key": "Furniture",
"doc_count": 44,
"revenue": { "value": 83500.00 }
}
]
}
]
}

POST /api/datasets/{id}/group-rows

Apply grouping filter to rows (drill-down into specific group).

Authentication: Required

Path Parameters:

ParameterTypeDescription
idstringDataset ID or slug

Request Body:

FieldTypeDescription
timezonestringTimezone for date fields
groupsarrayGrouping configuration
parentsarrayParent group values to filter by
bodyobjectBase query

Example Request:

{
"groups": ["region", "category"],
"parents": ["West", "Electronics"],
"body": {
"query": {
"range": {
"order_date": { "gte": "2024-01-01" }
}
}
}
}

Response:

Returns filtered rows matching the group criteria:

{
"total": 45,
"hits": [
{
"order_id": "ORD-001",
"region": "West",
"category": "Electronics",
"amount": 1500.00
},
{
"order_id": "ORD-023",
"region": "West",
"category": "Electronics",
"amount": 2300.00
}
]
}

Aggregation Examples

Sales Summary

{
"aggregations": [
"sum:amount",
"count:order_id",
"avg:amount",
"cardinality:customer_id"
]
}

Group by Region and Date

{
"groups": ["region"],
"aggs": {
"daily_sales": {
"date_histogram": {
"field": "order_date",
"calendar_interval": "day"
},
"aggs": {
"revenue": {
"sum": { "field": "amount" }
}
}
}
},
"timezone": "America/New_York"
}

Top Products by Category

{
"groups": ["category"],
"aggs": {
"top_products": {
"terms": {
"field": "product_name",
"size": 10,
"order": { "revenue": "desc" }
},
"aggs": {
"revenue": {
"sum": { "field": "amount" }
}
}
}
}
}

Best Practices

Performance

  • Index grouping fields - Especially for terms aggregations
  • Limit bucket size - Don't group by high-cardinality fields
  • Use filters - Reduce data before aggregating
  • Consider caching - Cache aggregation results when possible

Accuracy

  • Use appropriate functions - Sum vs. avg vs. count
  • Handle nulls - Filter out or account for null values
  • Check precision - Elasticsearch approximations for large datasets
  • Validate results - Compare with source data

Hierarchical Grouping

  • Limit levels - 2-3 levels maximum for performance
  • Progressive loading - Load children on demand
  • Parent context - Pass parent values for drill-down
  • Breadcrumbs - Track user navigation through hierarchy