# Schema design and query - aggregation within nested document

**URL:** https://discuss.elastic.co/t/schema-design-and-query-aggregation-within-nested-document/266629
**Category:** Elasticsearch
**Created:** [March 9, 2021, 4:41am UTC](https://discuss.elastic.co/t/schema-design-and-query-aggregation-within-nested-document/266629 "2021-03-09T04:41:16Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![cuongthamhl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cuongthamhl/32/85200_2.png) [@cuongthamhl](https://discuss.elastic.co/u/cuongthamhl)
#### Post date: [March 9, 2021, 4:41am UTC](https://discuss.elastic.co/t/schema-design-and-query-aggregation-within-nested-document/266629/1 "2021-03-09T04:41:16Z")

</div>

I have a simple schema with a nested field. A sample product document look like this:

```auto
{
  "name": "product name",
  "events": [
    {
      "startDate": "2021-01-01 00:00:00",
      "endDate": "2021-01-01 00:00:00",
      "price": 1.5
    },
    {
      "startDate": "2021-02-01 00:00:00",
      "endDate": "2021-02-01 00:00:00",
      "price": 2.5
    },
   // more events ...
  ]
}

```

The "events" field is a nested document type. Let's say we have 10 millions documents in the Elasticsearch database, and I want to run aggregation only for the events occur on 2021-01-05. How would one write such query?

For clarification: the ask is to aggregate prices among all the products where events occur on 2021-01-05.

I googled "inner hits aggregation." Apparently "inner hits" does support aggregation and it's slow.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [March 9, 2021, 8:14am UTC](https://discuss.elastic.co/t/schema-design-and-query-aggregation-within-nested-document/266629/2 "2021-03-09T08:14:32Z")

</div>

Why not indexing individual events instead of updating everytime the same document?

---

<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: [April 6, 2021, 8:14am UTC](https://discuss.elastic.co/t/schema-design-and-query-aggregation-within-nested-document/266629/3 "2021-04-06T08:14:40Z")

</div>

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