Type to search.

Write your first script

Serverless

Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Serverless interfaces and procedures might differ from classic Elastic Stack deployments.

Stack

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

Painless is the default scripting language for Elasticsearch. It is secure, performant, and provides a natural syntax for anyone with a little coding experience.

A Painless script is structured as one or more statements and optionally has one or more user-defined functions at the beginning. A script must always have at least one statement.

The Painless execute API provides the ability to test a script with simple user-defined parameters and receive a result. Let’s start with a complete script and review its constituent parts.

  1. Index a document

    Index a document with a single field so that we have some data to work with:

    JSON
    PUT my-index-000001/_doc/1
    {
      "my_field": 5
    }
  2. Operate on a field

    You can now construct a script that operates on that field and then evaluate the script as part of a query. The following query uses the script_fields parameter of the search API to retrieve a script valuation.

    The components of this script are detailed in later pages. For now, note that the script takes my_field as input and operates on it.

    JSON
    GET my-index-000001/_search
    {
      "script_fields": {
        "my_doubled_field": {
          "script": {
            "source": "doc['my_field'].value * params['multiplier']",
            "params": {
              "multiplier": 2
            }
          }
        }
      }
    }
    1. script object
    2. script source

    The script is a standard JSON object that defines scripts under most APIs in Elasticsearch. This object requires source to define the script itself. Since script isn't set, the scripting language is interpreted as being Painless, by default.