Ingest processor context
editIngest processor context
editUse a Painless script in an ingest processor to modify documents upon insertion.
Variables
-
params
(Map
, read-only) - User-defined parameters passed in as part of the query.
-
ctx['_index']
(String
) - The name of the index.
-
ctx['_type']
(String
) - The type of document within an index.
-
ctx
(Map
) -
Contains extracted JSON in a
Map
andList
structure for the fields that are part of the document.
Side Effects
-
ctx['_index']
- Modify this to change the destination index for the current document.
-
ctx['_type']
- Modify this to change the type for the current document.
-
ctx
(Map
) -
Modify the values in the
Map/List
structure to add, modify, or delete the fields of a document.
Return
- void
- No expected return value.
API
The standard Painless API is available.
Example
To run this example, first follow the steps in context examples.
The seat data contains:
-
A date in the format
YYYY-MM-DD
where the second digit of both month and day is optional. -
A time in the format HH:MM* where the second digit of both hours and minutes
is optional. The star (*) represents either the
String
AM
orPM
.
The following ingest script processes the date and time Strings
and stores the
result in a datetime
field.
String[] split(String s, char d) { int count = 0; for (char c : s.toCharArray()) { if (c == d) { ++count; } } if (count == 0) { return new String[] {s}; } String[] r = new String[count + 1]; int i0 = 0, i1 = 0; count = 0; for (char c : s.toCharArray()) { if (c == d) { r[count++] = s.substring(i0, i1); i0 = i1 + 1; } ++i1; } r[count] = s.substring(i0, i1); return r; } String[] dateSplit = split(ctx.date, (char)"-"); String year = dateSplit[0].trim(); String month = dateSplit[1].trim(); if (month.length() == 1) { month = "0" + month; } String day = dateSplit[2].trim(); if (day.length() == 1) { day = "0" + day; } boolean pm = ctx.time.substring(ctx.time.length() - 2).equals("PM"); String[] timeSplit = split( ctx.time.substring(0, ctx.time.length() - 2), (char)":"); int hours = Integer.parseInt(timeSplit[0].trim()); int minutes = Integer.parseInt(timeSplit[1].trim()); if (pm) { hours += 12; } String dts = year + "-" + month + "-" + day + "T" + (hours < 10 ? "0" + hours : "" + hours) + ":" + (minutes < 10 ? "0" + minutes : "" + minutes) + ":00+08:00"; ZonedDateTime dt = ZonedDateTime.parse( dts, DateTimeFormatter.ISO_OFFSET_DATE_TIME); ctx.datetime = dt.getLong(ChronoField.INSTANT_SECONDS)*1000L;
Creates a |
|
The first pass through each |
|
Returns the original |
|
Creates an array type value to collect the split |
|
The second pass through each |
|
Collects the last substring into the array type value of |
|
Uses the
|
|
Appends the string literal |
|
Appends the string literal |
|
Sets the
|
|
Uses the
|
|
If the time |
|
Builds a new time |
|
Creates a |
|
Sets the datetime field
|
Submit the following request:
PUT /_ingest/pipeline/seats { "description": "update datetime for seats", "processors": [ { "script": { "source": "String[] split(String s, char d) { int count = 0; for (char c : s.toCharArray()) { if (c == d) { ++count; } } if (count == 0) { return new String[] {s}; } String[] r = new String[count + 1]; int i0 = 0, i1 = 0; count = 0; for (char c : s.toCharArray()) { if (c == d) { r[count++] = s.substring(i0, i1); i0 = i1 + 1; } ++i1; } r[count] = s.substring(i0, i1); return r; } String[] dateSplit = split(ctx.date, (char)\"-\"); String year = dateSplit[0].trim(); String month = dateSplit[1].trim(); if (month.length() == 1) { month = \"0\" + month; } String day = dateSplit[2].trim(); if (day.length() == 1) { day = \"0\" + day; } boolean pm = ctx.time.substring(ctx.time.length() - 2).equals(\"PM\"); String[] timeSplit = split(ctx.time.substring(0, ctx.time.length() - 2), (char)\":\"); int hours = Integer.parseInt(timeSplit[0].trim()); int minutes = Integer.parseInt(timeSplit[1].trim()); if (pm) { hours += 12; } String dts = year + \"-\" + month + \"-\" + day + \"T\" + (hours < 10 ? \"0\" + hours : \"\" + hours) + \":\" + (minutes < 10 ? \"0\" + minutes : \"\" + minutes) + \":00+08:00\"; ZonedDateTime dt = ZonedDateTime.parse(dts, DateTimeFormatter.ISO_OFFSET_DATE_TIME); ctx.datetime = dt.getLong(ChronoField.INSTANT_SECONDS)*1000L;" } } ] }