# Elastic search nested aggrigation

**URL:** <https://discuss.elastic.co/t/elastic-search-nested-aggrigation/371709>\
**Category:** Elasticsearch\
**Tags:** aggregations, esql\
**Created:** [December 9, 2024, 2:28pm UTC](https://discuss.elastic.co/t/elastic-search-nested-aggrigation/371709 "2024-12-09T14:28:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhay\_Pratap\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhay_pratap_singh/32/139824_2.png) [@Abhay\_Pratap\_Singh](https://discuss.elastic.co/u/Abhay_Pratap_Singh)\
**Post date:** [December 9, 2024, 2:28pm UTC](https://discuss.elastic.co/t/elastic-search-nested-aggrigation/371709/1 "2024-12-09T14:28:23Z")

</div>

I am try to get final price of each product of orders bu not able to calculate.  
The Document sample :

```auto
Order Index :
-------------
{
  "order_id": "123",
  "customer": "John Doe",
  "deliveryFee": 40,
      "items": [
      {
          "product": "Laptop",
          "price": 1200,
          "quantity": 1
      },
      {
          "product": "Mouse",
          "price": 25,
          "quantity": 1
      }
  ]
}

```

The Response i am expecting (product wise Total price like below ) :

```auto
{
  "product":{
    buckets:[
      {
        "key":Laptop,
        "Price" : 1240 //(price + deliveryfee)*quantity
      },
      {
        "key":Mouse,
        "Price" : 65 //(price + deliveryfee)*quantity
      }
    ]
  }
}

```

The Query :

```auto

{
  "aggs": {
    "AllProductPrice": {
      "nested": {
        "path": "items"
      },
      "aggs": {
        "product":{
          "term":{
            "field": "items.product.keyword",
            "size": 10
          }
        },
        "aggs":{
          "totalProductPrice": {
            "sum": {
              "script": {
                "source":"return (doc['deliveryFee'].value * doc['item.Price']) * doc['item.quantity']"
              }
            }
          }
        }
      }
    }
  }
}

```

The Main challanges is we are not able to get doc['deliveryFee'].value . Because we have used nested path aggrigation. So i used back reverse nested But its also not working.2024-12-08T18:30:00Z
