Index refresh interval in Elasticsearch - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
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?
Practice
index.refresh_interval setting control in Elasticsearch?Solution
Step 1: Understand the role of
This setting controls the frequency at which Elasticsearch refreshes the index to make newly indexed data searchable.index.refresh_intervalStep 2: Compare with other options
The other options relate to the number of shards, size limits, and replicas, which are unrelated to refresh timing.Final Answer:
How often the index makes new data searchable -> Option CQuick Check:
Refresh interval = data searchable frequency [OK]
- Confusing refresh interval with shard count
- Thinking it controls index size
- Mixing it up with replica settings
Solution
Step 1: Identify correct JSON structure for refresh interval
The refresh interval must be a string with time units, inside theindexobject.Step 2: Validate options
{ "index": { "refresh_interval": "5s" } } uses "5s" (5 seconds) correctly as a string with units. Plain numbers like 5 without units are invalid. { "index": { "refresh_interval": "5000" } } uses "5000" without units, which is incorrect. Missing theindexobject is also invalid.Final Answer:
{ "index": { "refresh_interval": "5s" } } -> Option DQuick Check:
Refresh interval needs string with units [OK]
- Using number without time unit
- Placing refresh_interval outside index object
- Using milliseconds as plain number string
{ "index": { "refresh_interval": "30s" } }What happens if you index a document and immediately search for it within 10 seconds?
Solution
Step 1: Understand refresh interval effect on search
With a 30-second refresh interval, Elasticsearch refreshes the index every 30 seconds to make new data searchable.Step 2: Analyze timing of search after indexing
If you search within 10 seconds, the index has not refreshed yet, so the new document is not visible.Final Answer:
The document will not be found until after 30 seconds -> Option AQuick Check:
Refresh interval delays new data visibility [OK]
- Assuming instant search visibility
- Confusing refresh interval with indexing speed
- Thinking document is never searchable
index.refresh_interval to -1 to disable automatic refresh during heavy indexing. After indexing, you want to make all data searchable immediately. What is the correct way to do this?Solution
Step 1: Understand disabling refresh with -1
Setting refresh_interval to -1 disables automatic refresh, so new data is not searchable until manually refreshed.Step 2: Identify how to make data searchable immediately
Using the_refreshAPI triggers an immediate refresh, making all indexed data searchable without restarting or recreating.Final Answer:
Run a manual_refreshAPI call on the index -> Option BQuick Check:
Manual refresh needed when auto refresh disabled [OK]
- Setting refresh_interval to 0 instead of calling _refresh
- Restarting cluster unnecessarily
- Deleting index instead of refreshing
Solution
Step 1: Understand trade-off between refresh interval and indexing speed
Frequent refreshes slow indexing but improve data freshness; less frequent refreshes speed indexing but delay visibility.Step 2: Choose best practice for heavy write load
Setting a higher refresh interval during bulk indexing reduces refresh overhead, then manually refreshing after bulk load balances speed and search freshness.Final Answer:
Setindex.refresh_intervalto a higher value during indexing, then manually refresh after bulk load -> Option AQuick Check:
Adjust refresh interval for bulk, then manual refresh [OK]
- Setting refresh_interval to 0 causes slow indexing
- Disabling refresh permanently loses search freshness
- Deleting index wastes resources
