# Fallback value for a bucket aggregation

**URL:** <https://discuss.elastic.co/t/fallback-value-for-a-bucket-aggregation/295983>\
**Category:** Elasticsearch\
**Created:** [February 1, 2022, 6:44pm UTC](https://discuss.elastic.co/t/fallback-value-for-a-bucket-aggregation/295983 "2022-02-01T18:44:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Graham-Yooll](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrew_graham-yooll/32/101177_2.png) [@Andrew\_Graham-Yooll](https://discuss.elastic.co/u/Andrew_Graham-Yooll)\
**Post date:** [February 1, 2022, 6:44pm UTC](https://discuss.elastic.co/t/fallback-value-for-a-bucket-aggregation/295983/1 "2022-02-01T18:44:25Z")

</div>

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

```auto
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

```auto
"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

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 2, 2022, 2:54am UTC](https://discuss.elastic.co/t/fallback-value-for-a-bucket-aggregation/295983/2 "2022-02-02T02:54:34Z")

</div>

Try `include` paramter in terms aggregation.

> **[Terms aggregation | Elasticsearch Guide \[7.17\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values_with_exact_values_2)**

---

<div class="post-metadata">

**Author:** ![Andrew\_Graham-Yooll](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrew_graham-yooll/32/101177_2.png) [@Andrew\_Graham-Yooll](https://discuss.elastic.co/u/Andrew_Graham-Yooll)\
**Post date:** [February 2, 2022, 7:16am UTC](https://discuss.elastic.co/t/fallback-value-for-a-bucket-aggregation/295983/3 "2022-02-02T07:16:39Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 2, 2022, 7:48am UTC](https://discuss.elastic.co/t/fallback-value-for-a-bucket-aggregation/295983/4 "2022-02-02T07:48:21Z")

</div>

I know.

Try

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

```

---

<div class="post-metadata">

**Author:** ![Andrew\_Graham-Yooll](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrew_graham-yooll/32/101177_2.png) [@Andrew\_Graham-Yooll](https://discuss.elastic.co/u/Andrew_Graham-Yooll)\
**Post date:** [February 2, 2022, 7:51am UTC](https://discuss.elastic.co/t/fallback-value-for-a-bucket-aggregation/295983/5 "2022-02-02T07:51:22Z")

</div>

Genius! That worked!  
Thank you soooo much!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 2, 2022, 7:51am UTC](https://discuss.elastic.co/t/fallback-value-for-a-bucket-aggregation/295983/6 "2022-03-02T07:51:26Z")

</div>

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