# Aggregation on terms and find min value of script results

**URL:** <https://discuss.elastic.co/t/aggregation-on-terms-and-find-min-value-of-script-results/240214>\
**Category:** Elasticsearch\
**Created:** [July 7, 2020, 7:51pm UTC](https://discuss.elastic.co/t/aggregation-on-terms-and-find-min-value-of-script-results/240214 "2020-07-07T19:51:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akihito\_Kanbara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akihito_kanbara/32/71754_2.png) [@Akihito\_Kanbara](https://discuss.elastic.co/u/Akihito_Kanbara)\
**Post date:** [July 7, 2020, 7:51pm UTC](https://discuss.elastic.co/t/aggregation-on-terms-and-find-min-value-of-script-results/240214/1 "2020-07-07T19:51:05Z")

</div>

Hi, I have a use case need to first groupBy "user" and do some calculation and find the min value of the results.  
What I am doing is:

```auto
GET test/_search
{
  "size": 0,
  "aggs": {
    "throughput": {
      "terms": {
        "field": "user.keyword"
      },
      "aggs": {
        "sum_len": {
          "sum": {
            "field": "processed_length"
          }
        },
        "sum_pt": {
          "sum": {
            "field": "processed_duration"
          }
        },
        "throu": {
          "bucket_script": {
            "buckets_path": {
              "len": "sum_len",
              "pt": "sum_pt"
            },
            "script": "params.len / params.pt"
          }
        }
      }
    },
    "min_throu": {
      "min_bucket": {
        "buckets_path": "throughput>throu"
      }
    }
  }
}

```

It is working but I have a few questions:  
I have about 100,000 users so the buckets number will be large. I need to set size = LARGE NUMBER in term aggregation. Is it safe to do it since I will need really a lot of buckets? Is there an alternate way to this job?

Another question is about Java REST Client, I can do filter\_path to filter the response, but I did not find any way to do it with Java Client. Since I just care about the min value, I dont want the response carry redundent data which will make it slow. Is there any way I can reduce the size of response with Java client?

---

<div class="post-metadata">

**Author:** ![Akihito\_Kanbara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akihito_kanbara/32/71754_2.png) [@Akihito\_Kanbara](https://discuss.elastic.co/u/Akihito_Kanbara)\
**Post date:** [July 8, 2020, 12:27am UTC](https://discuss.elastic.co/t/aggregation-on-terms-and-find-min-value-of-script-results/240214/2 "2020-07-08T00:27:31Z")

</div>

Need help. Hope someone can share some ideas or hints.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [July 8, 2020, 12:47am UTC](https://discuss.elastic.co/t/aggregation-on-terms-and-find-min-value-of-script-results/240214/3 "2020-07-08T00:47:25Z")

</div>

I might a scripted metric under the terms and sorting the terms on that. I _think_ that works. Scripted metric is always a bit fiddly and slow but it can push the math to the shards and stop you from having to pull everything back to the coordinating node.

---

<div class="post-metadata">

**Author:** ![Akihito\_Kanbara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akihito_kanbara/32/71754_2.png) [@Akihito\_Kanbara](https://discuss.elastic.co/u/Akihito_Kanbara)\
**Post date:** [July 8, 2020, 12:54am UTC](https://discuss.elastic.co/t/aggregation-on-terms-and-find-min-value-of-script-results/240214/4 "2020-07-08T00:54:45Z")

</div>

Thanks for reply. Are you suggesting use Scripted Metric Aggregation to directly do the calculation and for a term, here for a user. And then do Min Aggregation or Sorting based on that?  
Can I have more details or some sample code?

---

<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:** [August 5, 2020, 12:54am UTC](https://discuss.elastic.co/t/aggregation-on-terms-and-find-min-value-of-script-results/240214/5 "2020-08-05T00:54:49Z")

</div>

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