Skip to content

Commit 40e3e0f

Browse files
committed
add pages
1 parent ea47019 commit 40e3e0f

File tree

6 files changed

+410
-61
lines changed

6 files changed

+410
-61
lines changed
70.8 KB
Loading

docs/reference/scripting-languages/painless/how-painless-dispatches-function.md

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,48 @@ products:
88
- id: painless
99
---
1010

11-
# How painless dispatches function [modules-scripting-painless-dispatch]
11+
# Understanding method dispatching in Painless [modules-scripting-painless-dispatch]
1212

13-
Painless uses receiver, name, and [arity](https://en.wikipedia.org/wiki/Arity) for method dispatch. For example, `s.foo(a, b)` is resolved by first getting the class of `s` and then looking up the method `foo` with two parameters. This is different from Groovy which uses the [runtime types](https://en.wikipedia.org/wiki/Multiple_dispatch) of the parameters and Java which uses the compile time types of the parameters.
13+
Painless uses a function dispatch mechanism based on the receiver, method name, and [arity](https://en.wikipedia.org/wiki/Arity) (number of parameters). This approach differs from Java, which dispatches based on compiled-time types, and Groovy, which uses [runtime types](https://en.wikipedia.org/wiki/Multiple_dispatch). Understanding this mechanism is fundamental when migrating scripts or interacting with Java standard library APIs from Painless, as it helps you avoid common errors and write more efficient, secure scripts.
1414

15-
The consequence of this that Painless doesn’t support overloaded methods like Java, leading to some trouble when it allows classes from the Java standard library. For example, in Java and Groovy, `Matcher` has two methods: `group(int)` and `group(String)`. Painless can’t allow both of these methods because they have the same name and the same number of parameters. So instead it has `group(int)` and `namedGroup(String)`.
15+
## Key terms
16+
17+
Before diving into the dispatch process, here a brief definition of the main concepts:
18+
19+
* **Receiver:** The object or class on which the method is called. For example, in `s.foo(a, b)` the variable `s` is the receiver
20+
* **Name:** The name of the method being invoked, such as `foo` in `s.foo(a, b)`
21+
* **Arity:** The number of parameters the method accepts. In `s.foo(a, b)` the arity is 2
22+
* **Dispatch:** The process of determining which method implementation to execute based on the receiver, name, and arity
23+
24+
:::{image} ../../images/painless/painless-method-dispatching.png
25+
:alt: Flowchart showing five steps: s.foo, receiver, name, arity, and execute method
26+
:width: 250px
27+
:::
28+
29+
## Why method dispatch matters
30+
31+
This fundamental difference affects how you work with Java APIs in your scripts. When translating Java code to Painless, methods you expect from the standard library might have different names or behave differently. Understanding method dispatch helps you avoid common errors and write more efficient scripts, particularly when working with `def` types that benefit from this optimized resolution mechanism.
32+
33+
## Impact on Java standard library usage
34+
35+
The consequence of the different approach used by Painless is that Painless doesn’t support overload methods like Java, leading to some trouble when it allows classes from the Java standard library. For example, in Java and Groovy, `Matcher` has two methods:
36+
37+
* `group(int)`
38+
* `group(string)`
39+
40+
Painless can’t allow both of these methods because they have the same name and the same number of parameters. Instead, it has `group(int)` and `namedGroup(String)`. If you try to call a method that is not exposed in Painless, you will get a compilation error.
41+
42+
This renaming pattern occurs throughout the Painless API when adapting Java standard library classes. Any methods that would conflict due to identical names and parameter counts receive distinct names in Painless to ensure unambiguous method resolution.
43+
44+
## Justification for this approach
1645

1746
We have a few justifications for this different way of dispatching methods:
1847

19-
1. It makes operating on `def` types simpler and, presumably, faster. Using receiver, name, and arity means that when Painless sees a call on a `def` object it can dispatch the appropriate method without having to do expensive comparisons of the types of the parameters. The same is true for invocations with `def` typed parameters.
20-
2. It keeps things consistent. It would be genuinely weird for Painless to behave like Groovy if any `def` typed parameters were involved and Java otherwise. It’d be slow for it to behave like Groovy all the time.
21-
3. It keeps Painless maintainable. Adding the Java or Groovy like method dispatch **feels** like it’d add a ton of complexity which’d make maintenance and other improvements much more difficult.
48+
1. It makes operating on `def` types simpler and, presumably, faster. Using receiver, name and arity means that when Painless sees a call on a `def` object it can dispatch the appropriate method without having to do expensive comparisons of the types of the parameters. The same is true for invocation with `def` typed parameters.
49+
2. It keeps things consistent. It would be genuinely weird for Painless to behave like Groovy if any `def` typed parameters were involved and Java otherwise. It’d be slow for Painless to behave like Groovy all the time.
50+
3. It keeps Painless maintainable. Adding the Java or Groovy like method dispatch **feels** like it’d add a lot of complexity, which would make maintenance and other improvements much more difficult.
51+
52+
## Next steps
53+
54+
For more details, view the [Painless language specification](/reference/scripting-languages/painless/painless-language-specification.md) and the [Painless API examples](/reference/scripting-languages/painless/painless-api-examples.md).
2255

docs/reference/scripting-languages/painless/painless-api-examples.md

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,61 @@ products:
88
- id: painless
99
---
1010

11-
# Painless API examples [painless-execute-api]
11+
# Painless execute API [painless-execute-api]
1212

1313
::::{warning}
1414
This functionality is in technical preview and may be changed or removed in a future release. Elastic will work to fix any issues, but features in technical preview are not subject to the support SLA of official GA features.
1515
::::
1616

17-
1817
The Painless execute API runs a script and returns a result.
1918

19+
## Description
20+
21+
Use this API to build and test scripts, such as when defining a script for a [runtime field](docs-content://manage-data/data-store/mapping/runtime-fields.md). This API requires very few dependencies and is especially useful if you don’t have the required permissions to write documents to a cluster.
22+
23+
## How it works
24+
25+
The Painless execute API runs scripts in an isolated execution environment and returns results for development and testing purposes. The request body must include a `script` object, and additional parameters may be required depending on the selected context. For example, providing a `context_setup` parameter allows you to add a document to execute the script against.
26+
27+
The API executes the script within the {{es}} Painless engine and does not perform write operations or modify indexed documents.
28+
29+
## Supported contexts
30+
31+
The API supports the following contexts for script execution:
32+
33+
### Test context (`test`)
34+
35+
Test scripts without additional parameters or context settings. Only the `params` variable is available, and results are always converted to a string. This is the default context when no other is specified.
36+
37+
### Filter context (`filter`)
38+
39+
Test scripts that return boolean values, such as those used in script queries for field comparisons or value checks. Scripts run as if inside a `script` query and have access to `_source`, stored fields, and doc values of the provided document.
40+
41+
### Score context (`score`)
42+
43+
Validate custom scoring functions for `function_score` queries, such as ranking calculations based on field values. Scripts run as if inside a `script_score` function and can access document fields for scoring calculations.
44+
45+
### Field contexts
46+
47+
Test field context scripts using the `emit` function for runtime fields:
48+
49+
* `boolean_field`: Return `true` or `false` values for boolean field types.
50+
* `date_field`: Process and emit date values as long timestamps.
51+
* `double_field`: Return sorted lists of double numeric values.
52+
* `geo_point_field`: Emit latitude and longitude coordinates for geo-point fields.
53+
* `ip_fields`: Process and return IP addresses from text data.
54+
* `keyword_field`: Return sorted lists of string values.
55+
* `long_field`: Return sorted lists of long numeric values.
56+
* `composite_field`: Return maps of values for composite runtime fields.
57+
58+
## When to use the Painless execute API
59+
60+
Use the Painless execute API in the following scenarios:
61+
62+
* To test script syntax and logic before deploying to production.
63+
* When you do not have write permissions on a live cluster.
64+
* To validate script behavior using specific test data.
65+
2066
## {{api-request-title}} [painless-execute-api-request]
2167

2268
`POST /_scripts/painless/_execute`
@@ -859,6 +905,3 @@ The response includes the values that the script emitted:
859905
}
860906
}
861907
```
862-
863-
864-

0 commit comments

Comments
 (0)