# Elasticsearch Engineer I - aggregations problem/question

**URL:** https://discuss.elastic.co/t/elasticsearch-engineer-i-aggregations-problem-question/219335
**Category:** Elastic Training
**Created:** [February 14, 2020, 9:03am UTC](https://discuss.elastic.co/t/elasticsearch-engineer-i-aggregations-problem-question/219335 "2020-02-14T09:03:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dyjo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dyjo/32/62691_2.png) [@dyjo](https://discuss.elastic.co/u/dyjo)
#### Post date: [February 14, 2020, 9:03am UTC](https://discuss.elastic.co/t/elasticsearch-engineer-i-aggregations-problem-question/219335/1 "2020-02-14T09:03:20Z")

</div>

During exam preparation I run across the following:

I wanted to see requests per country:

```
GET logs_server*/_search
{
  "size": 0,
  "aggs": {
    "top_countries": {
      "terms": {
        "field": "geoip.country_name.keyword",
        "size": 400
      }
    }
  }
}

```

which worked as expected.  
And now I want to get/calculate the percentages per country so I tried:

```
GET logs_server*/_search
{
  "size": 0,
  "aggs": {
    "top_countries": {
      "significant_terms": {
        "field": "geoip.country_name.keyword",
        "percentage": {},
        "size": 400
      }
    }
  }
}

```

Gets me all the numbers but does not calculate percentages. Next try:

```
GET logs_server*/_search
{
  "size": 0,
  "aggs": {
    "all_countries": {
      "value_count": {
        "field": "geoip.country_name.keyword"
      }
    },
    "by_countries": {
      "terms": {
        "field": "geoip.country_name.keyword",
        "size": 400
      },
      "aggs": {
        "percentage": {
          "bucket_script": {
            "buckets_path": {
              "all": "all_countries",
              "country": "by_countries"
            },
            "script": "country /all * 100"
          }
        }
      }
    }
  }
}

```

Produces:

```
"reason" : "No aggregation found for path [all_countries]"

```

So, how could I calculate the requests (doc\_count) percentage per country?

---

<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: [February 14, 2020, 10:50am UTC](https://discuss.elastic.co/t/elasticsearch-engineer-i-aggregations-problem-question/219335/2 "2020-02-14T10:50:18Z")

</div>

Hey @dyjo

I don't think this can be done with a pipeline aggregation. You'd have to compute the percentages on the client side.

By the way, pipeline aggregations are not currently listed on the [Elastic Certified Engineer exam objectives](https://training.elastic.co/exam/elastic-certified-engineer#objectives), so I would not worry about getting a question at this level on your exam.

---

<div class="post-metadata">

### Author: ![pmusa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pmusa/32/3498_2.png) [@pmusa](https://discuss.elastic.co/u/pmusa)
#### Post date: [February 14, 2020, 11:04am UTC](https://discuss.elastic.co/t/elasticsearch-engineer-i-aggregations-problem-question/219335/3 "2020-02-14T11:04:54Z")

</div>

If you don't want to calculate it on the client side, you could solve it with 2 requests. The main issue is that documents can be indexed in between the 2 calls, generating results that are not 100% precise.

```auto
# the result here is 1751476
GET logs_server*/_count

# add the above result to the params
GET logs_server*/_search
{
  "size": 0,
  "aggs": {
    "top_countries": {
      "terms": {
        "field": "geoip.country_name.keyword",
        "size": 400
      },
      "aggs": {
        "percentage": {
          "bucket_script": {
            "buckets_path": {
              "count": "_count"
            },
            "script": {
              "source": "(params.count / params.total) * 100",
              "params": {
                "total": 1751476
              }
            }
          }
        }
      }
    }
  }
}

```

---

<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: [March 15, 2020, 11:07am UTC](https://discuss.elastic.co/t/elasticsearch-engineer-i-aggregations-problem-question/219335/4 "2020-03-15T11:07:34Z")

</div>

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