# Create a stored script
PUT _scripts/getTimestampUnitValue
{
"script": {
"lang": "painless",
"source": """
def value = ctx[params['field']];
if (value == null) {
return;
}
def timestamp = ZonedDateTime.parse(value);
def localTimestamp = timestamp.withZoneSameInstant(ZoneId.of(params['timezone'])).toLocalDateTime();
def get = params['get'];
def target = params['target_field'];
if (get == 'month') {
ctx[target] = localTimestamp.getMonthValue();
} else if (get == 'dayOfMonth') {
ctx[target] = localTimestamp.getDayOfMonth();
} else if (get == 'dayOfYear') {
ctx[target] = localTimestamp.getDayOfYear();
}
"""
}
}
# Reuse the script from ingest pipelines
POST _ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"script": {
"id": "getTimestampUnitValue",
"params": {
"field": "order_date",
"get": "month",
"target_field": "month",
"timezone": "Asia/Tokyo"
}
}
},
{
"script": {
"id": "getTimestampUnitValue",
"params": {
"field": "order_date",
"get": "dayOfMonth",
"target_field": "day_of_month",
"timezone": "Asia/Tokyo"
}
}
},
{
"script": {
"id": "getTimestampUnitValue",
"params": {
"field": "order_date",
"get": "dayOfYear",
"target_field": "day_of_year",
"timezone": "Asia/Tokyo"
}
}
}
]
},
"docs": [
{
"_source": {
"description": "Does not have order_date"
}
},
{
"_source": {
"description": "If parsed as UTC, it fill be Feb 28 instead of Mar 1.",
"order_date": "2022-03-01T00:01:23.456+09:00"
}
}
]
}
Result:
{
"docs" : [
{
"doc" : {
"_index" : "_index",
"_id" : "_id",
"_source" : {
"description" : "Does not have order_date"
},
"_ingest" : {
"timestamp" : "2022-03-11T09:33:24.588671723Z"
}
}
},
{
"doc" : {
"_index" : "_index",
"_id" : "_id",
"_source" : {
"order_date" : "2022-03-01T00:01:23.456+09:00",
"month" : 3,
"day_of_year" : 60,
"description" : "If parsed as UTC, it fill be Feb 28 instead of Mar 1.",
"day_of_month" : 1
},
"_ingest" : {
"timestamp" : "2022-03-11T09:33:24.588678409Z"
}
}
}
]
}