# Dsl Query - calculate incidence in percentage (%) or bucket\_script

**URL:** <https://discuss.elastic.co/t/dsl-query-calculate-incidence-in-percentage-or-bucket-script/323501>\
**Category:** Elasticsearch\
**Created:** [January 19, 2023, 11:24am UTC](https://discuss.elastic.co/t/dsl-query-calculate-incidence-in-percentage-or-bucket-script/323501 "2023-01-19T11:24:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SalvoDM91](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@SalvoDM91](https://discuss.elastic.co/u/SalvoDM91)\
**Post date:** [January 19, 2023, 11:24am UTC](https://discuss.elastic.co/t/dsl-query-calculate-incidence-in-percentage-or-bucket-script/323501/1 "2023-01-19T11:24:21Z")

</div>

Hi guys,

I've a sample dataset with 3 columns (ID, ACTION and AMOUNT):

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/d/1/d1c3fefbb5cc112c19d6a9029489f0bced42e529.png)

I would like to create a DSL query in order to check minute by minute:

- number of documents with ACTION = "purchase" (paramA)
- number of documents with ACTION = "refund" (paramB)
- calculate incidence of purchases minute by minute as: paramA / total number of documents (in same minute)
- calculate incidence of refunds minute by minute as: paramB / total number of documents (in same minute)

I found documentation about "bucket\_script", but I'm not able to count Items, not sum them.  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html)

My expectation is:  
minute 11:30:

- Number of documents: 6
- Number of purchases: 3
- Number of refunds: 3
- Incidence of purchases: 3 / 6 \* 100
- Incidence of refunds: 3 / 6 \* 100

Could you help me, please?

---

<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:** [February 16, 2023, 11:24am UTC](https://discuss.elastic.co/t/dsl-query-calculate-incidence-in-percentage-or-bucket-script/323501/2 "2023-02-16T11:24:29Z")

</div>

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