Prompt
How do I index logs so searches stay fast at high volume?
Latest observation
To keep log search fast at high volume, focus on making queries cheap and keeping the index small, ordered, and time-based.
Core principles
1) Index only what you actually search
Don’t blindly index every field.
Good candidates:
timestampservice/appenvironmenthostlevel(info,error, etc.)trace_id/request_id- key error codes or status fields
Usually don’t index:
- full log message text
- large payloads
- high-cardinality free-form fields unless needed
- fields you never filter on
If you use Elasticsearch/OpenSearch, mark many fields as doc_values-only or not indexed at all when appropriate.
2) Use time-based partitioning
Logs are almost always searched by time range, so split data by time:
- daily or hourly indices / partitions for very large volumes
- use retention/TTL policies
- drop old shards/partitions instead of deleting individual records
This reduces the amount of data each query touches.
3) Keep shard/partition sizes reasonable
Too many tiny shards hurt performance; too few huge shards hurt parallelism.
Rule of thumb:
- aim for shards/partitions that are not too small
- keep hot shards manageable for your storage and query load
- scale shard count with ingest/query volume
For Elasticsearch/OpenSearch, a common goal is to avoid lots of tiny shards and keep shard sizes in a healthy range for your workload.
4) Design around common filters
Put the most selective and most common filters into indexed fields:
serviceenvironmentclusterregionseveritytenant/customer_idif multi-tenant
If most searches are like:
- “errors from service X in prod last 15 min” then those fields should be indexed and optimized.
5) Use inverted-index-friendly field types
For exact matches and faceting:
- use
keyword/ exact string fields - use numeric types for codes/timestamps
- avoid analyzed text fields unless you need full-text search
For message search:
- consider a separate analyzed field for full-text
- keep structured fields separate from message text
6) Reduce cardinality where possible
High-cardinality fields can bloat indexes and slow aggregations:
- request IDs
- user IDs
- session IDs
- unique URLs with query params
Strategies:
- don’t index them unless needed
- index a normalized version
- bucket/normalize values
- store raw value in source, but not as searchable field
7) Normalize logs at ingest
Parse logs into structured fields at ingestion time:
- JSON logs are much faster to search than raw text
- extract
level,service,trace_id, etc. - strip noisy/unnecessary fields
This turns expensive text searches into fast field filters.
8) Use sampling or tiered storage for very high volume
If volume is extreme:
- keep full-fidelity logs for recent data
- downsample older logs
- move cold data to cheaper storage
- archive to object storage and query less often
This keeps hot search paths fast.
9) Precompute what you search often
If users often search for the same patterns:
- build summaries or rollups
- maintain error counts by service/minute
- store top exceptions separately
This reduces repeated expensive scans.
10) Tune query patterns
Even with a good index, bad queries are slow:
- always include a time range
- prefer exact match filters before full-text search
- avoid leading wildcards like
*error - avoid regex on large fields unless necessary
- paginate efficiently
Practical recipe
If you’re building or tuning a log system:
- Store logs in structured JSON
- Partition by time
- Index only search/filter fields
- Use exact-match fields for service/env/level/trace_id
- Keep message text searchable, but don’t over-index everything
- Apply retention and tiered storage
- Monitor shard/index size and query latency
- Review real query patterns and adjust mappings accordingly
Common mistakes
- indexing raw log text plus every JSON field
- not using time-based indices
- huge shards with no rollover
- querying across months of data without filters
- indexing high-cardinality fields unnecessarily
- relying on regex for normal searches
If you tell me what stack you’re using—Elasticsearch/OpenSearch, Splunk, Loki, ClickHouse, BigQuery, PostgreSQL, etc.—I can give specific indexing and schema recommendations.