OpenSearch/docs/painless/painless-lang-spec/painless-statements.asciidoc

58 lines
1.4 KiB
Plaintext

[[painless-statements]]
=== Statements
Painless supports all of Java's https://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html[
control flow statements] except the `switch` statement.
==== Conditional statements
===== If / Else
[source,painless]
---------------------------------------------------------
if (doc[item].size() == 0) {
// do something if "item" is missing
} else if (doc[item].value == 'something') {
// do something if "item" value is: something
} else {
// do something else
}
---------------------------------------------------------
==== Loop statements
===== For
Painless also supports the `for in` syntax:
[source,painless]
---------------------------------------------------------
for (def item : list) {
// do something
}
---------------------------------------------------------
[source,painless]
---------------------------------------------------------
for (item in list) {
// do something
}
---------------------------------------------------------
===== While
[source,painless]
---------------------------------------------------------
while (ctx._source.item < condition) {
// do something
}
---------------------------------------------------------
===== Do-While
[source,painless]
---------------------------------------------------------
do {
// do something
}
while (ctx._source.item < condition)
---------------------------------------------------------