Prompt
How do I troubleshoot slow log searches in Elasticsearch?
Latest observation
Here’s a practical way to troubleshoot slow log searches in Elasticsearch, from quickest checks to deeper root-cause analysis.
1) Confirm where the slowness is
First determine whether the delay is in:
- Elasticsearch query execution
- Network / client-side latency
- Dashboard/app rendering
- A specific index or time range
Useful checks:
- Run the same search directly in Elasticsearch with
curlor Kibana Dev Tools. - Compare:
tookin the response- client-observed latency
- Try a very small time range and a broad one.
If took is low but the app is slow, the issue may not be Elasticsearch.
2) Use the Search Profiler / _profile
If a search is slow, inspect what Elasticsearch is spending time on.
Search Profiler
In Kibana:
- Dev Tools / Search Profiler
- Paste the query
- Look for:
- expensive
match_all+ heavy sort/aggregation - high-cost scripts
- large
termsaggregations - expensive wildcard/regex queries
- expensive
_profile
Example:
GET my-index/_search
{
"profile": true,
"query": {
"bool": {
"must": [
{ "match": { "message": "error" } }
]
}
}
}
Look for:
- Which query clause is slow
- Which shard is slow
- Whether time is spent in scoring, filtering, or aggregations
3) Check whether the query is forcing full scans
Common causes of slow searches:
Expensive query patterns
- Leading wildcard:
*error
- Regex queries
- Very broad time range
query_stringwith many fields- Sorting on fields not optimized for sorting
- Large
frompagination values - Heavy aggregations on high-cardinality fields
Better alternatives
- Use
prefixor analyzed search instead of leading wildcards - Use filters (
bool.filter) for non-scoring clauses - Use
search_afterinstead of deep pagination - Limit fields returned with
_sourcefiltering - Avoid huge
termsaggregations unless needed
4) Inspect shard health and distribution
Slow searches often happen when shards are too many, too large, or unbalanced.
Check:
GET _cat/shards?v
GET _cat/indices?v
GET _cluster/health
Watch for:
- Hot shards on one node
- Too many shards
- Very large shards
- Unassigned or relocating shards
General guidance:
- Too many small shards adds overhead
- Too few huge shards can make each search expensive
- Uneven shard allocation can overload one node
5) Look at node and cluster load
Search latency can rise when the cluster is under resource pressure.
Check:
GET _cat/nodes?v&h=name,cpu,heap.percent,ram.percent,load_1m,node.role
GET _nodes/stats
Look for:
- High CPU
- High heap usage / frequent GC
- Disk I/O saturation
- Thread pool rejections
- High search queue depth
Useful areas in node stats:
indices.searchthread_pool.searchjvm.gcfs.io_stats
If search threads are queued or rejected, the node is overloaded.
6) Check for cache misses and segment issues
Search performance depends on Lucene segment behavior and cache usage.
Possible issues:
- Frequent indexing causing many small segments
- Cache churn
- Cold filesystem cache
- Large merges happening in the background
Things to inspect:
- Are you searching recently indexed data?
- Is the query repeatedly executed or always unique?
- Are segments being constantly merged?
Warm queries may be fast while first-time queries are slower due to cache cold starts.
7) Examine mappings and field types
Poor mappings can make searches slower.
Common mapping problems
- Using
textwhen exact matching is needed - Sorting/aggregating on analyzed fields
- Missing
keywordsubfields - Fields with extremely high cardinality used in aggregations
- Dynamic mappings creating unexpected field types
Check mappings:
GET my-index/_mapping
Good practices:
- Use
keywordfor sorting, aggregations, and exact match - Use
textfor full-text search - Prefer doc values for aggregations/sorting
8) Review the query itself
If possible, simplify it.
Questions to ask:
- Can any
mustclause be moved tofilter? - Can you narrow the time range?
- Do you really need all aggregations?
- Are you sorting by
_scoreor by a field? - Are you returning too many hits?
- Are scripts used in runtime fields or script queries?
Try benchmarking a minimal version of the query and add clauses back one by one.
9) Use slow logs
Elasticsearch slow logs help identify slow queries and shards.
Configure search slow logs per index, for example:
PUT my-index/_settings
{
"index.search.slowlog.threshold.query.warn": "5s",
"index.search.slowlog.threshold.fetch.warn": "1s"
}
Then inspect logs for:
- Query phase time
- Fetch phase time
- The exact query and shard involved
This is especially helpful when the slowness is intermittent.
10) Check the fetch phase separately
A search can be slow because of:
- large
_source - highlighting
- many stored fields
- script fields
- rescore / post-processing
If query time is fine but fetch is slow:
- Reduce
_source - Avoid highlighting on large text fields unless needed
- Return fewer fields
- Check document size
11) Look for pagination problems
Deep pagination is a common cause of slow log searches.
Bad pattern:
"from": 50000,
"size": 50
Better:
search_after- Point-in-time (
PIT) for consistent pagination - Time-based filtering
12) Benchmark with realistic data and concurrency
A query that is fine alone may be slow under load.
Test:
- Query latency under production-like concurrency
- Different time ranges
- Different shard counts
- Different node sizes
You may discover:
- thread pool saturation
- cache eviction under load
- disk contention from concurrent ingestion
Quick checklist
If log searches are slow, check in this order:
- Query text and time range
_profile/ Search Profiler- Search slow logs
- Shard count and distribution
- CPU, heap, GC, disk I/O
- Mapping and field types
- Aggregations, sorting, pagination
- Fetch phase / source size
- Concurrent load and indexing activity
If you want a fast next step
Paste:
- your query
- index mapping
- shard count
- one slow-log entry or
_profileoutput
and I can help identify the bottleneck.
Brands