# How to use total hit count in bucket\_script aggregation under the terms aggregation?

**URL:** <https://discuss.elastic.co/t/how-to-use-total-hit-count-in-bucket-script-aggregation-under-the-terms-aggregation/183268>\
**Category:** Elasticsearch\
**Created:** [May 29, 2019, 8:05am UTC](https://discuss.elastic.co/t/how-to-use-total-hit-count-in-bucket-script-aggregation-under-the-terms-aggregation/183268 "2019-05-29T08:05:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![murat](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/murat/32/47049_2.png) [@murat](https://discuss.elastic.co/u/murat)\
**Post date:** [May 29, 2019, 8:05am UTC](https://discuss.elastic.co/t/how-to-use-total-hit-count-in-bucket-script-aggregation-under-the-terms-aggregation/183268/1 "2019-05-29T08:05:12Z")

</div>

I'm trying to use total hit count with the bucket\_script aggregation as sub aggregation of terms aggregation. Any suggestions are welcome.

I also tried value\_count and terms aggregation on the package id field. But it also didn't work.

Mapping:

```bash
PUT /packages/_mapping/_doc
{
  "properties": {
    "items": {
      "type": "nested"
    }
  }
}

```

Example document:

```json
{
  "id": 1,
  "name": "Lorem",
  "items": [
    {
      "infos": {
        "available": true
      },
      "item": {
        "id": 1,
        "meta": false,
        "name": "Ipsum"
      }
    },
    {
      "infos": {
        "available": false
      },
      "item": {
        "id": 2,
        "meta": false,
        "name": "Ipsum 2"
      }
    },
    {
      "infos": {
        "available": false
      },
      "item": {
        "id": 3,
        "meta": false,
        "name": "Ipsum 3"
      }
    }
  ]
}

```

My aggregation query:

```bash
GET /packages/_search

{
  "query": { "match_all": {} },
  "size": 0,
  "aggs": {
    "items": {
      "nested": {
        "path": "items"
      },
      "aggs": {
        "non_meta": {
          "filter": {
            "term": {
              "items.item.meta": false
            }
          },
          "aggs": {
            "item": {
              "terms": {
                "field": "items.item.id"
              },
              "aggs": {
                "total_count": { <--------------------- I want to get total document count here, but I can only reach `doc_count` for docs that includes items
                  "reverse_nested": {}
                },
                "available_count": {
                  "sum": {
                    "script": {
                      "source": "doc['items.infos.available'].value == true ? 1 : 0"
                    }
                  }
                },
                "penetration": {
                  "bucket_script": {
                    "buckets_path": {
                      "available": "available_count",
                      "total": "total_count>_count"
                    },
                    "script": "params.available / params.total * 100"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

---

<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:** [June 26, 2019, 8:05am UTC](https://discuss.elastic.co/t/how-to-use-total-hit-count-in-bucket-script-aggregation-under-the-terms-aggregation/183268/2 "2019-06-26T08:05:17Z")

</div>

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