0
0
ElasticsearchDebug / FixBeginner · 4 min read

How to Handle Slow Queries in Elasticsearch Effectively

Slow queries in Elasticsearch often happen due to inefficient queries, large data scans, or missing indexes. To handle them, use profiling to find bottlenecks, optimize queries by filtering early, and add proper mappings and indices. Also, limit the size of results and avoid expensive operations like wildcard searches on large datasets.
🔍

Why This Happens

Slow queries in Elasticsearch usually happen because the query scans too much data or uses inefficient search patterns. For example, searching with wildcard or regex on large fields, or missing filters that reduce the data early, causes Elasticsearch to work harder and slower.

Here is an example of a slow query that uses a wildcard on a large text field without filters:

json
{
  "query": {
    "wildcard": {
      "message": "*error*"
    }
  }
}
Output
Takes several seconds or more to return results, high CPU usage on Elasticsearch nodes.
🔧

The Fix

To fix slow queries, avoid expensive wildcard searches on large fields. Instead, use keyword fields or add filters to reduce the data early. Also, use the profile API to see which parts of the query are slow and optimize them.

Here is a fixed query that uses a match_phrase on a keyword field and a filter to narrow results:

json
{
  "query": {
    "bool": {
      "must": {
        "match_phrase": {
          "message.keyword": "error"
        }
      },
      "filter": {
        "range": {
          "timestamp": {
            "gte": "now-1d/d"
          }
        }
      }
    }
  }
}
Output
Returns results quickly with lower CPU usage by filtering data and avoiding wildcard scans.
🛡️

Prevention

To prevent slow queries in the future, follow these best practices:

  • Use proper mappings with keyword fields for exact matches.
  • Apply filters early to reduce data scanned.
  • Avoid wildcard and regex queries on large text fields.
  • Use the profile API regularly to monitor query performance.
  • Limit the size of returned results with size parameter.
  • Keep Elasticsearch cluster healthy and nodes balanced.
⚠️

Related Errors

Other common issues related to slow queries include:

  • Timeouts: Queries taking too long may time out; increase timeout or optimize query.
  • Memory pressure: Heavy queries can cause high memory use; optimize queries and increase heap if needed.
  • Shard imbalance: Uneven data distribution slows queries; rebalance shards.

Key Takeaways

Use the Elasticsearch profile API to identify slow parts of queries.
Avoid wildcard and regex queries on large text fields to improve speed.
Apply filters early to reduce the amount of data Elasticsearch scans.
Use keyword fields for exact matches instead of text fields.
Limit result size and keep your cluster balanced for better performance.