The 30 second answer
First I'd find out where the time goes, using the slow log and the Profile API, which breaks a query down into its parts. Then I'd look for query shapes that can't use the index well: a leading wildcard, a script, a term lookup on a numeric ID, a range with now that can't be cached, or a search across dozens of fields. Most fixes move work from search time to index time.
A quick picture of what "indexed" means. For a text or keyword field, Elasticsearch, through the Lucene library underneath it, builds an inverted index. That's a sorted list of every distinct term, and for each term, the list of documents that contain it. Finding the documents for one exact term is a fast lookup in that sorted list. Anything that can't be turned into "look up a few known terms" is slower, because Elasticsearch has to walk through many terms or run code on many documents.
Step 1: measure, don't guess.
- Search slow log. Set a threshold per index, for example
index.search.slowlog.threshold.query.warn, and Elasticsearch logs every query slower than that. - Profile API. Add
"profile": trueto the search request, and the response shows how long each part of the query took on each shard. A wildcard or script clause usually stands out at once.
Step 2: look for the usual causes. Each is a separate question on this page:
- A wildcard that starts with
*, such as*phone. Elasticsearch has to scan the term list instead of jumping into it. - A script in a query, sort or aggregation. It's code that runs once for every matching document.
- A numeric ID searched with exact term lookups, even though numeric fields are optimized for ranges.
- A date range using
now, which changes every millisecond, so the result can't be cached. - Prefix or autocomplete queries without any prefix structure built at index time.
- A
multi_matchacross many fields, which repeats the search once per field.
Also check whether exact yes/no conditions (brand, size, in stock) sit in filter context. Filters skip scoring and can be cached. That's covered in the search fundamentals track.
Step 3: the general fix. Almost every fix is the same idea: do the expensive work once at index time, not on every search. That can mean extra sub-fields, precomputed values, a different field type or n-grams. The cost is slower indexing and more disk, which you should say out loud in the interview.
GET products/_search
{
"profile": true,
"query": { "wildcard": { "name.keyword": { "value": "*phone" } } }
}Alternate interview scenarios
"Search p99 latency doubled after a release, and nothing changed in the cluster."
Compare the query bodies before and after the release. A new wildcard, script or extra multi_match field is the usual culprit. Run the Profile API on one slow example and compare it with the old query.
"Only some queries are slow."
Search the slow log for what they have in common. They're often user-typed patterns with a leading *, or very long date ranges with now.
Sources (3)
- Elasticsearch docs: Tune for search speedelastic.co
- Elasticsearch docs: Profile APIelastic.co
- Elasticsearch docs: Slow logelastic.co