# How to count of nested inner\_hits documents?

**URL:** <https://discuss.elastic.co/t/how-to-count-of-nested-inner-hits-documents/139615>\
**Category:** Elasticsearch\
**Created:** [July 11, 2018, 3:46pm UTC](https://discuss.elastic.co/t/how-to-count-of-nested-inner-hits-documents/139615 "2018-07-11T15:46:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Approach](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/approach/32/33248_2.png) [@Approach](https://discuss.elastic.co/u/Approach)\
**Post date:** [July 11, 2018, 3:46pm UTC](https://discuss.elastic.co/t/how-to-count-of-nested-inner-hits-documents/139615/1 "2018-07-11T15:46:23Z")

</div>

Hi@all

i have many companies and each company has many products. But how i get the count of all products.

Query:

```
  GET /company/_search
    {
      "from": 0,
      "size": 5,
      "_source": false,
      "query": {
        "nested": {
          "path": "products",
          "inner_hits": {
            "_source": false
          },
          "query": {
            "match": {
              "products.category.id": 3
            }
          }
        }
      },
      "aggs": {
        "products": {
          "nested": {
            "path": "products"
          },
          "aggs": {
            "count": {
              "value_count": {
                "field": "products.id"
              }
            }
          }
        }
      }
    }

```

The result looks like:

```
  "aggregations": {
    "products": {
      "doc_count": 121,
      "count": {
        "value": 121
      }
    }
  }

```

This value is wrong. Me need Elasticsearch to tell, count please from **inner\_hits**. But how?

Anybody a idea?

---

<div class="post-metadata">

**Author:** ![Approach](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/approach/32/33248_2.png) [@Approach](https://discuss.elastic.co/u/Approach)\
**Post date:** [July 13, 2018, 10:51am UTC](https://discuss.elastic.co/t/how-to-count-of-nested-inner-hits-documents/139615/2 "2018-07-13T10:51:42Z")

</div>

Solution found...

```
 "aggs": {
"products": {
  "nested": {
    "path": "products"
  },
  "aggs": {
    "inner": {
      "filter": {
        "bool": {
          "must": [
            {
              "match": {
                "products.category.id": 6
              }
            }
          ]
        }
      },
      "aggs": {
        "agg_terms_products.country": {
          "terms": {
            "field": "products.country",
            "order": {
              "_count": "desc"
            },
            "size": 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:** [August 10, 2018, 10:51am UTC](https://discuss.elastic.co/t/how-to-count-of-nested-inner-hits-documents/139615/3 "2018-08-10T10:51:54Z")

</div>

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