Elasticsearch Best Practices
Core Principles
- Design indices and mappings based on query patterns
- Optimize for search performance with proper analysis and indexing
- Use appropriate shard sizing and cluster configuration
- Implement proper security and access control
- Monitor cluster health and optimize queries
Index Design
Mapping Best Practices
- Define explicit mappings instead of relying on dynamic mapping
- Use appropriate data types for each field
- Disable indexing for fields you do not search on
- Use keyword type for exact matches, text for full-text search
Field Types
keyword: Exact values, filtering, aggregations, sortingtext: Full-text search with analysisdate: Date/time values with format specificationnumeric types: long, integer, short, byte, double, float, scaled_floatboolean: True/false valuesgeo_point: Latitude/longitude pairsnested: Arrays of objects that need independent querying
Index Settings
Shard Sizing
Guidelines
- Target 20-40GB per shard
- Aim for ~20 shards per GB of heap
- Avoid oversharding (too many small shards)
- Consider time-based indices for time-series data
Index Lifecycle Management (ILM)
Query Optimization
Query Types
Match Query (Full-text search)
Term Query (Exact match)
Bool Query (Combining queries)
Query Best Practices
- Use
filtercontext for non-scoring queries (cacheable) - Use
mustonly when scoring is needed - Avoid wildcards at the beginning of terms
- Use
keywordfields for exact matches - Limit result size with
sizeparameter
Aggregations
Common Aggregation Patterns
Aggregation Best Practices
- Use
size: 0when you only need aggregations - Set appropriate
shard_sizefor terms aggregations - Use composite aggregations for pagination
- Consider using
aggsfilters to narrow scope
Indexing Best Practices
Bulk Indexing
Bulk API Guidelines
- Use bulk API for batch operations
- Optimal bulk size: 5-15MB per request
- Monitor for rejected requests (thread pool queue full)
- Disable refresh during bulk indexing for better performance
Document Updates
Analysis and Tokenization
Custom Analyzers
Test Analyzer
Search Features
Autocomplete/Suggestions
Highlighting
Performance Optimization
Query Caching
- Filter queries are cached automatically
- Use
filtercontext for frequently repeated conditions - Monitor cache hit rates
Search Performance
- Avoid deep pagination (use
search_afterinstead) - Limit
_sourcefields returned - Use
doc_valuesfor sorting and aggregations - Pre-sort index for common sort orders


