# Elasticsearch sub-aggregation on bucket script - Categorize bucket script result

**URL:** https://discuss.elastic.co/t/elasticsearch-sub-aggregation-on-bucket-script-categorize-bucket-script-result/114558
**Category:** Elasticsearch
**Created:** [January 8, 2018, 4:08pm UTC](https://discuss.elastic.co/t/elasticsearch-sub-aggregation-on-bucket-script-categorize-bucket-script-result/114558 "2018-01-08T16:08:32Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![nicolas-guerrier](https://avatars.discourse-cdn.com/v4/letter/n/aeb1de/32.png) [@nicolas-guerrier](https://discuss.elastic.co/u/nicolas-guerrier)
#### Post date: [January 8, 2018, 4:08pm UTC](https://discuss.elastic.co/t/elasticsearch-sub-aggregation-on-bucket-script-categorize-bucket-script-result/114558/1 "2018-01-08T16:08:32Z")

</div>

I would like to categorize a bucket script result.  
If I take the elasticsearch example :

```
POST /sales/_search
{
    "size": 0,
    "aggs" : {
        "sales_per_month" : {
            "date_histogram" : {
                "field" : "date",
                "interval" : "month"
            },
            "aggs": {
                "t-shirts": {
                  "filter": {
                    "term": {
                      "type": "t-shirt"
                    }
                  },
                  "aggs": {
                    "sales": {
                      "sum": {
                        "field": "price"
                      }
                    }
                  }
                },
                "t-shirt-percentage": {
                    "bucket_script": {
                        "buckets_path": {
                          "tShirtSales": "t-shirts>sales"
                        },
                        "script": "if (params.tShirtSales < 10) { 1 } else if(params.tShirtSales >= 10 && params.tShirtSales < 50) { 2 } else { 3 }"
                    }
                }
            }
        }
    }
}

```

I'm trying to break out three categories according to the number of tshirt sales.  
The categories if the number of tshirt sales are:

```
 1. <10 => Category 1 
 2. 10-50 => Category 2
 3. >50 => Category 3

```

I would like to know the number of tshirt sales for each category.

I thougt to use a terms aggregations on bucket script to display this category but the bucket script not accept sub aggregation.

Is-it possible ? Have you an idea please ?

Thank you in advance for your response.

---

<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 5, 2018, 4:08pm UTC](https://discuss.elastic.co/t/elasticsearch-sub-aggregation-on-bucket-script-categorize-bucket-script-result/114558/2 "2018-02-05T16:08:58Z")

</div>

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