# Use script in aggregation

**URL:** <https://discuss.elastic.co/t/use-script-in-aggregation/275873>\
**Category:** Kibana\
**Created:** [June 14, 2021, 4:46pm UTC](https://discuss.elastic.co/t/use-script-in-aggregation/275873 "2021-06-14T16:46:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sam3546](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sam3546/32/79281_2.png) [@sam3546](https://discuss.elastic.co/u/sam3546)\
**Post date:** [June 14, 2021, 4:46pm UTC](https://discuss.elastic.co/t/use-script-in-aggregation/275873/1 "2021-06-14T16:46:41Z")

</div>

I think im almost there so hopefully this should be an easy fix, im trying to get a percentage value

Percentage of docs that meet the filtered\_docs criteria out of all the docs in the index.

Calculation should be - (total(from filter)/ total)\*100

The query im trying to do gets me my values but I don't know how to complete the calculation with a script

```auto
GET <index>/_search
{
  "aggs": {
    "total": { "value_count": { "field": "_id" } },
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "wildcard": {
                "field1": {
                  "value": "*<string>*"
                }
              }
            },
            {
              "range": {
                "field2": {
                  "lt": "now-30d/d"
                }
              }
            }
          ],
          "must_not": {
            "range": {
              "field3": {
                "gt": "now"
              }
            }
          }
        }
      },
      "aggs": {
        "total": {
          "value_count": { "field": "_id" }
        }
      }
    }
  }
}

```

I get the below result which is correct but I want to use a script of some kind in this aggregation to do (params.filtered.total / params.total) \* 100

```auto
"aggregations" : {
    "total" : {
      "value" : 200
    },
    "filtered_docs" : {
      "meta" : { },
      "doc_count" : 20,
      "total" : {
        "value" : 20
      }
    }
  }

```

Happy to clarify any questions

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [June 14, 2021, 7:09pm UTC](https://discuss.elastic.co/t/use-script-in-aggregation/275873/2 "2021-06-14T19:09:10Z")

</div>

Are you attempting to visualize the results in Kibana? If so, I would recommend the TSVB filter ratio function which can perform this calculation. Another alternative is Vega for custom queries. You can [compare the various editors](https://www.elastic.co/guide/en/kibana/current/create-panels-with-editors.html) that we have to learn more.

If you are not trying to visualize the results in Kibana, you can use a [bucket script](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html). However Kibana does not support this.

---

<div class="post-metadata">

**Author:** ![sam3546](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sam3546/32/79281_2.png) [@sam3546](https://discuss.elastic.co/u/sam3546)\
**Post date:** [June 15, 2021, 8:33am UTC](https://discuss.elastic.co/t/use-script-in-aggregation/275873/3 "2021-06-15T08:33:17Z")

</div>

Thanks @wylie thanks for the assistance,

Yes i am trying to visualise it in kibana but running into an issue, I was originally trying to use the filter ratio in TSVB but the filter would reduce the results and not give me the value i want.

I ONLY want to filter for the Numerator not the denominator, and Lucene does not allow me to do date maths such as the gt/lt statements. (I am using 7.9 and cannot upgrade)

```auto
"field3": {"gt": "now"} 
OR 
"field2": {"lt": "now-30d/d"}

```

I cannot set a global filter or my Denominator will also only be the filtered results and not the total number of files in the index.

I hope that makes sense.

Instead I am trying to use Vega but need to get the actual calculation working before I can start the visualisation \*_(total(from filter)/ total)100_ which is where im struggling on the syntax.

If you could assist with that i would be grateful. Cheers

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [June 15, 2021, 1:38pm UTC](https://discuss.elastic.co/t/use-script-in-aggregation/275873/4 "2021-06-15T13:38:31Z")

</div>

1. I don't believe datemath is supported in Lucene, so like I've previously mentioned to you your best option in 7.9 is Vega.

2. You need to use a [bucket script](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-aggregations-pipeline-bucket-script-aggregation.html) aggregation, and the very first code example on the page has an example.

---

<div class="post-metadata">

**Author:** ![sam3546](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sam3546/32/79281_2.png) [@sam3546](https://discuss.elastic.co/u/sam3546)\
**Post date:** [June 29, 2021, 4:18pm UTC](https://discuss.elastic.co/t/use-script-in-aggregation/275873/5 "2021-06-29T16:18:31Z")

</div>

Follow up on the question

- Wasn't able to upgrade so made do with a simplified version as a pie chart, updated the date field to be the date isn't invalid and just did field \> now/d

---

<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:** [July 27, 2021, 4:18pm UTC](https://discuss.elastic.co/t/use-script-in-aggregation/275873/6 "2021-07-27T16:18:38Z")

</div>

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