# How to get average of items in an array of a nested field?

**URL:** <https://discuss.elastic.co/t/how-to-get-average-of-items-in-an-array-of-a-nested-field/322690>\
**Category:** Elasticsearch\
**Created:** [January 8, 2023, 9:34pm UTC](https://discuss.elastic.co/t/how-to-get-average-of-items-in-an-array-of-a-nested-field/322690 "2023-01-08T21:34:51Z")\
**Posts on this page:** 6\
**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:** [January 8, 2023, 9:34pm UTC](https://discuss.elastic.co/t/how-to-get-average-of-items-in-an-array-of-a-nested-field/322690/1 "2023-01-08T21:34:51Z")

</div>

I'm trying to get the average price of products that are in stock across my different physical warehouses. I made this index:

```auto
PUT warehouse
POST warehouse/_mapping
{
"properties": {
      "inventory": {
        "properties": {
          "equipment": {
            "type": "keyword"
          },
          "price": {
            "type": "float"
          }
        }
      },
      "profile": {
        "properties": {
          "name": {
            "type": "keyword"
          }
        }
      }
    }
}

```

Then I inserted 3 documents each with the name of a warehouse and some inventory

```auto
POST warehouse/_doc
{
  "profile": {
    "name": "Texas"
  },
  "inventory": [
    {"equipment":"guitar", "price": 20.00},
    {"equipment":"book", "price": 25.00}
  ]
}

POST warehouse/_doc
{
  "profile": {
    "name": "Ohio"
  },
  "inventory": [
    {"equipment":"piano", "price": 20.00},
    {"equipment":"book", "price": 25.00}
  ]
}

POST warehouse/_doc
{
  "profile": {
    "name": "New Mexico"
  },
  "inventory": [
    {"equipment":"guitar", "price": 20.00},
    {"equipment":"book", "price": 25.00},
    {"equipment":"laptop", "price": 250.00}
  ]
}

```

Then I ran this query to get the average price of each equipment I have:

```auto
GET warehouse/_search
{
  "aggs": {
    "0": {
      "terms": {
        "field": "inventory.equipment"
      },
      "aggs": {
        "1": {
          "avg": {
            "field": "inventory.price"
          }
        }
      }
    }
 }
}

```

And this was the aggregation response:

```auto
  "aggregations": {
    "0": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "1": {
            "value": 55
          },
          "key": "book",
          "doc_count": 3
        },
        {
          "1": {
            "value": 68
          },
          "key": "guitar",
          "doc_count": 2
        },
        {
          "1": {
            "value": 98.33333333333333
          },
          "key": "laptop",
          "doc_count": 1
        },
        {
          "1": {
            "value": 22.5
          },
          "key": "piano",
          "doc_count": 1
        }
      ]
    }
  }
}

```

These results don't look correct because I was expecting piano to be 20, guitar to be 20, book to be 25, and laptop to be 250.

What did I do wrong?

---

<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:** [January 8, 2023, 10:49pm UTC](https://discuss.elastic.co/t/how-to-get-average-of-items-in-an-array-of-a-nested-field/322690/2 "2023-01-08T22:49:30Z")

</div>

Have a look at nested type. Here your array is flattened by default.

---

<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:** [January 8, 2023, 11:44pm UTC](https://discuss.elastic.co/t/how-to-get-average-of-items-in-an-array-of-a-nested-field/322690/4 "2023-01-08T23:44:03Z")

</div>

Thanks for suggestion! My question is based on the concept of nested field type that I read on this page:

> **[Nested field type | Elasticsearch Guide \[8.5\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html)**

I also tried adding the property `properties.inventory.type: "nested"` to my mappings so that it looks like this:

```auto
POST warehouse/_mapping
{
  "properties": {
    "inventory": {
      "type": "nested",
      "properties": {
        "equipment": {
          "type": "keyword"
        },
        "price": {
          "type": "float"
        }
      }
    },
    "profile": {
      "properties": {
        "name": {
          "type": "keyword"
        }
      }
    }
  }
}

```

But now my search query returns nothing in the aggregations as shown here:

```auto
{
  "aggregations": {
    "0": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": []
    }
  }
}

```

Any ideas what I'm missing next?

---

<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:** [January 8, 2023, 11:54pm UTC](https://discuss.elastic.co/t/how-to-get-average-of-items-in-an-array-of-a-nested-field/322690/5 "2023-01-08T23:54:01Z")

</div>

Maybe I figured it out. I also changed my search query by wrapping it with another level of aggregation to mention a nested path like this:

```auto
GET warehouse/_search
{
  "aggs": {
    "inventory": {
      "nested": {
        "path": "inventory"
      },
      "aggs": {
        "0": {
          "terms": {
            "field": "inventory.equipment"
          },
          "aggs": {
            "1": {
              "avg": {
                "field": "inventory.price"
              }
            }
          }
        }
      }
    }
  }
}

```

This seems to give the correct results so far, but need more testing to confirm

```auto
{
"aggregations": {
    "inventory": {
      "0": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "1": {
              "value": 25
            },
            "key": "book",
            "doc_count": 3
          },
          {
            "1": {
              "value": 20
            },
            "key": "guitar",
            "doc_count": 2
          },
          {
            "1": {
              "value": 250
            },
            "key": "laptop",
            "doc_count": 1
          },
          {
            "1": {
              "value": 20
            },
            "key": "piano",
            "doc_count": 1
          }
        ]
      },
      "doc_count": 7
    }
  }
}

```

---

<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:** [January 9, 2023, 12:32am UTC](https://discuss.elastic.co/t/how-to-get-average-of-items-in-an-array-of-a-nested-field/322690/6 "2023-01-09T00:32:28Z")

</div>

And a related problem...by adding `properties.inventory.type: "nested"` to the mapping, kibana lens can no longer access any of the `inventory.*` fields. I posted the problem as a separate question here:

> [@Kibana Visualize Lens not showing nested fields](https://discuss.elastic.co/t/kibana-visualize-lens-not-showing-nested-fields/322691):
>
> I'm not sure what I did wrong, but Kibana Lenses aren't showing me the nested field inventory.equipment. In terms of what I did, I set up an index and added a document with this script: PUT warehouse POST warehouse/\_mapping { "properties": { "inventory": { "type": "nested", "properties": { "equipment": { "type": "keyword" }, "price": { "type": "float" } } }, "profile": { "properties": { "name": { …

---

<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:** [February 6, 2023, 12:32am UTC](https://discuss.elastic.co/t/how-to-get-average-of-items-in-an-array-of-a-nested-field/322690/7 "2023-02-06T00:32:31Z")

</div>

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