# Calculating percent

**URL:** <https://discuss.elastic.co/t/calculating-percent/293688>\
**Category:** Elasticsearch\
**Created:** [January 6, 2022, 8:02pm UTC](https://discuss.elastic.co/t/calculating-percent/293688 "2022-01-06T20:02:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nabil86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nabil86/32/99932_2.png) [@nabil86](https://discuss.elastic.co/u/nabil86)\
**Post date:** [January 6, 2022, 8:02pm UTC](https://discuss.elastic.co/t/calculating-percent/293688/1 "2022-01-06T20:02:32Z")

</div>

Hello,  
I want to calculte the percent of a given type on my index: Ok\_Count/total , I wrote this query  
But it doesn't work as i excepted , could you help me to correct it

```auto
{
  
  "aggs": {
    "total":{
      "filter": {
        "bool": {
            "should": [{
              "term": {"FieldPresentOnAllDocument": "F"}
              }]
          }
        }
      },
    "OK": {
      "filter": {
        "bool": {
          "must": {
            "bool": {
              "should": [
                {
                  "bool": {
                    "must": {
                      "range": {
                        "MyField": {
                          "gte": 100,
                          "lte": 399
                        }
                      }
                    }
                  }
                },
                {
                  "bool": {
                    "should": [
                      {
                        "term": {"MyField": "0"}
                      },
                      {
                        "term": {"MyField": "5"}
                      },
                      {
                        "term": {"MyField": "6"}
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    }                
  },
  "size":0
}

```

result is :

```auto
{
  "took" : 1101,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : []
  },
  "aggregations" : {
    "Ok" : {
      "doc_count" : 5342374
    },
    "total" : {
      "MyField" : 0
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 7, 2022, 2:20am UTC](https://discuss.elastic.co/t/calculating-percent/293688/2 "2022-01-07T02:20:57Z")

</div>

Though I'm not sure your overall picture, you can match all document with "match all query".

> **[Match all query | Elasticsearch Guide \[7.16\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html)**

The following might not be what you need, you may also be able calculate the perecent itself by adding bucket script aggregation in a single query.

> **[Bucket script aggregation | Elasticsearch Guide \[7.16\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html)**

---

<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 4, 2022, 2:21am UTC](https://discuss.elastic.co/t/calculating-percent/293688/3 "2022-02-04T02:21:45Z")

</div>

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