# Bucket Script Aggregation - Filter Ratio with STDEV

**URL:** <https://discuss.elastic.co/t/bucket-script-aggregation-filter-ratio-with-stdev/247746>\
**Category:** Elasticsearch\
**Created:** [September 7, 2020, 9:44am UTC](https://discuss.elastic.co/t/bucket-script-aggregation-filter-ratio-with-stdev/247746 "2020-09-07T09:44:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shiko](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@shiko](https://discuss.elastic.co/u/shiko)\
**Post date:** [September 7, 2020, 9:44am UTC](https://discuss.elastic.co/t/bucket-script-aggregation-filter-ratio-with-stdev/247746/1 "2020-09-07T09:44:39Z")

</div>

Hello,  
I'm kind of new to ES. I'm searching for the possibilities to do some high level aggregation with ES.

I want to know if I could do this on ES :

```auto

                  AVG ( Bucket X ) - AVG ( Bucket Y) 
              ----------------------------------------------------------
                                   STDEV( Bucket Y)

```

I have tried some queries , but the problem is with the division and how can I store the STDEV to apply Ratio filter on.

this is what I did:

```auto

GET /index/_search
{
  "size": 0,
  "aggs": {
    "date-ranges": {
      "date_range": {
        "field": "publishDate",
        "format": "yyyy-MM-dd",
        "ranges": [
          {
            "from": "now-61d/d",
            "to": "now-1d/d",
            "key": "count_for_60d"
          }
        ]
      },
      "aggs": {
        "STDEV_60": {
          "extended_stats": {
            "field": "Pol"
          }
        },
        "AVG_Polarity_60": {
          "avg": {
            "field": "Pol"
          }
        }
      }
    },
    "count_for_1d": {
      "filter": {
        "range": {
          "publishDate": {
            "gte": "now-1d/d",
            "lte": "now"
          }
        }
      },
      "aggs": {
        "count_1d": {
          "avg": {
            "field": "Pol"
          }
        }
      }
    }
  }
}

```

Any tips will be appreciated .. Thanks a lot

---

<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:** [October 5, 2020, 9:44am UTC](https://discuss.elastic.co/t/bucket-script-aggregation-filter-ratio-with-stdev/247746/2 "2020-10-05T09:44:55Z")

</div>

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