How to Optimize Elasticsearch Performance: Tips and Examples
To optimize
Elasticsearch performance, focus on efficient indexing, proper shard and replica settings, and use filters instead of queries when possible. Also, monitor resource usage and tune refresh_interval and cache settings to reduce overhead and speed up searches.Syntax
Key settings and commands to optimize Elasticsearch include:
index.number_of_shards: Controls how many pieces your data is split into for parallel processing.index.number_of_replicas: Sets how many copies of your data exist for fault tolerance and read speed.refresh_interval: How often Elasticsearch refreshes the index to make new data searchable.query with filters: Filters are cached and faster than full queries.
json
PUT /my_index/_settings
{
"index": {
"number_of_shards": 3,
"number_of_replicas": 1,
"refresh_interval": "30s"
}
}Example
This example shows how to create an index with optimized shard and replica settings, and how to use a filter in a search query for better performance.
json
PUT /products
{
"settings": {
"index": {
"number_of_shards": 2,
"number_of_replicas": 1,
"refresh_interval": "60s"
}
},
"mappings": {
"properties": {
"name": { "type": "text" },
"category": { "type": "keyword" },
"price": { "type": "float" }
}
}
}
GET /products/_search
{
"query": {
"bool": {
"filter": [
{ "term": { "category": "electronics" } },
{ "range": { "price": { "lte": 1000 } } }
]
}
}
}Output
{
"hits": {
"total": { "value": 5, "relation": "eq" },
"hits": [
{ "_source": { "name": "Smartphone", "category": "electronics", "price": 999 } },
{ "_source": { "name": "Headphones", "category": "electronics", "price": 199 } }
]
}
}
Common Pitfalls
Common mistakes that hurt Elasticsearch performance include:
- Using too many shards for small datasets, which wastes resources.
- Setting
refresh_intervaltoo low, causing frequent costly refreshes. - Running full-text queries instead of filters when filtering data, which is slower.
- Not monitoring cluster health and ignoring slow queries.
json
GET /my_index/_settings
-- This can be optimized to --
PUT /my_index/_settings
{
"index": {
"number_of_shards": 3,
"refresh_interval": "30s"
}
}Quick Reference
Summary tips to optimize Elasticsearch performance:
- Use fewer shards for small to medium indexes.
- Set
refresh_intervalhigher during heavy indexing. - Use filters instead of queries for exact matches.
- Monitor and tune JVM heap size and garbage collection.
- Use
doc_valuesfor sorting and aggregations.
Key Takeaways
Set appropriate shard and replica counts based on data size and query load.
Use filters instead of queries for faster, cached searches.
Adjust refresh_interval to balance indexing speed and search freshness.
Monitor cluster health and resource usage regularly.
Avoid over-sharding and excessive refreshes to reduce overhead.