Fallback value for a bucket aggregation

I know this query works. However it does not work when the value of either "failed" or "declined" is not present.

GET /metrics-*/_search
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "transaction_lifecycle_event": [
              "AUTHORIZATION_DECLINED",
              "AUTHORIZATION_FAILED",
              "AUTHORIZATION_SUBMITTED"
            ]
          }
        }
      ]
    }
  },
  "aggs": {
    "payment_methods": {
      "terms": {
        "field": "type"
      },
      "aggs": {
        "auths": {
          "terms": {
            "field": "lifecycle_event"
          }
        },
        "auth_rate": {
          "bucket_script": {
            "gap_policy": "insert_zeros", 
            "buckets_path": {
              "declined": "auths['AUTHORIZATION_DECLINED']>_count",
              "failed": "auths['AUTHORIZATION_FAILED']>_count",
              "denominator": "auths['AUTHORIZATION_SUBMITTED']>_count"
            },
            "script": "((params.denominator - params.declined + params.failed) / params.denominator) * 100"
          }
        }
      }
    }
  }
}

Is there a way to have a default value of 0 for failed/declined in the bucket?

I tried "gap_policy": "insert_zeros", but got

"caused_by" : {
        "type" : "null_pointer_exception",
        "reason" : "Cannot invoke \"Object.getClass()\" because \"right\" is null"
      }

Without the gap policy, there is no "auth_rate" value in the result

Try include paramter in terms aggregation.

Thanks for the suggestion! But I dont think thatll work. The issue is that I want all the buckets, however its just sometimes in a given date rage, there is no 'Authorization_failed' events. I would have thought since there are none, a count on an empty result would give the count 0. However, it doesnt seem like that is the case...

I know.

Try

"auths": {
          "terms": {
            "field": "lifecycle_event",
            "include": [
              "AUTHORIZATION_DECLINED",
              "AUTHORIZATION_FAILED",
              "AUTHORIZATION_SUBMITTED"
            ],
           "min_doc_count": 0
          }
        },

Genius! That worked!
Thank you soooo much!

1 Like

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