Type to search.

Functions

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.

A function is a reusable block of code that performs a specific task and can be called multiple times throughout your script. Functions help organize your code, reduce repetition, and make complex scripts more maintainable and readable.

Functions in Painless work similarly to Java functions, allowing you to encapsulate logic, accept input parameters, and return calculated results. You can define custom functions to handle common operations, calculations, or data transformations that your script needs to perform repeatedly.

In the statement(s) of a function, a parameter is a named type value available as a variable. Each function specifies zero-to-many parameters, and when a function is called, a value is specified per parameter. An argument is a value passed into a function at the point of call. A function specifies a type value, though if the type is void then no value is returned. Any non-void type return value is available for use within an operation or is discarded otherwise.

You can declare functions at the beginning of a Painless script. Functions must be declared before they are used in your script.

  • Function with return value:

    This code calculates the VAT tax on an amount = 100

    Java
    double calculateVAT(double amount) {
        return amount * 0.19;
    }
    
    return calculateVAT(100);
    
    1. return double type
  • Function with void return type:

    This snippet uses a function called addName to add the name “Elyssa” to a list of names.

    Java
    void addName(List l, String n) {
        l.add(n);
    }
    
    List names = new ArrayList();
    addName(names, "Elyssa");
    
  • Simple boolean function:

    This code uses a boolean function to evaluate the if statement. If the customer is prime, the code inside the statement is run; otherwise, it continues with the next part of the program.

    Java
    boolean hasFreeShipping(def customer) { customer.isPrime }
    ...
    if (hasFreeShipping(client)) {
     ...
    }
    

When you write functions in Painless:

  • Use descriptive names that clearly indicate what the function does.
  • Define appropriate parameter types to ensure type safety.
  • Keep functions focused on a single, well-defined task.
  • Return consistent types to make functions predictable.
  • Declare functions at the beginning of your script for clarity.