Index refresh interval in Elasticsearch - Time & Space Complexity
When working with Elasticsearch, the index refresh interval controls how often new data becomes visible for searching.
We want to understand how changing this interval affects the time it takes for data to appear in search results.
Analyze the time complexity of the index refresh operation with this setting.
PUT /my-index/_settings
{
"index" : {
"refresh_interval" : "1s"
}
}
POST /my-index/_doc
{
"message": "Hello Elasticsearch"
}
GET /my-index/_search
{
"query": { "match_all": {} }
}
This code sets the refresh interval to 1 second, indexes a document, then searches the index.
Look at what repeats during index refresh:
- Primary operation: Periodic refresh of the index to make new data searchable.
- How many times: Once every refresh interval (e.g., every 1 second).
The refresh operation runs regularly regardless of how many documents are added.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 documents | Refresh runs every interval, processing 10 new docs |
| 100 documents | Refresh runs every interval, processing 100 new docs |
| 1000 documents | Refresh runs every interval, processing 1000 new docs |
Pattern observation: The cost per refresh grows roughly linearly with the number of new documents since each must be made searchable.
Time Complexity: O(n)
This means the time to refresh grows in direct proportion to the number of new documents waiting to be made searchable.
[X] Wrong: "The refresh interval time directly controls how fast each refresh operation runs."
[OK] Correct: The interval controls how often refresh happens, not how long each refresh takes. The refresh time depends on how many new documents need processing.
Understanding how refresh intervals affect indexing and search latency helps you balance speed and resource use in real Elasticsearch setups.
What if we changed the refresh interval from 1 second to 30 seconds? How would the time complexity of each refresh operation change?