Skip to content

Commit c833167

Browse files
authored
Painless Spec Documentation Clean Up (#29441)
Created a flatter structure for the different sections. Cleaned up comments, keywords, and literals. Used callouts for examples where it made sense.
1 parent 7969eb7 commit c833167

13 files changed

+384
-384
lines changed

docs/painless/index.asciidoc

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,6 @@ include::../Versions.asciidoc[]
55

66
include::painless-getting-started.asciidoc[]
77

8-
// include::painless-examples.asciidoc[]
9-
10-
// include::painless-design.asciidoc[]
11-
128
include::painless-lang-spec.asciidoc[]
139

14-
include::painless-syntax.asciidoc[]
15-
1610
include::painless-api-reference.asciidoc[]
17-
18-
////
19-
Proposed Outline (WIP)
20-
Getting Started with Painless
21-
Accessing Doc Values
22-
Updating Fields
23-
Working with Dates
24-
Using Regular Expressions
25-
Debugging Painless Scripts
26-
27-
Example Scripts
28-
Using Painless in Script Fields
29-
Using Painless in Watches
30-
Using Painless in Function Score Queries
31-
Using Painless in Script Queries
32-
Using Painless When Updating Docs
33-
Using Painless When Reindexing
34-
35-
How Painless Works
36-
Painless Architecture
37-
Dispatching Functions
38-
39-
Painless Language Specification
40-
Painless API
41-
////
42-
43-
Painless API Reference
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
["appendix",id="painless-api-reference"]
2-
= Painless API Reference
1+
[[painless-api-reference]]
2+
== Painless API Reference
33

4-
Painless has a strict whitelist for methods and
5-
classes to make sure that all painless scripts are secure and fast. Most of
6-
these methods are exposed directly from the JRE while others are part of
7-
Elasticsearch or Painless itself. Below is a list of all available methods
8-
grouped under the classes on which you can call them. Clicking on the method
9-
name takes you to the documentation for the method.
10-
11-
NOTE: Methods defined in the JRE also have a `(java 9)` link which can be used
12-
to see the method's documentation in Java 9 while clicking on the method's name
13-
goes to the Java 8 documentation. Usually these aren't different but it is
14-
worth going to the version that matches the version of Java you are using to
15-
run Elasticsearch just in case.
4+
Painless has a strict whitelist for methods and classes to ensure all
5+
painless scripts are secure. Most of these methods are exposed directly
6+
from the Java Runtime Enviroment (JRE) while others are part of
7+
Elasticsearch or Painless itself. Below is a list of all available
8+
classes grouped with their respected methods. Clicking on the method
9+
name takes you to the documentation for that specific method. Methods
10+
defined in the JRE also have a `(java 9)` link which can be used to see
11+
the method's documentation in Java 9.
1612

1713
include::painless-api-reference/index.asciidoc[]
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
[[painless-casting]]
2+
=== Casting
3+
4+
Casting is the conversion of one type to another. Implicit casts are casts that
5+
occur automatically, such as during an assignment operation. Explicit casts are
6+
casts where you use the casting operator to explicitly convert one type to
7+
another. This is necessary during operations where the cast cannot be inferred.
8+
9+
To cast to a new type, precede the expression by the new type enclosed in
10+
parentheses, for example
11+
`(int)x`.
12+
13+
The following sections specify the implicit casts that can be performed and the
14+
explicit casts that are allowed. The only other permitted cast is casting
15+
a single character `String` to a `char`.
16+
17+
*Grammar:*
18+
[source,ANTLR4]
19+
----
20+
cast: '(' TYPE ')' expression
21+
----
22+
23+
[[numeric-casting]]
24+
==== Numeric Casting
25+
26+
The following table shows the allowed implicit and explicit casts between
27+
numeric types. Read the table by row. To find out if you need to explicitly
28+
cast from type A to type B, find the row for type A and scan across to the
29+
column for type B.
30+
31+
IMPORTANT: Explicit casts between numeric types can result in some data loss. A
32+
smaller numeric type cannot necessarily accommodate the value from a larger
33+
numeric type. You might also lose precision when casting from integer types
34+
to floating point types.
35+
36+
|====
37+
| | byte | short | char | int | long | float | double
38+
| byte | | implicit | implicit | implicit | implicit | implicit | implicit
39+
| short | explicit | | explicit | implicit | implicit | implicit | implicit
40+
| char | explicit | explicit | | implicit | implicit | implicit | implicit
41+
| int | explicit | explicit | explicit | | implicit | implicit | implicit
42+
| long | explicit | explicit | explicit | explicit | | implicit | implicit
43+
| float | explicit | explicit | explicit | explicit | explicit | | implicit
44+
| double | explicit | explicit | explicit | explicit | explicit | explicit |
45+
|====
46+
47+
48+
Example(s)
49+
[source,Java]
50+
----
51+
int a = 1; // Declare int variable a and set it to the literal
52+
// value 1
53+
long b = a; // Declare long variable b and set it to int variable
54+
// a with an implicit cast to convert from int to long
55+
short c = (short)b; // Declare short variable c, explicitly cast b to a
56+
// short, and assign b to c
57+
byte d = a; // ERROR: Casting an int to a byte requires an explicit
58+
// cast
59+
double e = (double)a; // Explicitly cast int variable a to a double and assign
60+
// it to the double variable e. The explicit cast is
61+
// allowed, but it is not necessary.
62+
----
63+
64+
[[reference-casting]]
65+
==== Reference Casting
66+
67+
A reference type can be implicitly cast to another reference type as long as
68+
the type being cast _from_ is a descendant of the type being cast _to_. A
69+
reference type can be explicitly cast _to_ if the type being cast to is a
70+
descendant of the type being cast _from_.
71+
72+
*Examples:*
73+
[source,Java]
74+
----
75+
List x; // Declare List variable x
76+
ArrayList y = new ArrayList(); // Declare ArrayList variable y and assign it a
77+
// newly allocated ArrayList [1]
78+
x = y; // Assign Arraylist y to List x using an
79+
// implicit cast
80+
y = (ArrayList)x; // Explicitly cast List x to an ArrayList and
81+
// assign it to ArrayList y
82+
x = (List)y; // Set List x to ArrayList y using an explicit
83+
// cast (the explicit cast is not necessary)
84+
y = x; // ERROR: List x cannot be implicitly cast to
85+
// an ArrayList, an explicit cast is required
86+
Map m = y; // ERROR: Cannot implicitly or explicitly cast [2]
87+
// an ArrayList to a Map, no relationship
88+
// exists between the two types.
89+
----
90+
[1] `ArrayList` is a descendant of the `List` type.
91+
[2] `Map` is unrelated to the `List` and `ArrayList` types.
92+
93+
[[def-type-casting]]
94+
==== def Type Casting
95+
All primitive and reference types can always be implicitly cast to
96+
`def`. While it is possible to explicitly cast to `def`, it is not necessary.
97+
98+
However, it is not always possible to implicitly cast a `def` to other
99+
primitive and reference types. An explicit cast is required if an explicit
100+
cast would normally be required between the non-def types.
101+
102+
103+
*Examples:*
104+
[source,Java]
105+
----
106+
def x; // Declare def variable x and set it to null
107+
x = 3; // Set the def variable x to the literal 3 with an implicit
108+
// cast from int to def
109+
double a = x; // Declare double variable a and set it to def variable x,
110+
// which contains a double
111+
int b = x; // ERROR: Results in a run-time error because an explicit cast is
112+
// required to cast from a double to an int
113+
int c = (int)x; // Declare int variable c, explicitly cast def variable x to an
114+
// int, and assign x to c
115+
----
116+
117+
[[boxing-unboxing]]
118+
==== Boxing and Unboxing
119+
120+
Boxing is where a cast is used to convert a primitive type to its corresponding
121+
reference type. Unboxing is the reverse, converting a reference type to the
122+
corresponding primitive type.
123+
124+
There are two places Painless performs implicit boxing and unboxing:
125+
126+
* When you call methods, Painless automatically boxes and unboxes arguments
127+
so you can specify either primitive types or their corresponding reference
128+
types.
129+
* When you use the `def` type, Painless automatically boxes and unboxes as
130+
needed when converting to and from `def`.
131+
132+
The casting operator does not support any way to explicitly box a primitive
133+
type or unbox a reference type.
134+
135+
If a primitive type needs to be converted to a reference type, the Painless
136+
reference type API supports methods that can do that. However, under normal
137+
circumstances this should not be necessary.
138+
139+
*Examples:*
140+
[source,Java]
141+
----
142+
Integer x = 1; // ERROR: not a legal implicit cast
143+
Integer y = (Integer)1; // ERROR: not a legal explicit cast
144+
int a = new Integer(1); // ERROR: not a legal implicit cast
145+
int b = (int)new Integer(1); // ERROR: not a legal explicit cast
146+
----
147+
148+
[[promotion]]
149+
==== Promotion
150+
151+
Promotion is where certain operations require types to be either a minimum
152+
numerical type or for two (or more) types to be equivalent.
153+
The documentation for each operation that has these requirements
154+
includes promotion tables that describe how this is handled.
155+
156+
When an operation promotes a type or types, the resultant type
157+
of the operation is the promoted type. Types can be promoted to def
158+
at compile-time; however, at run-time, the resultant type will be the
159+
promotion of the types the `def` is representing.
160+
161+
*Examples:*
162+
[source,Java]
163+
----
164+
2 + 2.0 // Add the literal int 2 and the literal double 2.0. The literal
165+
// 2 is promoted to a double and the resulting value is a double.
166+
167+
def x = 1; // Declare def variable x and set it to the literal int 1 through
168+
// an implicit cast
169+
x + 2.0F // Add def variable x and the literal float 2.0.
170+
// At compile-time the types are promoted to def.
171+
// At run-time the types are promoted to float.
172+
----
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[[painless-comments]]
2+
=== Comments
3+
4+
Painless supports both single-line and multi-line comments. Comments can be
5+
included anywhere within a script. Use the `//` token anywhere on a line to
6+
specify a single-line comment. All characters from the `//` token to the end
7+
of the line are ignored. Use an opening `/*` token and a closing `*/` token
8+
to specify a multi-line comment. Multi-line comments can start anywhere on a
9+
line, and all characters in between the `/*` token and `*/` token are ignored.
10+
11+
*Grammar*
12+
[source,ANTLR4]
13+
----
14+
SINGLE_LINE_COMMENT: '//' .*? [\n\r];
15+
MULTI_LINE_COMMENT: '/*' .*? '*/';
16+
----
17+
18+
*Examples*
19+
20+
Single-line comments.
21+
22+
[source,Painless]
23+
----
24+
// single-line comment
25+
26+
int value; // single-line comment
27+
----
28+
29+
Multi-line comments.
30+
31+
[source,Painless]
32+
----
33+
/* multi-
34+
line
35+
comment */
36+
37+
int value; /* multi-
38+
line
39+
comment */ value = 0;
40+
41+
int value; /* multi-line
42+
comment */
43+
44+
/* multi-line
45+
comment */ int value;
46+
47+
int value; /* multi-line
48+
comment */ value = 0;
49+
50+
int value; /* multi-line comment */ value = 0;
51+
----

docs/painless/painless-description.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ _Painless_ is a simple, secure scripting language designed specifically for use
22
with Elasticsearch. It is the default scripting language for Elasticsearch and
33
can safely be used for inline and stored scripts. For a detailed description of
44
the Painless syntax and language features, see the
5-
{painless}/painless-specification.html[Painless Language Specification].
5+
{painless}/painless-lang-spec.html[Painless Language Specification].
66

77
[[painless-features]]
88
You can use Painless anywhere scripts can be used in Elasticsearch. Painless

docs/painless/painless-syntax.asciidoc renamed to docs/painless/painless-general-syntax.asciidoc

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
[[painless-syntax]]
2-
=== Painless Syntax
1+
[[painless-general-syntax]]
2+
=== General Syntax
33

4-
[float]
54
[[control-flow]]
65
==== Control flow
76

@@ -17,7 +16,6 @@ for (item : list) {
1716
}
1817
---------------------------------------------------------
1918

20-
[float]
2119
[[functions]]
2220
==== Functions
2321

@@ -32,7 +30,6 @@ if (isNegative(someVar)) {
3230
}
3331
---------------------------------------------------------
3432

35-
[float]
3633
[[lambda-expressions]]
3734
==== Lambda expressions
3835
Lambda expressions and method references work the same as in https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html[Java].
@@ -49,7 +46,6 @@ list.sort(Integer::compare);
4946
You can make method references to functions within the script with `this`,
5047
for example `list.sort(this::mycompare)`.
5148

52-
[float]
5349
[[patterns]]
5450
==== Patterns
5551

@@ -62,7 +58,6 @@ are always constants and compiled efficiently a single time.
6258
Pattern p = /[aeiou]/
6359
---------------------------------------------------------
6460

65-
[float]
6661
[[pattern-flags]]
6762
===== Pattern flags
6863

@@ -84,34 +79,3 @@ Pattern class] using these characters:
8479
|`u` | UNICODE_CASE | `'Ɛ' ==~ /ɛ/iu`
8580
|`x` | COMMENTS (aka extended) | `'a' ==~ /a #comment/x`
8681
|=======================================================================
87-
88-
[float]
89-
[[painless-deref]]
90-
==== Dereferences
91-
92-
Like lots of languages, Painless uses `.` to reference fields and call methods:
93-
94-
[source,painless]
95-
---------------------------------------------------------
96-
String foo = 'foo';
97-
TypeWithGetterOrPublicField bar = new TypeWithGetterOrPublicField()
98-
return foo.length() + bar.x
99-
---------------------------------------------------------
100-
101-
Like Groovy, Painless uses `?.` to perform null-safe references, with the
102-
result being `null` if the left hand side is `null`:
103-
104-
[source,painless]
105-
---------------------------------------------------------
106-
String foo = null;
107-
return foo?.length() // Returns null
108-
---------------------------------------------------------
109-
110-
Unlike Groovy, Painless doesn't support writing to `null` values with this
111-
operator:
112-
113-
[source,painless]
114-
---------------------------------------------------------
115-
TypeWithSetterOrPublicField foo = null;
116-
foo?.x = 'bar' // Compile error
117-
---------------------------------------------------------
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[painless-keywords]]
2+
=== Keywords
3+
4+
The keywords in the table below are reserved for built-in language
5+
features. These keywords cannot be used as <<identifiers, identifiers>> or
6+
<<painless-types, types>>.
7+
8+
[cols="^1,^1,^1,^1,^1"]
9+
|====
10+
| if | else | while | do | for
11+
| in | continue | break | return | new
12+
| try | catch | throw | this | instanceof
13+
|====

0 commit comments

Comments
 (0)