1

I am working with ELK and i have created index pattern with build_date. Now to calculate the Avg of build duration, i need to find the start-time and end-time in minutes using painless script.

My logstash output data given below

"build_end_time" => "2021-01-13 01:29:49",
"build_duration" => "6409651",
"build_start_time" => "2021-01-12 23:43:00",
"build_date" => "2021-01-12",
"@timestamp" => 2021-02-02T11:40:50.747Z,

Scripted field settings given below.

Name: Duration_time
Language: painless
Type: number
Format: Duration
Input format: minutes 
Output format: Human readable
Popularity: 0

Script: def doc['build_end_time'].date.millisOfDay - doc['build_start_time'].date.millisOfDay

it throws - Script is invalid.

{
 "root_cause": [
  {
   "type": "script_exception",
   "reason": "compile error",
   "script_stack": [
    "def doc['build_end_time'].date.m ...",
    "       ^---- HERE"
   ],
   "script": "def doc['build_end_time'].date.millisOfDay - doc['build_start_time'].date.millisOfDay",
   "lang": "painless",
   "position": {
    "offset": 7,
    "start": 0,
    "end": 32
   }
  }
 ],
 "type": "search_phase_execution_exception",
 "reason": "all shards failed",
 "phase": "query",
 "grouped": true,
 "failed_shards": [
  {
   "shard": 0,
   "index": "build-logs",
   "node": "JSvuaBbCQr6uI5qKvavj7Q",
   "reason": {
    "type": "script_exception",
    "reason": "compile error",
    "script_stack": [
     "def doc['build_end_time'].date.m ...",
     "       ^---- HERE"
    ],
    "script": "def doc['build_end_time'].date.millisOfDay - doc['build_start_time'].date.millisOfDay",
    "lang": "painless",
    "position": {
     "offset": 7,
     "start": 0,
     "end": 32
    },
    "caused_by": {
     "type": "illegal_argument_exception",
     "reason": "invalid sequence of tokens near ['['].",
     "caused_by": {
      "type": "no_viable_alt_exception",
      "reason": null
     }
    }
   }
  }
 ]
}

Can someone help me to work this?

user4948798
  • 1,924
  • 4
  • 43
  • 89

1 Answers1

0

This looks like a syntactic error.

Instead of

def doc['build_end_time'].date.millisOfDay - doc['build_start_time'].date.millisOfDay

use

return doc['build_end_time'].date.millisOfDay - doc['build_start_time'].date.millisOfDay

The return is actually not required -- you can leave it out entirely.


The def keyword defines something. So you could in theory say:

def result = doc['build_end_time'].date.millisOfDay - doc['build_start_time'].date.millisOfDay

but you'd need to return something -- so:

def result = '...'; return result
Joe - GMapsBook.com
  • 15,787
  • 4
  • 23
  • 68