1

I'm trying to visualize the time difference (in hours) between two date fields.

The fields are declared like this:

 "fieldname": {
     "type": "date"
  },

The solution I found was:

,
  "script_fields" : {
    "timedifference" : {
      "script" : "doc['loading_startTime'].value - doc['startTime'].value"
    }
  }

The result I get is as follows:

enter image description here

I'd like this number to be in either seconds, minutes or hours, what am I doing wrong?

Rick van Lieshout
  • 2,276
  • 2
  • 22
  • 39

1 Answers1

2

How about this?

"script_fields" : {
  "timedifference" : {
    "script" : "def msDiff = (doc['loading_startTime'].value - doc['startTime'].value); return [msDiff/1000.0, msDiff/60000.0, msDiff/3600000.0]"
  }
}

It's going to return an array with all the differences you need, i.e.

"fields": {
    "timedifference": [
        2700,                <-- difference in seconds
        45,                  <-- difference in minutes
        0.75,                <-- difference in hours
    ]
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • Hey again, I should put you on speed dial you're so damn quick! This however hides my data, I'd like to see the other data as well. (also, how could I get the minutes difference in kibana) – Rick van Lieshout Dec 02 '16 at 15:18
  • You can just return `msDiff` in the array as well and you'll get milliseconds difference as well. – Val Dec 02 '16 at 15:25
  • Sorry I wasn't clear enough. This only returns the "fields" object but I'd like the _source object as well. Any tip for getting this in Kibana? – Rick van Lieshout Dec 02 '16 at 15:28
  • You should compute that difference at indexing time so you have a real field to use in Kibana, besides it's much more performant than running scripts at runtime. – Val Dec 02 '16 at 15:33
  • Yeah thats what several online sources said, I didn't want to do that at first because Es seems so powerful, anyways.... I'll accept your answer and wish you a good weekend! – Rick van Lieshout Dec 02 '16 at 15:43