Visualization types in Elasticsearch - Time & Space Complexity
When we create visualizations in Elasticsearch, we want to know how the time to build them changes as we add more data.
We ask: How does the work grow when the data or buckets increase?
Analyze the time complexity of the following aggregation for a visualization.
GET /sales/_search
{
"size": 0,
"aggs": {
"by_category": {
"terms": { "field": "category.keyword", "size": 10 },
"aggs": {
"avg_price": { "avg": { "field": "price" } }
}
}
}
}
This query groups sales by category and calculates the average price per category for visualization.
Look at what repeats as data grows.
- Primary operation: Elasticsearch scans all documents to group them by category.
- How many times: Once per document, then it processes each category bucket (up to 10 here).
As the number of documents grows, Elasticsearch must check each one once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 document checks + 10 buckets |
| 100 | About 100 document checks + 10 buckets |
| 1000 | About 1000 document checks + 10 buckets |
Pattern observation: The work grows roughly in direct proportion to the number of documents.
Time Complexity: O(n)
This means the time to build the visualization grows linearly with the number of documents.
[X] Wrong: "The number of buckets (like categories) makes the time grow a lot more than the documents."
[OK] Correct: The main work is scanning documents once; buckets add some work but usually less than scanning all data.
Understanding how Elasticsearch handles data for visualizations helps you explain performance clearly and shows you know how data size affects response time.
"What if we increased the bucket size from 10 to 1000? How would the time complexity change?"