Debug date math errors in Painless
Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud. Availability This functionality is generally available and ready for production usage. Serverless interfaces and procedures might differ from classic Elastic Stack deployments. The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats. Availability This functionality is generally available and ready for production usage. Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
Serverless
Generally available
Stack
Generally available
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.
Follow these guidelines to avoid date operation errors in your Painless scripts:
- 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.
For details, refer to the following sample error and solution.
{
"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
.valueto the method.