# These two nested aggregations produce the same result, but is one approach better than the other?

**URL:** <https://discuss.elastic.co/t/these-two-nested-aggregations-produce-the-same-result-but-is-one-approach-better-than-the-other/324941>\
**Category:** Elasticsearch\
**Created:** [February 7, 2023, 9:59pm UTC](https://discuss.elastic.co/t/these-two-nested-aggregations-produce-the-same-result-but-is-one-approach-better-than-the-other/324941 "2023-02-07T21:59:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![learningelastic](https://avatars.discourse-cdn.com/v4/letter/l/958977/32.png) [@learningelastic](https://discuss.elastic.co/u/learningelastic)\
**Post date:** [February 7, 2023, 9:59pm UTC](https://discuss.elastic.co/t/these-two-nested-aggregations-produce-the-same-result-but-is-one-approach-better-than-the-other/324941/1 "2023-02-07T21:59:00Z")

</div>

I have two scenarios that produce identical aggregation results in the final `GET order/_search` query. The only difference between the two scenarios is where I choose to declare the `type: nested` in the mapping and what I specify in the `nested.path: ` in the query.

**SCENARIO 1 - Declare nested for order\_items**

```auto
PUT order

POST order/_mapping
{
  "properties": {
    "order_items": {
      "type": "nested",
      "properties": {
        "product_id": {
          "type": "long"
        },
        "product": {
          "properties": {
            "name": {
              "type": "keyword"
            },
            "product_id": {
              "type": "long"
            },
            "price": {
              "type": "long"
            }
          }
        }
      }
    }
  }
}

POST order/_bulk
{"index":{}}
{"order_items":[{"product":{"name":"book","price":10}},{"product":{"name":"pencil","price":1}}]}
{"index":{}}
{"order_items":[{"product":{"name":"pen","price":5}},{"product":{"name":"eraser","price":1}}]}

GET order/_search
{
  "size": 0,
  "aggs": {
    "order": {
      "nested": {
        "path": "order_items"
      },
      "aggs": {
        "product_name" : {
          "terms": {
            "field": "order_items.product.name"
          },
          "aggs": {
            "avg_price": {
              "avg": {
                "field": "order_items.product.price"
              }
            }
          }
        }
      }
    }
  }
}

```

**SCENARIO 2 - Declare nested for product**

```auto
PUT order

POST order/_mapping
{
  "properties": {
    "order_items": {
      "properties": {
        "product_id": {
          "type": "long"
        },
        "product": {
          "type": "nested",
          "properties": {
            "name": {
              "type": "keyword"
            },
            "product_id": {
              "type": "long"
            },
            "price": {
              "type": "long"
            }
          }
        }
      }
    }
  }
}

POST order/_bulk
{"index":{}}
{"order_items":[{"product":{"name":"book","price":10}},{"product":{"name":"pencil","price":1}}]}
{"index":{}}
{"order_items":[{"product":{"name":"pen","price":5}},{"product":{"name":"eraser","price":1}}]}

GET order/_search
{
  "size": 0,
  "aggs": {
    "order": {
      "nested": {
        "path": "order_items.product"
      },
      "aggs": {
        "product_name" : {
          "terms": {
            "field": "order_items.product.name"
          },
          "aggs": {
            "avg_price": {
              "avg": {
                "field": "order_items.product.price"
              }
            }
          }
        }
      }
    }
  }
}

```

My question is whether one approach is better than the other? Does this have anything to do with field map explosions or other performance concerns? Are there other things I need to be aware of between these two scenarios?

---

<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:** [March 7, 2023, 9:59pm UTC](https://discuss.elastic.co/t/these-two-nested-aggregations-produce-the-same-result-but-is-one-approach-better-than-the-other/324941/2 "2023-03-07T21:59:42Z")

</div>

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