# Bucket Script and Gaps

**URL:** <https://discuss.elastic.co/t/bucket-script-and-gaps/301619>\
**Category:** Elasticsearch\
**Created:** [April 5, 2022, 10:09am UTC](https://discuss.elastic.co/t/bucket-script-and-gaps/301619 "2022-04-05T10:09:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bianca6](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@bianca6](https://discuss.elastic.co/u/bianca6)\
**Post date:** [April 5, 2022, 10:09am UTC](https://discuss.elastic.co/t/bucket-script-and-gaps/301619/1 "2022-04-05T10:09:15Z")

</div>

Hi,

According to the documentation, the bucket script should be able to deal with gaps:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html#gap-policy](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html#gap-policy)

But in my case, I got the following error:

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

```

I expected the gaps to be replace by 0, not to return me an error.

Here is my bucket script:

```auto
"download": {
  "aggs": {
    "type": {
      "terms": {
        "field": "event.fields.message.keyword"
      },
      "aggs": {
        "count": {
          "value_count": {
            "field": "timestamp"
          }
        }
      }
    }
  },
  "download_error_percentage": {
    "bucket_script": {
      "buckets_path": {
        "start": "download>type['Download']>count.value",
        "succ": "download>type['Download (SUCCEEDED)']>count.value",
        "fail": "download>type['Download (FAILED)']>count.value"
      },
      "gap_policy": "insert_zeros",
      "script": "params.fail / params.start"
    }
  }
}

```

I expect the "download\_error\_percentage" to be the operation result or 0 (if there is no 'Download (FAILED)')

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 5, 2022, 12:42pm UTC](https://discuss.elastic.co/t/bucket-script-and-gaps/301619/2 "2022-04-05T12:42:37Z")

</div>

Do you have a **minimal** example to share including index creation, mapping and few sample documents to reproduce?

---

<div class="post-metadata">

**Author:** ![bianca6](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@bianca6](https://discuss.elastic.co/u/bianca6)\
**Post date:** [April 5, 2022, 1:08pm UTC](https://discuss.elastic.co/t/bucket-script-and-gaps/301619/3 "2022-04-05T13:08:44Z")

</div>

More information:

There are a lot of logs, to make it simple we can see them as:

```auto
timestamp = t1, object = 1, event.fields.message.keyword = Download
timestamp = t2, object = 1, event.fields.message.keyword = Download Succedded
timestamp = t3, object = 2, event.fields.message.keyword = Download
timestamp = t4, object = 2, event.fields.message.keyword = Download Failed

```

Then, I did a Transform in order to get an "entity-centric" view, the group by is based on the field "object".

I use this transform to get the duration of download for my objects, but also the error %.

The code above is a part of my transform (aggregations). I used the bucket script in order to calculate the duration (I just substract the min an max timestamp per object).

But now, I'm trying to use another bucket script, this time for the error %. But, the missing values (not every objects is 'failed' or 'succeeded') cause an error as shown above (Cannot invoke "Object.getClass()" because "leftObject" is null)

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 7, 2022, 7:06am UTC](https://discuss.elastic.co/t/bucket-script-and-gaps/301619/4 "2022-04-07T07:06:26Z")

</div>

With minimal example I meant a snippet that I can copy & paste into the Kibana Dev Tools

---

<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:** [May 5, 2022, 7:06am UTC](https://discuss.elastic.co/t/bucket-script-and-gaps/301619/5 "2022-05-05T07:06:29Z")

</div>

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