Get current bucket value in a Moving function agg

Hi all,

I'm trying to calculate the year over year percentage change, which comes down to finding the percentage change of bucket values in a histogram.

I can use the derivative aggregation as discussed here to get the simple difference. But I don't think Bucket script agg could be used for this although it's suggested there.

Then I figured the new moving function aggregation is a great fit because I can use a window of 2 with a custom function.
But the issue with that is, moving fn agg doesn't seem to consider the current bucket value as discussed in the following issue.

Although, moving fn supports scripting, the values array which is passed in contains only the bucket values prior to the current bucket as shown in the following example. The values array length is always 1 less than the bucket count.

GET /demand/_search?typed_keys
{
  "size": 0,
  "aggs": {
    "by_qtr": {
      "histogram": {
        "field": "fiscal_quarter_year",
        "interval": 1,
        "min_doc_count": 1
      },
      "aggs": {
        "b_sum": {
          "sum": {
            "field": "qty"
          }
        },
        "movfn": {
          "moving_fn": {
            "buckets_path": "b_sum",
            "window": 2,
            "script": "return values.length"
          }
        }
      }
    }
  }
}

Response -

  "aggregations" : {
    "histogram#by_qtr" : {
      "buckets" : [
        {
          "key" : 201801.0,
          "doc_count" : 781,
          "sum#b_sum" : {
            "value" : 8156.0
          },
          "simple_value#movfn" : {
            "value" : 0.0
          }
        },
        {
          "key" : 201802.0,
          "doc_count" : 309,
          "sum#b_sum" : {
            "value" : 2460.0
          },
          "simple_value#movfn" : {
            "value" : 1.0
          }
        }
      ]
    }
  }

Ideally I should be able to use a moving function like following with some size checks
"movfn": {
"moving_fn": {
"buckets_path": "b_sum",
"window": 2,
"script": "return (values[1] / values[0]) - 1"
}
}

So is there any way to get the current bucket value in a moving function agg or is there another way to implement this requirement?

Thanks.

1 Like

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