Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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: 1 addition & 32 deletions docs/painless/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,8 @@ 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-general-syntax.asciidoc[]

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 include this from painless-lang-spec.asciidoc, rather than at the top level so it reflects how things are actually nested.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed, please note this is how it was before.

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
23 changes: 10 additions & 13 deletions docs/painless/painless-api-reference.asciidoc
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
["appendix",id="painless-api-reference"]
[appendix]
[[painless-api-reference]]
Copy link
Contributor

Choose a reason for hiding this comment

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

I wouldn't make this an appendix, it's fine as a regular top-level section.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will remove. Was just leaving it as it was. What's the purpose of appendix in this case, does it add anything special to the docs other than the 'A'?

Copy link
Contributor

Choose a reason for hiding this comment

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

This can just be another section, it doesn't need to be an appendix.

Copy link
Contributor Author

@jdconrad jdconrad Apr 16, 2018

Choose a reason for hiding this comment

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

Changed, please note this is how it was before.

= 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[]
304 changes: 304 additions & 0 deletions docs/painless/painless-basic-syntax.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
[[painless-basic-syntax]]
=== Basic Syntax

[[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];
Copy link
Member

Choose a reason for hiding this comment

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

If you are really into including bits of the grammar you can actually include it at documentation build time with a construct like this one:

["source","ANTLR4",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{parser}[comments]
--------------------------------------------------

It'd be a neat thing to do in a followup because it'd make sure the grammar never goes out of date.

Copy link
Contributor Author

@jdconrad jdconrad Apr 12, 2018

Choose a reason for hiding this comment

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

I will keep this in mind. Some of the grammar has been modified for the documentation and does not directly follow the current grammar precisely out of convenience for the reader.

MULTI_LINE_COMMENT: '/*' .*? '*/';
----

*Examples:*

Single-line and multi-line comments where `<code>` represents tokens within a
script.

[source,Java]
----
// single-line comment
<code> // single-line comment
Copy link
Member

Choose a reason for hiding this comment

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

I'd probably write some actual code here, like int i = 0; // single line comment or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

/* multi-
line
comment */
<code> /* multi-line
comment */ <code>
<code> /* multi-line comment */ <code>
----
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 probably break this up into separate single and multiline examples. (And multi-line doesn't really need a hyphen.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


[[keywords]]
==== Keywords

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

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

[[literals]]
==== Literals

Use literals to specify different types of values directly in a script.

[[integers]]
===== Integers

Use integer literals to specify an integer value in decimal, octal, or hex
notation of the <<primitive-types, primitive types>> int, long, float, or
double. Use the following single letter designations to specify the
<<primitive-types, primitive type>>: `l` or `L` for `long`, `f` or `F` for
`float`, and `d` or `D` for `double`. If not specified, the type defaults to
`int`. Use `0` as a prefix to specify an integer literal as octal, and use
`0x` or `0X` as a prefix to specify an integer literal as hex.

*Grammar:*
[source,ANTLR4]
----
INTEGER: '-'? ( '0' | [1-9] [0-9]* ) [lLfFdD]?;
OCTAL: '-'? '0' [0-7]+ [lL]?;
HEX: '-'? '0' [xX] [0-9a-fA-F]+ [lL]?;
----

*Examples:*

Integer literals of `int 0`, `double 0.0`, `long 1234`,
`float -90.0`, `int -18` in octal, and `int 3882` in hex.

[source,Java]
Copy link
Member

Choose a reason for hiding this comment

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

I'd probably use [source,painless] here instead. The language doesn't really do anything at documentation rendering time. It is used more when we test the documentation. And it might be useful to have this be accurate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Will work through switching all the examples as I edit those sections.

----
0
0D
1234L
-90f
-022
0xF2A
----

[[floats]]
===== Floats

Use floating point literals to specify a floating point value of the
<<primitive-types, primitive types>> float or double. Use the following single
Copy link
Member

Choose a reason for hiding this comment

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

Should float and double be in a monospace font (as they are below)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

letter designations for the <<primitive-types, primitive type>>: `f` for
`float` and `d` for `double`. If not specified, the type defaults to `double`.

*Grammar:*
[source,ANTLR4]
----
DECIMAL: '-'? ( '0' | [1-9] [0-9]* ) (DOT [0-9]+)? EXPONENT? [fFdD]?;
EXPONENT: ( [eE] [+\-]? [0-9]+ );
----

*Examples:*

Floating point literals of `double 0.0`, `double 1000000.0` in
exponent notation, `double 0.977777`, `double -126.34`, and `float 89.9`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe use comments like you did in the variable declaration section?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched to callouts.


[source,Java]
----
0.0
1E6
0.977777
-126.34
89.9F
----

[[strings]]
===== Strings

Use string literals to specify string values of the
<<string-type, String type>> with either single or double quotes.
Use a `\"` token to include a double-quote as part of a double-quoted string
literal. Use a `\'` token to include a single-quote as part of a single-quoted
string literal. Use a `\\` token to include a backslash as part of any string
literal.

*Grammar:*
[source,ANTLR4]
----
STRING: ( '"' ( '\\"' | '\\\\' | ~[\\"] )*? '"' )
| ( '\'' ( '\\\'' | '\\\\' | ~[\\'] )*? '\'' );
----

*Examples:*

String literals using both single-quotes and double-quotes.

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 probably split this into "Single quoted string literals" and "Double quoted string literals" examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

[source,Java]
----
"double-quoted String literal"
'single-quoted String literal'
"\"double-quoted String with escaped double-quotes\" and backslash: \\"
'\'single-quoted String with escaped single-quotes\' and backslash \\'
"double-quoted String with non-escaped 'single-quotes'"
'single-quoted String with non-escaped "double-quotes"'
----

[[characters]]
===== Characters

Use the <<casting, casting operator>> to convert string literals or
<<string-type, String type>> values into <<primitive-types, char type>> values.
<<string-type, String type>> values converted into
<<primitive-types, char type>> values must be exactly one character in length
or an error will occur.

*Examples:*

Casting string literals into <<primitive-types, char type>> values.

[source,Java]
----
(char)"C"
(char)'c'
----

Casting a <<string-type, String type>> value into a
<<primitive-types, char type>> value.

[source,Java]
----
String s = "s";
char c = (char)s;
----

[[variables]]
==== Variables

Variables in Painless must be declared and can be statically or <<dynamic-types,
dynamically typed>>.

[[variable-identifiers]]
===== Variable Identifiers

Specify variable identifiers using the following grammar. Variable identifiers
must start with a letter or underscore. You cannot use <<keywords, keywords>> or
<<painless-types, types>> as identifiers.

*Grammar:*
[source,ANTLR4]
----
ID: [_a-zA-Z] [_a-zA-Z-0-9]*;
----

*Examples:*
[source,Java]
----
_
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should document that _ is a valid variable identifier. I think Java has been trying to move away from that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed from the docs. Probably not worth removing from the language at this point given backwords compat issues, but that's a different discussion.

a
Z
id
list
list0
MAP25
_map25
----

[[variable-declaration]]
===== Variable Declaration

Variables must be declared before you use them. The format is `type-name
identifier-name`. To declare multiple variables of the same type, specify a
comma-separated list of identifier names. You can immediately assign a value to
a variable when you declare it.

*Grammar:*
[source,ANTLR4]
----
type: ID ('[' ']')*;
declaration : type ID (',' ID)*;
----

*Examples:*
[source,Java]
----
int x; // Declare a variable with type int and id x
List y; // Declare a variable with type List and id y
int x, y, z; // Declare variables with type int and ids x, y, and z
def[] d; // Declare the variable d with type def[]
int i = 10; // Declare the int variable i and set it to the int literal 10
----

[[variable-assignment]]
===== Variable Assignment

Use the equals operator (`=`) to assign a value to a variable. The format is
`identifier-name = value`. Any value expression can be assigned to any variable
as long as the types match or the expression's type can be implicitly cast to
the variable's type. An error occurs if the types do not match.

*Grammar:*
[source,ANTLR4]
----
assignment: ID '=' expression
----


*Examples:*

Assigning a literal of the appropriate type directly to a declared variable.
Copy link
Member

Choose a reason for hiding this comment

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

I'd probably replace the trailing . with a : because it is describing the block. I'm not sure what feels more right to be honest.

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'm going to stick with standard sentence notation as it's more natural if there's more than one sentence.

Copy link
Contributor

Choose a reason for hiding this comment

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

As long as it's consistent, either (or even none) works. I'd be inclined to drop the colons after the Grammar and Examples headings, they aren't really necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will remove the colons from example and grammar as I work through editing the sections.


[source,Java]
----
int i;   // Declare an int i
i = 10;  // Set the int i to the int literal 10
----

Immediately assigning a value when declaring a variable.

[source,Java]
----
int i = 10; // Declare the int variable i and set it the int literal 1
double j = 2.0; // Declare the double variable j and set it to the double
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be 10?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haven't updated this section yet. Will do so in another PR.

// literal 2.0
----

Assigning a variable of one primitive type to another variable of the same
type.

[source,Java]
----
int i = 10; // Declare the int variable i and set it to the int literal 10
int j = i;  // Declare the int variable j and set it to the int variable i
----

Assigning a reference type to a new heap allocation with the `new` operator.

[source,Java]
----
ArrayList l = new ArrayList();  // Declare an ArrayList variable l and set it
Copy link
Member

Choose a reason for hiding this comment

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

This one is one character too wide on my screen. The t gets cut off.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't get to that section yet. Only edited comments, keywords, and literals so far.

// to a newly allocated ArrayList
Map m = new HashMap(); // Declare a Map variable m and set it
// to a newly allocated HashMap
----

Assigning a variable of one reference type to another variable of the same type.

[source,Java]
----
List l = new ArrayList(); // Declare List variable l and set it a newly
// allocated ArrayList
List k = l;  // Declare List variable k and set it to the
// value of the List variable l
List m;                   // Declare List variable m and set it the
// default value null
m = k;                    // Set the value of List variable m to the value
// of List variable k
----
Copy link
Contributor

Choose a reason for hiding this comment

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

Inserting a blank line before each example would make it a little easier to read the wrapped comments. We could use callouts instead of the comments, but I think there are advantages to keeping the info inline.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Started to switch some to callouts. I really like the formatting of the callouts and think that can make it easier to follow the examples.

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,5 +1,5 @@
[[painless-syntax]]
=== Painless Syntax
[[painless-general-syntax]]
=== General Syntax

[float]
Copy link
Contributor

Choose a reason for hiding this comment

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

These subheadings should not be floated. That way, they'll show up in the "On this Page" section like the subheadings in the Basic Syntax section.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

[[control-flow]]
Expand Down
Loading