# Sum aggregation on nested query

**URL:** https://discuss.elastic.co/t/sum-aggregation-on-nested-query/149093
**Category:** Elasticsearch
**Created:** [September 19, 2018, 9:37am UTC](https://discuss.elastic.co/t/sum-aggregation-on-nested-query/149093 "2018-09-19T09:37:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![kardagan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kardagan/32/35661_2.png) [@kardagan](https://discuss.elastic.co/u/kardagan)
#### Post date: [September 19, 2018, 9:37am UTC](https://discuss.elastic.co/t/sum-aggregation-on-nested-query/149093/1 "2018-09-19T09:37:48Z")

</div>

Hello,

In my database I have products, each products have differents offers and offers have sales.  
A sale is defined by a sale datetime and a nb (number of sales for de date)

I have to get produts and offers who have sales on a period, for each offer get the sum of sale for the period and car sort on this sales sum.

so I've defined in mapping offers.sales as nested object and for the moment i've have this query :

```
{
  "query": {
    "bool": {
      "must": {
        "nested": {
          "path": "offers.sales",
          "query": {
            "range": {
              "offers.sales.saleDate": {
                "from": "2018-08-01"
              }
            }
          },
          "inner_hits": {}
        }
      }
    }
  },
  "sort": [
    {
      "offers.sales.nb": {
        "order": "desc",
        "mode": "sum",
        "nested_filter": {
          "range": {
            "offers.sales.saleDate": {
              "gte": "2018-08-01"
            }
          }
        }
      }
    }
  ]
}

```

int inner\_hits I have filtered sales, but I don't know how to sum them because aggs are not able in nested query...

someone could give me a way to get my result please ?

---

<div class="post-metadata">

### Author: ![kardagan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kardagan/32/35661_2.png) [@kardagan](https://discuss.elastic.co/u/kardagan)
#### Post date: [September 20, 2018, 2:00pm UTC](https://discuss.elastic.co/t/sum-aggregation-on-nested-query/149093/2 "2018-09-20T14:00:30Z")

</div>

I've found the solution, i give it if someone need it :

```
POST /ogo-test/item/_search
{
  "query": {
    "bool": {
      "must": {
        "nested": {
          "path": "offers.sales",
          "query": {
            "range": {
              "offers.sales.saleDate": {
                "gte": 1533081600
              }
            }
          }
        }
      }
    }
  },
  "aggs": {
    "offers_sales": {
      "nested": {
        "path": "offers.sales"
      },
      "aggs": {
        "offers": {
          "filter": {
            "range": {
              "offers.sales.saleDate": {
                "gte": 1533081600
              }
            }
          },
          "aggs": {
            "offers": {
              "terms": {
                "field": "offers.sales.offerId"
              },
              "aggs": {
                "period_sale_number": {
                  "sum": {
                    "field": "offers.sales.nb"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "sort": {
    "offers.sales.nb": {
      "mode": "sum",
      "order": "asc",
      "nested": {
        "path": "offers.sales",
        "filter": {
          "range": {
            "offers.sales.saleDate": {
              "gte": 1533081600
            }
          }
        }
      }
    }
  }
}
```

---

<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: [October 18, 2018, 2:00pm UTC](https://discuss.elastic.co/t/sum-aggregation-on-nested-query/149093/3 "2018-10-18T14:00:31Z")

</div>

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