Run a script Technical preview; Added in 6.3.0

GET /_scripts/painless/_execute

Runs a script and returns a result. Use this API to build and test scripts, such as when defining a script for a runtime field. This API requires very few dependencies and is especially useful if you don't have permissions to write documents on a cluster.

The API uses several contexts, which control how scripts are run, what variables are available at runtime, and what the return type is.

Each context requires a script, but additional parameters depend on the context you're using for that script.

application/json

Body Required

  • context string

    The context that the script should run in. NOTE: Result ordering in the field contexts is not guaranteed.

    Values are painless_test, filter, score, boolean_field, date_field, double_field, geo_point_field, ip_field, keyword_field, long_field, or composite_field.

  • context_setup object

    Additional parameters for the context. NOTE: This parameter is required for all contexts except painless_test, which is the default if no value is provided for context.

    Hide context_setup attributes Show context_setup attributes object
    • document object Required

      Document that's temporarily indexed in-memory and accessible from the script.

    • index string Required

      Index containing a mapping that's compatible with the indexed document. You may specify a remote index by prefixing the index with the remote cluster alias. For example, remote1:my_index indicates that you want to run the painless script against the "my_index" index on the "remote1" cluster. This request will be forwarded to the "remote1" cluster if you have configured a connection to that remote cluster.

      NOTE: Wildcards are not accepted in the index expression for this endpoint. The expression *:myindex will return the error "No such remote cluster" and the expression logs* or remote1:logs* will return the error "index not found".

    • query object

      Use this parameter to specify a query for computing a score.

      External documentation
      Hide query attributes Show query attributes object
      • bool object
      • boosting object
      • common object Deprecated
      • combined_fields object
      • constant_score object
      • dis_max object
      • distance_feature
      • exists object
      • function_score object
      • fuzzy object

        Returns documents that contain terms similar to the search term, as measured by a Levenshtein edit distance.

        External documentation
      • geo_bounding_box object
      • geo_distance object
      • geo_grid object

        Matches geo_point and geo_shape values that intersect a grid cell from a GeoGrid aggregation.

      • geo_polygon object
      • geo_shape object
      • has_child object
      • has_parent object
      • ids object
      • intervals object

        Returns documents based on the order and proximity of matching terms.

        External documentation
      • knn object
      • match object

        Returns documents that match a provided text, number, date or boolean value. The provided text is analyzed before matching.

        External documentation
      • match_all object
      • match_bool_prefix object

        Analyzes its input and constructs a bool query from the terms. Each term except the last is used in a term query. The last term is used in a prefix query.

        External documentation
      • match_none object
      • match_phrase object

        Analyzes the text and creates a phrase query out of the analyzed text.

        External documentation
      • match_phrase_prefix object

        Returns documents that contain the words of a provided text, in the same order as provided. The last term of the provided text is treated as a prefix, matching any words that begin with that term.

        External documentation
      • more_like_this object
      • multi_match object
      • nested object
      • parent_id object
      • percolate object
      • prefix object

        Returns documents that contain a specific prefix in a provided field.

        External documentation
      • query_string object
      • range object

        Returns documents that contain terms within a provided range.

        External documentation
      • rank_feature object
      • regexp object

        Returns documents that contain terms matching a regular expression.

        External documentation
      • rule object
      • script object
      • script_score object
      • semantic object
      • shape object
      • simple_query_string object
      • span_containing object
      • span_field_masking object
      • span_first object
      • span_multi object
      • span_near object
      • span_not object
      • span_or object
      • span_term object

        Matches spans containing a term.

        External documentation
      • span_within object
      • term object

        Returns documents that contain an exact term in a provided field. To return a document, the query term must exactly match the queried field's value, including whitespace and capitalization.

        External documentation
      • terms object
      • terms_set object

        Returns documents that contain a minimum number of exact terms in a provided field. To return a document, a required number of terms must exactly match the field values, including whitespace and capitalization.

        External documentation
      • text_expansion object Deprecated Generally available; Added in 8.8.0

        Uses a natural language processing model to convert the query text into a list of token-weight pairs which are then used in a query against a sparse vector or rank features field.

        External documentation
      • weighted_tokens object Deprecated Generally available; Added in 8.13.0

        Supports returning text_expansion query results by sending in precomputed tokens with the query.

        External documentation
      • wildcard object

        Returns documents that contain terms matching a wildcard pattern.

        External documentation
      • wrapper object
      • type object
  • script object

    The Painless script to run.

    Hide script attributes Show script attributes object
    • source string | object

      The script source.

      One of:
    • id string

      The id for a stored script.

    • params object

      Specifies any named parameters that are passed into the script as variables. Use parameters instead of hard-coded values to decrease compile time.

      Hide params attribute Show params attribute object
      • * object Additional properties
    • lang string

      Specifies the language the script is written in.

      Any of:

      Values are painless, expression, mustache, or java.

    • options object
      Hide options attribute Show options attribute object
      • * string Additional properties

Responses

  • 200 application/json
    Hide response attribute Show response attribute object
    • result object Required
GET /_scripts/painless/_execute
curl \
 --request GET 'http://api.example.com/_scripts/painless/_execute' \
 --header "Content-Type: application/json" \
 --data '"{\n  \"script\": {\n    \"source\": \"params.count / params.total\",\n    \"params\": {\n      \"count\": 100.0,\n      \"total\": 1000.0\n    }\n  }\n}"'
Request examples
Run `POST /_scripts/painless/_execute`. The `painless_test` context is the default context. It runs scripts without additional parameters. The only variable that is available is `params`, which can be used to access user defined values. The result of the script is always converted to a string.
{
  "script": {
    "source": "params.count / params.total",
    "params": {
      "count": 100.0,
      "total": 1000.0
    }
  }
}
Run `POST /_scripts/painless/_execute` with a `filter` context. It treats scripts as if they were run inside a script query. For testing purposes, a document must be provided so that it will be temporarily indexed in-memory and is accessible from the script. More precisely, the `_source`, stored fields, and doc values of such a document are available to the script being tested.
{
  "script": {
    "source": "doc['field'].value.length() <= params.max_length",
    "params": {
      "max_length": 4
    }
  },
  "context": "filter",
  "context_setup": {
    "index": "my-index-000001",
    "document": {
      "field": "four"
    }
  }
}
Run `POST /_scripts/painless/_execute` with a `score` context. It treats scripts as if they were run inside a `script_score` function in a `function_score` query.
{
  "script": {
    "source": "doc['rank'].value / params.max_rank",
    "params": {
      "max_rank": 5.0
    }
  },
  "context": "score",
  "context_setup": {
    "index": "my-index-000001",
    "document": {
      "rank": 4
    }
  }
}
Response examples (200)
A successful response from `POST /_scripts/painless/_execute` with a `painless_test` context.
{
  "result": "0.1"
}
A successful response from `POST /_scripts/painless/_execute` with a `filter` context.
{
  "result": true
}
A successful response from `POST /_scripts/painless/_execute` with a `score` context.
{
  "result": 0.8
}