elasticsearch/docs/reference/scripting-languages/painless/painless-statements.md
Colleen McGinnis ab5ff67bce
[docs] Add products to docset.yml (#128274)
* add products to docset.yml

* add page-level painless tags
2025-05-21 13:55:32 -05:00

66 lines
1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
mapped_pages:
- https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-statements.html
products:
- id: painless
---
# Statements [painless-statements]
Painless supports all of Javas [ control flow statements](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.md) except the `switch` statement.
## Conditional statements [_conditional_statements]
### If / Else [_if_else]
```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 [_loop_statements]
### For [_for]
Painless also supports the `for in` syntax:
```painless
for (def item : list) {
// do something
}
```
```painless
for (item in list) {
// do something
}
```
### While [_while]
```painless
while (ctx._source.item < condition) {
// do something
}
```
### Do-While [_do_while]
```painless
do {
// do something
}
while (ctx._source.item < condition)
```