Statements
Serverless Stack
Statements are individual instructions that perform actions in your Painless scripts. They control the flow of the code, define logic branches, and manage how your code processes data. Painless supports all Java control flow statements except the switch statement.
Statements in Painless allow you to create conditional logic, iterate through data, and structure your scripts for complex data processing tasks. Understanding these control structures is essential for writing effective scripts that can handle various scenarios in Elasticsearch workflows.
Conditional statements enable your script to run different code paths based on values in the data.
Use if, else if, and else statements to create conditional logic that runs different code blocks based on boolean expressions.
In this example, the product’s category is determined based on the product’s price value, classifying it as "Affordable"m "Moderately priced", or "Expensive".
int price = 64;
String priceCategory;
if (price > 60) {
priceCategory = "Expensive";
} else if (price < 30) {
priceCategory = "Affordable";
} else {
priceCategory = "Moderately Priced";
}
return priceCategory;
- Expensive
The ternary operator (? :) provides a concise way to perform conditional assignments. It’s a conditional statement that achieves the same purpose as if/else but in a more compact form.
In this example, the product’s category is determined based on the product’s price value, classifying it as "Affordable" or "Expensive".
int price = 64;
String priceCategory;
priceCategory = (price >= 60) ? "Affordable" : "Expensive";
return priceCategory;
- Expensive
Loop statements allow you to repeat running code multiple times, either for a specific number of iterations or while a condition remains true.
Painless supports both traditional for loops and enhanced for loops (for-each). Use for loops to iterate through data collections or repeat operations a specific number of times.
Traditional for loop:
The following loop creates an empty array with four positions and assigns a value from
0to3to each position.int[] arr = new int[4]; for (int i = 0; i < 4; i++) { arr[i] = i; } return arr;- [0, 1, 2, 3]
Enhanced
forloop (for-each):The following code snippets create a list containing letters. Using a
for-eachloop, they concatenate the letters into a single string calledword.List letters = ["h", "e", "l", "l", "o"]; String word = ""; for (l in letters) { word += l; } return word;- hello
Alternative
forloop syntax:List letters = ["h", "e", "l", "l", "o"]; String word = ""; for (def l : letters) { word += l; } return word;- hello
Use while loops to repeat running code as long as a specified condition remains true. The condition is evaluated before each iteration.
Similar to the first example for the for statement, this one assigns a number from 0 to 3 to each position of an array with four elements using a while loop.
int[] arr = new int[4];
int i = 0;
while (i < 4) {
arr[i] = i;
i++;
}
return arr;
- counter
- increment counter
- [0, 1, 2, 3]
Use do-while loops to run code at least once, then repeat as long as the condition remains true. The condition is evaluated after each iteration.
This code defines an array with mixed types (strings and integers) and uses a do-while loop to concatenate all elements into a single string called word.
def[] letters = new def[] {"a", 1, "b", 2};
String word = "";
int i = 0;
do {
word += letters[i];
i++;
} while (i < letters.length)
return word;
- counte
- increment counter
- a1b2