Statements

edit

Painless supports all of Java’s control flow statements except the switch statement.

Conditional statements

edit

If / Else

edit
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

edit

For

edit

Painless also supports the for in syntax:

for (item in list) {
  // do something
}
for (def item : list) {
  // do something
}

While

edit
while (ctx._source.item < condition) {
  // do something
}

Do-While

edit
do {
  // do something
}
while (ctx._source.item < condition)