Hi @FTOR
You are close but missing a few key concepts ...
1 runtime fields are fields not _source so if you had run your search
GET index_example/_search
{
"fields": [ "*"]
}
The result would be
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"emit(doc['count']/(doc['end']-doc['start']))",
" ^---- HERE"
],
"script": "emit(doc['count']/(doc['end']-doc['start']))",
"lang": "painless",
"position": {
"offset": 33,
"start": 0,
"end": 44
}
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
.....
Bad Definition
So now on to the solution / Suggestions
First when you are new to runtime fields use the Data View / Interactive UI to do it.
Remove the index, remove the bad runtime mapping and start over.
Put your mapping in without the runtime field
Add the Doc
Create A Data View and Add a Runtime Field
Now you can work on it interactively
Next you need to look at the painless docs ... especially around dates .. it is basically Java
dates are ZonedDateTimes types
So not sure if you have seconds or millis... there is a couple ways to do it
Here is my code there are several ways, also this does not protect against missing value etc...
long differenceInMillis = ChronoUnit.MILLIS.between(doc['start'].value, doc['end'].value);
double average = (double) doc['count'].value / differenceInMillis;
emit (average);
Now that that works ... you can add it to the actual mapping of you like