Troubleshoot date math errors in Painless
Serverless Stack
Follow these guidelines to avoid date operation errors in your Painless scripts.
When you work with date fields in runtime mappings, accessing methods directly on the document field object can cause errors if the proper value accessor is not used.
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"""emit(orderDate.toInstant().toEpochMilli() + 14400000);
""",
" ^---- HERE"
],
"script": " ...",
"lang": "painless",
"position": {
"offset": 75,
"start": 61,
"end": 124
}
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "kibana_sample_data_ecommerce",
"node": "CxMTEjvKSEC0k0aTr4OM3A",
"reason": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"""emit(orderDate.toInstant().toEpochMilli() + 14400000);
""",
" ^---- HERE"
],
"script": " ...",
"lang": "painless",
"position": {
"offset": 75,
"start": 61,
"end": 124
},
"caused_by": {
"type": "illegal_argument_exception",
"reason": "dynamic method [org.elasticsearch.index.fielddata.ScriptDocValues.Dates, toInstant/0] not found"
}
}
}
]
},
"status": 400
}
"script": {
"lang": "painless",
"source": """
def orderDate = doc['order_date'];
emit(orderDate.toInstant().toEpochMilli() + 14400000);
"""
}
The script attempts to call toInstant() directly on a ScriptDocValues.Dates object. Date fields in Painless require accessing the .value property to get the actual date value before calling date methods.
Access the date value using .value before calling date methods:
"script": {
"lang": "painless",
"source": """
def orderDate = doc['order_date'].value;
emit(orderDate.toInstant().toEpochMilli() + 14400000);
"""
}
- Appended `.value` to the method.
- Always use
.valuewhen accessing single values from document fields in Painless. - Check for empty fields when the field might not exist in all documents.
- Date arithmetic should be performed on the actual date value, not the field container object.