Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions docs/painless/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,6 @@ include::../Versions.asciidoc[]

include::painless-getting-started.asciidoc[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be nice to fix up painless-description at some point. It talks about "alternatives" like groovy is still an alternative.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it'd be good to clean up getting-started at some point too. Both are fine to do later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on both. Most of the documentation needs some editing. Definitely different PRs.


// include::painless-examples.asciidoc[]

// include::painless-design.asciidoc[]

include::painless-lang-spec.asciidoc[]

include::painless-syntax.asciidoc[]

include::painless-api-reference.asciidoc[]

////
Proposed Outline (WIP)
Getting Started with Painless
Accessing Doc Values
Updating Fields
Working with Dates
Using Regular Expressions
Debugging Painless Scripts

Example Scripts
Using Painless in Script Fields
Using Painless in Watches
Using Painless in Function Score Queries
Using Painless in Script Queries
Using Painless When Updating Docs
Using Painless When Reindexing

How Painless Works
Painless Architecture
Dispatching Functions

Painless Language Specification
Painless API
////

Painless API Reference
24 changes: 10 additions & 14 deletions docs/painless/painless-api-reference.asciidoc
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
["appendix",id="painless-api-reference"]
= Painless API Reference
[[painless-api-reference]]
== Painless API Reference

Painless has a strict whitelist for methods and
classes to make sure that all painless scripts are secure and fast. Most of
these methods are exposed directly from the JRE while others are part of
Elasticsearch or Painless itself. Below is a list of all available methods
grouped under the classes on which you can call them. Clicking on the method
name takes you to the documentation for the method.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should consider (not here, but as a followup/separate issue) changing this to java 10, since in 6.3 we are changing to java 10 support.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, this whole thing needs to be updated, and it's actually blocking finishing up type removal. Just never had the opportunity to go back and modify it for contexts as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd mention the Java version once and avoid repeating it in every link.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the links redirect to the Java 9 counterpart, but I agree that we need a better overall solution to keep up with the Java API moving forward. I might be inclined to only link to the last long-term release, but I'm not sure that's correct depending on what version we are technically supporting for each ES release.

the method's documentation in Java 9.

include::painless-api-reference/index.asciidoc[]
172 changes: 172 additions & 0 deletions docs/painless/painless-casting.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
[[painless-casting]]
=== Casting

Casting is the conversion of one type to another. Implicit casts are casts that
occur automatically, such as during an assignment operation. Explicit casts are
casts where you use the casting operator to explicitly convert one type to
another. This is necessary during operations where the cast cannot be inferred.

To cast to a new type, precede the expression by the new type enclosed in
parentheses, for example
`(int)x`.

The following sections specify the implicit casts that can be performed and the
explicit casts that are allowed. The only other permitted cast is casting
a single character `String` to a `char`.

*Grammar:*
[source,ANTLR4]
----
cast: '(' TYPE ')' expression
----

[[numeric-casting]]
==== Numeric Casting

The following table shows the allowed implicit and explicit casts between
numeric types. Read the table by row. To find out if you need to explicitly
cast from type A to type B, find the row for type A and scan across to the
column for type B.

IMPORTANT: Explicit casts between numeric types can result in some data loss. A
smaller numeric type cannot necessarily accommodate the value from a larger
numeric type. You might also lose precision when casting from integer types
to floating point types.

|====
| | byte | short | char | int | long | float | double
| byte | | implicit | implicit | implicit | implicit | implicit | implicit
| short | explicit | | explicit | implicit | implicit | implicit | implicit
| char | explicit | explicit | | implicit | implicit | implicit | implicit
| int | explicit | explicit | explicit | | implicit | implicit | implicit
| long | explicit | explicit | explicit | explicit | | implicit | implicit
| float | explicit | explicit | explicit | explicit | explicit | | implicit
| double | explicit | explicit | explicit | explicit | explicit | explicit |
|====


Example(s)
[source,Java]
----
int a = 1; // Declare int variable a and set it to the literal
// value 1
long b = a; // Declare long variable b and set it to int variable
// a with an implicit cast to convert from int to long
short c = (short)b; // Declare short variable c, explicitly cast b to a
// short, and assign b to c
byte d = a; // ERROR: Casting an int to a byte requires an explicit
// cast
double e = (double)a; // Explicitly cast int variable a to a double and assign
// it to the double variable e. The explicit cast is
// allowed, but it is not necessary.
----

[[reference-casting]]
==== Reference Casting

A reference type can be implicitly cast to another reference type as long as
the type being cast _from_ is a descendant of the type being cast _to_. A
reference type can be explicitly cast _to_ if the type being cast to is a
descendant of the type being cast _from_.

*Examples:*
[source,Java]
----
List x; // Declare List variable x
ArrayList y = new ArrayList(); // Declare ArrayList variable y and assign it a
// newly allocated ArrayList [1]
x = y; // Assign Arraylist y to List x using an
// implicit cast
y = (ArrayList)x; // Explicitly cast List x to an ArrayList and
// assign it to ArrayList y
x = (List)y; // Set List x to ArrayList y using an explicit
// cast (the explicit cast is not necessary)
y = x; // ERROR: List x cannot be implicitly cast to
// an ArrayList, an explicit cast is required
Map m = y; // ERROR: Cannot implicitly or explicitly cast [2]
// an ArrayList to a Map, no relationship
// exists between the two types.
----
[1] `ArrayList` is a descendant of the `List` type.
[2] `Map` is unrelated to the `List` and `ArrayList` types.

[[def-type-casting]]
==== def Type Casting
All primitive and reference types can always be implicitly cast to
`def`. While it is possible to explicitly cast to `def`, it is not necessary.

However, it is not always possible to implicitly cast a `def` to other
primitive and reference types. An explicit cast is required if an explicit
cast would normally be required between the non-def types.


*Examples:*
[source,Java]
----
def x; // Declare def variable x and set it to null
x = 3; // Set the def variable x to the literal 3 with an implicit
// cast from int to def
double a = x; // Declare double variable a and set it to def variable x,
// which contains a double
int b = x; // ERROR: Results in a run-time error because an explicit cast is
// required to cast from a double to an int
int c = (int)x; // Declare int variable c, explicitly cast def variable x to an
// int, and assign x to c
----

[[boxing-unboxing]]
==== Boxing and Unboxing

Boxing is where a cast is used to convert a primitive type to its corresponding
reference type. Unboxing is the reverse, converting a reference type to the
corresponding primitive type.

There are two places Painless performs implicit boxing and unboxing:

* When you call methods, Painless automatically boxes and unboxes arguments
so you can specify either primitive types or their corresponding reference
types.
* When you use the `def` type, Painless automatically boxes and unboxes as
needed when converting to and from `def`.

The casting operator does not support any way to explicitly box a primitive
type or unbox a reference type.

If a primitive type needs to be converted to a reference type, the Painless
reference type API supports methods that can do that. However, under normal
circumstances this should not be necessary.

*Examples:*
[source,Java]
----
Integer x = 1; // ERROR: not a legal implicit cast
Integer y = (Integer)1; // ERROR: not a legal explicit cast
int a = new Integer(1); // ERROR: not a legal implicit cast
int b = (int)new Integer(1); // ERROR: not a legal explicit cast
----

[[promotion]]
==== Promotion

Promotion is where certain operations require types to be either a minimum
numerical type or for two (or more) types to be equivalent.
The documentation for each operation that has these requirements
includes promotion tables that describe how this is handled.

When an operation promotes a type or types, the resultant type
of the operation is the promoted type. Types can be promoted to def
at compile-time; however, at run-time, the resultant type will be the
promotion of the types the `def` is representing.

*Examples:*
[source,Java]
----
2 + 2.0 // Add the literal int 2 and the literal double 2.0. The literal
// 2 is promoted to a double and the resulting value is a double.

def x = 1; // Declare def variable x and set it to the literal int 1 through
// an implicit cast
x + 2.0F // Add def variable x and the literal float 2.0.
// At compile-time the types are promoted to def.
// At run-time the types are promoted to float.
----
51 changes: 51 additions & 0 deletions docs/painless/painless-comments.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[[painless-comments]]
=== Comments

Painless supports both single-line and multi-line comments. Comments can be
included anywhere within a script. Use the `//` token anywhere on a line to
specify a single-line comment. All characters from the `//` token to the end
of the line are ignored. Use an opening `/*` token and a closing `*/` token
to specify a multi-line comment. Multi-line comments can start anywhere on a
line, and all characters in between the `/*` token and `*/` token are ignored.

*Grammar*
[source,ANTLR4]
----
SINGLE_LINE_COMMENT: '//' .*? [\n\r];
MULTI_LINE_COMMENT: '/*' .*? '*/';
----

*Examples*

Single-line comments.

[source,Painless]
----
// single-line comment

int value; // single-line comment
----

Multi-line comments.

[source,Painless]
----
/* multi-
line
comment */

int value; /* multi-
line
comment */ value = 0;

int value; /* multi-line
comment */

/* multi-line
comment */ int value;

int value; /* multi-line
comment */ value = 0;

int value; /* multi-line comment */ value = 0;
----
2 changes: 1 addition & 1 deletion docs/painless/painless-description.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ _Painless_ is a simple, secure scripting language designed specifically for use
with Elasticsearch. It is the default scripting language for Elasticsearch and
can safely be used for inline and stored scripts. For a detailed description of
the Painless syntax and language features, see the
{painless}/painless-specification.html[Painless Language Specification].
{painless}/painless-lang-spec.html[Painless Language Specification].

[[painless-features]]
You can use Painless anywhere scripts can be used in Elasticsearch. Painless
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[[painless-syntax]]
=== Painless Syntax
[[painless-general-syntax]]
=== General Syntax

[float]
[[control-flow]]
==== Control flow

Expand All @@ -17,7 +16,6 @@ for (item : list) {
}
---------------------------------------------------------

[float]
[[functions]]
==== Functions

Expand All @@ -32,7 +30,6 @@ if (isNegative(someVar)) {
}
---------------------------------------------------------

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

[float]
[[patterns]]
==== Patterns

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

[float]
[[pattern-flags]]
===== Pattern flags

Expand All @@ -84,34 +79,3 @@ Pattern class] using these characters:
|`u` | UNICODE_CASE | `'Ɛ' ==~ /ɛ/iu`
|`x` | COMMENTS (aka extended) | `'a' ==~ /a #comment/x`
|=======================================================================

[float]
[[painless-deref]]
==== Dereferences

Like lots of languages, Painless uses `.` to reference fields and call methods:

[source,painless]
---------------------------------------------------------
String foo = 'foo';
TypeWithGetterOrPublicField bar = new TypeWithGetterOrPublicField()
return foo.length() + bar.x
---------------------------------------------------------

Like Groovy, Painless uses `?.` to perform null-safe references, with the
result being `null` if the left hand side is `null`:

[source,painless]
---------------------------------------------------------
String foo = null;
return foo?.length() // Returns null
---------------------------------------------------------

Unlike Groovy, Painless doesn't support writing to `null` values with this
operator:

[source,painless]
---------------------------------------------------------
TypeWithSetterOrPublicField foo = null;
foo?.x = 'bar' // Compile error
---------------------------------------------------------
13 changes: 13 additions & 0 deletions docs/painless/painless-keywords.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[painless-keywords]]
=== Keywords

The keywords in the table below are reserved for built-in language
features. These keywords cannot be used as <<identifiers, identifiers>> or
<<painless-types, types>>.

[cols="^1,^1,^1,^1,^1"]
|====
| if | else | while | do | for
| in | continue | break | return | new
| try | catch | throw | this | instanceof
|====
Loading