OpenSearch/docs/painless/painless-execute-script.asciidoc
Julie Tibshirani 36a3b84fc9
Update the default for include_type_name to false. (#37285)
* Default include_type_name to false for get and put mappings.

* Default include_type_name to false for get field mappings.

* Add a constant for the default include_type_name value.

* Default include_type_name to false for get and put index templates.

* Default include_type_name to false for create index.

* Update create index calls in REST documentation to use include_type_name=true.

* Some minor clean-ups around the get index API.

* In REST tests, use include_type_name=true by default for index creation.

* Make sure to use 'expression == false'.

* Clarify the different IndexTemplateMetaData toXContent methods.

* Fix FullClusterRestartIT#testSnapshotRestore.

* Fix the ml_anomalies_default_mappings test.

* Fix GetFieldMappingsResponseTests and GetIndexTemplateResponseTests.

We make sure to specify include_type_name=true during xContent parsing,
so we continue to test the legacy typed responses. XContent generation
for the typeless responses is currently only covered by REST tests,
but we will be adding unit test coverage for these as we implement
each typeless API in the Java HLRC.

This commit also refactors GetMappingsResponse to follow the same appraoch
as the other mappings-related responses, where we read include_type_name
out of the xContent params, instead of creating a second toXContent method.
This gives better consistency in the response parsing code.

* Fix more REST tests.

* Improve some wording in the create index documentation.

* Add a note about types removal in the create index docs.

* Fix SmokeTestMonitoringWithSecurityIT#testHTTPExporterWithSSL.

* Make sure to mention include_type_name in the REST docs for affected APIs.

* Make sure to use 'expression == false' in FullClusterRestartIT.

* Mention include_type_name in the REST templates docs.
2019-01-14 13:08:01 -08:00

177 lines
4.4 KiB
Plaintext

[[painless-execute-api]]
=== Painless execute API
experimental[The painless execute api is new and the request / response format may change in a breaking way in the future]
The Painless execute API allows an arbitrary script to be executed and a result to be returned.
[[painless-execute-api-parameters]]
.Parameters
[options="header"]
|======
| Name | Required | Default | Description
| `script` | yes | - | The script to execute
| `context` | no | `painless_test` | The context the script should be executed in.
| `context_setup` | no | - | Additional parameters to the context.
|======
==== Contexts
Contexts control how scripts are executed, what variables are available at runtime and what the return type is.
===== Painless test context
The `painless_test` context executes scripts as is and do not add any special parameters.
The only variable that is available is `params`, which can be used to access user defined values.
The result of the script is always converted to a string.
If no context is specified then this context is used by default.
*Example*
Request:
[source,js]
----------------------------------------------------------------
POST /_scripts/painless/_execute
{
"script": {
"source": "params.count / params.total",
"params": {
"count": 100.0,
"total": 1000.0
}
}
}
----------------------------------------------------------------
// CONSOLE
Response:
[source,js]
--------------------------------------------------
{
"result": "0.1"
}
--------------------------------------------------
// TESTRESPONSE
===== Filter context
The `filter` context executes scripts as if they were executed inside a `script` query.
For testing purposes a document must be provided that will be indexed temporarily in-memory and
is accessible to the script being tested. Because of this the _source, stored fields and doc values
are available in the script being tested.
The following parameters may be specified in `context_setup` for a filter context:
document:: Contains the document that will be temporarily indexed in-memory and is accessible from the script.
index:: The name of an index containing a mapping that is compatible with the document being indexed.
*Example*
[source,js]
----------------------------------------------------------------
PUT /my-index?include_type_name=true
{
"mappings": {
"_doc": {
"properties": {
"field": {
"type": "keyword"
}
}
}
}
}
POST /_scripts/painless/_execute
{
"script": {
"source": "doc['field'].value.length() <= params.max_length",
"params": {
"max_length": 4
}
},
"context": "filter",
"context_setup": {
"index": "my-index",
"document": {
"field": "four"
}
}
}
----------------------------------------------------------------
// CONSOLE
Response:
[source,js]
--------------------------------------------------
{
"result": true
}
--------------------------------------------------
// TESTRESPONSE
===== Score context
The `score` context executes scripts as if they were executed inside a `script_score` function in
`function_score` query.
The following parameters may be specified in `context_setup` for a score context:
document:: Contains the document that will be temporarily indexed in-memory and is accessible from the script.
index:: The name of an index containing a mapping that is compatible with the document being indexed.
query:: If `_score` is used in the script then a query can specified that will be used to compute a score.
*Example*
[source,js]
----------------------------------------------------------------
PUT /my-index?include_type_name=true
{
"mappings": {
"_doc": {
"properties": {
"field": {
"type": "keyword"
},
"rank": {
"type": "long"
}
}
}
}
}
POST /_scripts/painless/_execute
{
"script": {
"source": "doc['rank'].value / params.max_rank",
"params": {
"max_rank": 5.0
}
},
"context": "score",
"context_setup": {
"index": "my-index",
"document": {
"rank": 4
}
}
}
----------------------------------------------------------------
// CONSOLE
Response:
[source,js]
--------------------------------------------------
{
"result": 0.8
}
--------------------------------------------------
// TESTRESPONSE