2018-12-20 08:04:54 -05:00
|
|
|
[[script-processor]]
|
2020-08-12 11:49:54 -04:00
|
|
|
=== Script processor
|
|
|
|
++++
|
|
|
|
<titleabbrev>Script</titleabbrev>
|
|
|
|
++++
|
2018-12-20 08:04:54 -05:00
|
|
|
|
|
|
|
Allows inline and stored scripts to be executed within ingest pipelines.
|
|
|
|
|
|
|
|
See <<modules-scripting-using, How to use scripts>> to learn more about writing scripts. The Script Processor
|
|
|
|
leverages caching of compiled scripts for improved performance. Since the
|
|
|
|
script specified within the processor is potentially re-compiled per document, it is important
|
|
|
|
to understand how script caching works. To learn more about
|
|
|
|
caching see <<modules-scripting-using-caching, Script Caching>>.
|
|
|
|
|
|
|
|
[[script-options]]
|
|
|
|
.Script Options
|
|
|
|
[options="header"]
|
|
|
|
|======
|
|
|
|
| Name | Required | Default | Description
|
|
|
|
| `lang` | no | "painless" | The scripting language
|
|
|
|
| `id` | no | - | The stored script id to refer to
|
|
|
|
| `source` | no | - | An inline script to be executed
|
|
|
|
| `params` | no | - | Script Parameters
|
|
|
|
include::common-options.asciidoc[]
|
|
|
|
|======
|
|
|
|
|
|
|
|
One of `id` or `source` options must be provided in order to properly reference a script to execute.
|
|
|
|
|
|
|
|
You can access the current ingest document from within the script context by using the `ctx` variable.
|
|
|
|
|
|
|
|
The following example sets a new field called `field_a_plus_b_times_c` to be the sum of two existing
|
|
|
|
numeric fields `field_a` and `field_b` multiplied by the parameter param_c:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"script": {
|
|
|
|
"lang": "painless",
|
|
|
|
"source": "ctx.field_a_plus_b_times_c = (ctx.field_a + ctx.field_b) * params.param_c",
|
|
|
|
"params": {
|
|
|
|
"param_c": 10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// NOTCONSOLE
|
|
|
|
|
|
|
|
It is possible to use the Script Processor to manipulate document metadata like `_index` and `_type` during
|
2020-07-27 15:58:26 -04:00
|
|
|
ingestion. Here is an example of an Ingest Pipeline that renames the index and type to `my-index` no matter what
|
2018-12-20 08:04:54 -05:00
|
|
|
was provided in the original index request:
|
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2018-12-20 08:04:54 -05:00
|
|
|
--------------------------------------------------
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT _ingest/pipeline/my-index
|
2018-12-20 08:04:54 -05:00
|
|
|
{
|
2020-07-27 15:58:26 -04:00
|
|
|
"description": "use index:my-index",
|
2020-07-21 15:49:58 -04:00
|
|
|
"processors": [
|
|
|
|
{
|
|
|
|
"script": {
|
|
|
|
"source": """
|
2020-07-27 15:58:26 -04:00
|
|
|
ctx._index = 'my-index';
|
2020-07-21 15:49:58 -04:00
|
|
|
ctx._type = '_doc';
|
|
|
|
"""
|
2018-12-20 08:04:54 -05:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
|
|
|
]
|
2018-12-20 08:04:54 -05:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2020-07-27 15:58:26 -04:00
|
|
|
Using the above pipeline, we can attempt to index a document into the `any-index` index.
|
2018-12-20 08:04:54 -05:00
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2018-12-20 08:04:54 -05:00
|
|
|
--------------------------------------------------
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT any-index/_doc/1?pipeline=my-index
|
2018-12-20 08:04:54 -05:00
|
|
|
{
|
|
|
|
"message": "text"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TEST[continued]
|
|
|
|
|
|
|
|
The response from the above index request:
|
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console-result]
|
2018-12-20 08:04:54 -05:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
2020-07-27 15:58:26 -04:00
|
|
|
"_index": "my-index",
|
2018-12-20 08:04:54 -05:00
|
|
|
"_type": "_doc",
|
|
|
|
"_id": "1",
|
|
|
|
"_version": 1,
|
|
|
|
"result": "created",
|
|
|
|
"_shards": {
|
|
|
|
"total": 2,
|
|
|
|
"successful": 1,
|
|
|
|
"failed": 0
|
|
|
|
},
|
|
|
|
"_seq_no": 89,
|
|
|
|
"_primary_term": 1,
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
|
|
|
|
|
2020-07-27 15:58:26 -04:00
|
|
|
In the above response, you can see that our document was actually indexed into `my-index` instead of
|
|
|
|
`any-index`. This type of manipulation is often convenient in pipelines that have various branches of transformation,
|
2018-12-20 08:04:54 -05:00
|
|
|
and depending on the progress made, indexed into different indices.
|