Statementsedit

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

Conditional statementsedit

If / Elseedit

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 statementsedit

Foredit

Painless also supports the for in syntax:

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

Whileedit

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

Do-Whileedit

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