Keywords (reserved terms)
Serverless Stack
Keywords are reserved tokens for built-in language features in Painless. These special words have predefined meanings and cannot be used as identifiers, such as variable names, function names, or field names.
In Painless documentation, "keywords" refers to reserved words in the scripting language itself. They are different from the Elasticsearch keyword field type, which is used for exact-value searches and aggregations in your data mappings.
When you write Painless scripts, keywords provide the fundamental building blocks for creating logic, defining data types, and controlling program flow. Since these words have special significance to the Painless compiler, attempting to use them for other purposes results in compilation errors.
| if | else | while | do | for |
|---|---|---|---|---|
| in | continue | break | return | new |
| try | catch | throw | this | instanceof |
Examples of restricted terms include if, which tells the compiler to create a conditional statement, and int, which declares an integer variable type.
// Keywords used correctly for their intended purpose
int count = 0;
boolean isActive = true;
if (count > 0) {
return count;
}
- `int' declares integer type
- 'boolean' declares boolean type, 'true' is literal
- 'if' creates conditional logic
- 'return' exits with value
If a keyword is used as an identifier Painless generates a compilation error:
// These will cause compilation errors
int if = 10;
String return = "value";
boolean int = false;
// Use descriptive names instead
int count = 10;
String result = "value";
boolean isEnabled = false;
- Cannot use 'if' as variable name
- Cannot use 'return' as variable name
- Cannot use 'int' as variable name
These restrictions ensure that your scripts remain readable and that the Painless compiler can correctly parse your code without ambiguity.