System design Level 2 This level · 46 of 48
Medium search elasticsearch

Why do scripts make Elasticsearch queries slow, and what do you do instead?

The 30 second answer

A script is a small piece of code that Elasticsearch runs for every document it touches, so it can't use the index, a bit like wrapping a SQL column in a function. The docs say to avoid scripts in sorting, aggregations and script score where possible. Instead, I'd compute the value once at index time, in the application or an ingest pipeline, store it as a normal field, and sort or filter on that.

Could you say this in an interview?

What a script is. Elasticsearch lets you write small expressions in its scripting language, Painless, inside a query: for example "sort by price minus discount", or "score by rating times popularity". It's flexible, but the script runs once per matching document. With a million matches, that's a million executions. An inverted index or doc values lookup reads a ready-made value instead.

The official advice. In its search-speed guide, Elasticsearch says to avoid script-based sorting, scripts in aggregations, and the script_score query where possible. Its scripting guide shows the alternative with an example: instead of sorting by a script that adds two scores, compute the sum during ingest and sort on the stored field. It also notes the trade-off: search gets faster, but indexing often gets a bit slower.

How to move the work to index time.

  • In your application, compute the field before you send the document, for example final_price = price - discount.
  • In an ingest pipeline, a server-side step that changes documents before they're indexed, use a script processor or a set processor. The script then runs once per document write, not once per search.
  • Precompute buckets. If most searches aggregate prices into fixed ranges, store a keyword field such as price_range: "10-100" and run a fast terms aggregation on it. This exact example is in the Elastic guide.

Runtime fields are not a shortcut. A runtime field is computed at query time by a script. It's handy for exploring data without reindexing. But its speed depends on the script and on how many documents it runs over, so it's still per-document work. Promote a runtime field you use all the time to an indexed field.

PUT _ingest/pipeline/final-price
{ "processors": [ { "script": {
    "source": "ctx.final_price = ctx.price - (ctx.discount ?: 0)" } } ] }

GET products/_search
{ "sort": [ { "final_price": "asc" } ] }

Alternate interview scenarios

"Ranking uses a script that combines BM25 with sales rank and stock."

Store sales rank and stock as numeric fields and use function_score with field_value_factor, or a rank feature field. Keep any script small and only on the top results, for example in a rescore window.

"A dashboard aggregates on a runtime field and times out."

Add the field to the mapping, reindex or backfill it, and aggregate on the indexed field.

Sources (3)
  1. Elasticsearch docs: Tune for search speed (avoid scripts, pre-index data)elastic.co
  2. Elasticsearch docs: Scripts, caching, and search speedelastic.co
  3. Elasticsearch docs: Runtime fieldselastic.co
Previous
Next