# Sale Amount Per Second in a Five Minute Window

**URL:** <https://discuss.elastic.co/t/sale-amount-per-second-in-a-five-minute-window/98199>\
**Category:** Elasticsearch\
**Created:** [August 24, 2017, 9:26am UTC](https://discuss.elastic.co/t/sale-amount-per-second-in-a-five-minute-window/98199 "2017-08-24T09:26:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gringo](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@gringo](https://discuss.elastic.co/u/gringo)\
**Post date:** [August 24, 2017, 9:26am UTC](https://discuss.elastic.co/t/sale-amount-per-second-in-a-five-minute-window/98199/1 "2017-08-24T09:26:09Z")

</div>

I am trying to get the total amount of sale divide by 300 secs to get the sale amount per second in a five minute window

I am so far only able to construct the query until here. There seems to be no way to do a division on "total\_value\_five\_mins".

My elasticsearch version is 2.3

Tried all the elasticsearch docs can't understand a single one.

```
{ 
  "size": 0,
  "query": {   
    "aggs" : {
        "five_minute_data" : {
            "date_histogram" : {
                "field" : "timestamp",
                "interval" : "5m"
            },
            "aggs": {
              "total_value_five_mins": {
                "sum": {
                  "field": "sales"
                }
              }
            }
            
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 24, 2017, 10:09am UTC](https://discuss.elastic.co/t/sale-amount-per-second-in-a-five-minute-window/98199/2 "2017-08-24T10:09:59Z")

</div>

There are several ways of doing this, but the easiest may be to use a "bucket script aggregation" ([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)). This is an pipeline aggregation that executes a script on the result of another aggregation.

The following should work for you. This takes the result of the `total_value_five_mins` sum aggregation and divides the result by 300.

```
{
  "size": 0,
  "aggs": {
    "five_minute_data": {
      "date_histogram": {
        "field": "timestamp",
        "interval": "5m"
      },
      "aggs": {
        "total_value_five_mins": {
          "sum": {
            "field": "sales"
          }
        },
        "sales_per_second": {
          "bucket_script": {
            "buckets_path": {
              "total_value_five_mins": "total_value_five_mins"
            },
            "script": "params.total_value_five_mins / 300"
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![gringo](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@gringo](https://discuss.elastic.co/u/gringo)\
**Post date:** [August 24, 2017, 10:24am UTC](https://discuss.elastic.co/t/sale-amount-per-second-in-a-five-minute-window/98199/3 "2017-08-24T10:24:57Z")

</div>

does bucket script works on elasticsearch 2.3?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 24, 2017, 10:26am UTC](https://discuss.elastic.co/t/sale-amount-per-second-in-a-five-minute-window/98199/4 "2017-08-24T10:26:46Z")

</div>

Yep, should work. Check out the 2.3 docs here: [https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations-pipeline-bucket-script-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations-pipeline-bucket-script-aggregation.html)

---

<div class="post-metadata">

**Author:** ![gringo](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@gringo](https://discuss.elastic.co/u/gringo)\
**Post date:** [August 24, 2017, 10:29am UTC](https://discuss.elastic.co/t/sale-amount-per-second-in-a-five-minute-window/98199/5 "2017-08-24T10:29:17Z")

</div>

Got this error

```
{
   "error": {
      "root_cause": [],
      "type": "reduce_search_phase_exception",
      "reason": "[reduce] ",
      "phase": "fetch",
      "grouped": true,
      "failed_shards": [],
      "caused_by": {
         "type": "script_exception",
         "reason": "failed to run inline script [params.total_value_five_mins / 300] using lang [groovy]",
         "caused_by": {
            "type": "missing_property_exception",
            "reason": "No such property: params for class: bf3afb5e69f712ab651c4734eb9b7e2fbd478eb8"
         }
      }
   },
   "status": 503
}
```

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [August 24, 2017, 10:30am UTC](https://discuss.elastic.co/t/sale-amount-per-second-in-a-five-minute-window/98199/6 "2017-08-24T10:30:21Z")

</div>

change your script to be `"total_value_five_mins / 300"` for version 2.3 of Elasticsearch.

---

<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:** [September 21, 2017, 10:31am UTC](https://discuss.elastic.co/t/sale-amount-per-second-in-a-five-minute-window/98199/8 "2017-09-21T10:31:01Z")

</div>

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