# Aggregating child documents in has\_child query

**URL:** <https://discuss.elastic.co/t/aggregating-child-documents-in-has-child-query/38819>\
**Category:** Elasticsearch\
**Created:** [January 10, 2016, 7:40pm UTC](https://discuss.elastic.co/t/aggregating-child-documents-in-has-child-query/38819 "2016-01-10T19:40:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sundar\_V](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sundar_v/32/6928_2.png) [@Sundar\_V](https://discuss.elastic.co/u/Sundar_V)\
**Post date:** [January 10, 2016, 7:40pm UTC](https://discuss.elastic.co/t/aggregating-child-documents-in-has-child-query/38819/1 "2016-01-10T19:40:10Z")

</div>

I would like to aggregate data from child documents in has\_child query as well. The reason is, my child documents have the timestamp and when I aggregate the parent documents, I want to the first occurance and the last occurance of the parent in the child documents, within the time range that I query.

I found a [this question](https://discuss.elastic.co/t/aggregating-data-from-child-documents-in-has-child-query/3935) already posted, but in 2011. Is there any new recommended method to do this?

One option I’m using now is to get the "field":"date" in the inner\_hits and then sort it, with a size of 1.

```
      "inner_hits": {
        "fields": "date",
        "name": "date_sort",
        "size": 1,
        "sort": [
          {
            "date": {
              "order": "desc"
            }
          }
        ]
      },

```

By doing so, I’m able to get the last occurance, but not the first occurance.

Two questions:

1. What I’m doing, is that the best method, if I have to use only inner\_hits?
2. Is there any other better method, other than inner\_hits

---

<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:** [July 5, 2017, 11:25pm UTC](https://discuss.elastic.co/t/aggregating-child-documents-in-has-child-query/38819/2 "2017-07-05T23:25:39Z")

</div>


