Bucket script format parameter

In the documentation it states the "format" parameter (format to apply to the output value of this aggregation) but I can't figure out how to use it?

is it possible to have it format the result (a long) as a datetime?

my script is calculating a time range, like such:

"script": "params.lastDocument - params.firstDocument"

and the output is 3186895.0

Heya @Evan_Something. Looks like the answer (currently) is "no". BucketScript only supports a numeric/decimal formatter at the moment.

This is largely because bucket_script was originally designed to work entirely with numbers, which is why the output has to be a number as well. And so the formatter is designed to format numbers.

We do have some open issues about making the bucket script agg more flexible so that it can handle strings, etc. If/when those enhancements land, I imagine the formatter would be more flexible too.

We should however make the documentation more explicit that it's only a numeric formatter.

1 Like

Edited this heavily to be information about dates and format, hopefully useful to someone in the future:

The listed value of a date is something like 1.568084083246E12
(Java will convert doubles to strings with Scientific Notation)

Someone on ES irc suggested this was Decimal Format which seems to line up with what the documentation states:

Internally, dates are converted to UTC (if the time-zone is specified) and stored as a long number representing milliseconds-since-the-epoch.

Looking at a sample result query,

          "lastDocument" : {
            "value" : 1.568084083246E12,
            "value_as_string" : "2019-09-10T02:54:43.246Z"
          },
          "firstDocument" : {
            "value" : 1.568073644243E12,
            "value_as_string" : "2019-09-10T00:00:44.243Z"
          },
          "timeSpent" : {
            "value" : 1.0439003E7,
          },

So if timespent is a long of milliseconds... That means it's 10439003 milliseconds, aka 10439 seconds, which converts to 2 hours, 53 minutes and 59 seconds.

Finally, I updated my bucket_script aggregation, including a 'format' parameter, which delivers to me just the seconds value, which is simpler for post processing:

        "lastDocument" : {
          "max" : { "field" : "@timestamp" }
        },
        "firstDocument" : {
          "min" : { "field" : "@timestamp" }
        },
        "timeSpent" : {
          "bucket_script": {
            "buckets_path": {
              "firstDocument": "firstDocument",
              "lastDocument": "lastDocument"
            },
            "script": "(params.lastDocument - params.firstDocument) / 1000",
            "format": "0"
          }
        },

Which outputs nicely:

"timeSpent" : {
            "value" : 10439.003,
            "value_as_string" : "10439"
          },
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.