liveBook · Manning

Script parameters

As explained in Elasticsearch in Action, Second Edition

Script parameters are named values supplied separately from script source and made available to the script at execution time.

Script parameters are named values supplied separately from script source. The script reads them through the params object at execution time, keeping changing values such as thresholds outside the business logic. section 5.4.3

Listing 5.29 Dynamically passing a parameter to the script
POST movies/_update/1
{
  "script": {                                  #1
    "source": """                              #2
 if(ctx._source.boxoffice_gross_in_millions > params.gross_earnings_threshold) 
   {ctx._source.blockbuster = true} 
 else 
   {ctx._source.blockbuster = false}
 """,
    "params": {                                #3
      "gross_earnings_threshold":150
    }
  }
}
#1 Business logic goes here.
#2 Checks the value against params
#3 Provides the parameter values

A movie update compares box-office earnings with params.gross_earnings_threshold and sets the blockbuster flag. Change the threshold in params without editing the script logic. section 5.4.3

A parameterized script separates:

  • Business logic in source. section 5.4.3
  • Runtime values in the params object, such as gross_earnings_threshold: 150. section 5.4.3
  • Reusable behavior from values that may change, such as raising the threshold to 500. section 5.4.3

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains script anatomy and parameter substitution through one conditional update, while deferring scripting internals.
Examples
Uses a movie blockbuster flag whose earnings threshold arrives through gross_earnings_threshold.
What you can do after
Apply parameters via an Elasticsearch update script to vary thresholds without rewriting its logic.
How it connects
Connects parameters with scripts, source, languages, conditional updates, and document fields.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.