# Aggregation with nested fields. Newbie problem

**URL:** <https://discuss.elastic.co/t/aggregation-with-nested-fields-newbie-problem/159828>\
**Category:** Elasticsearch\
**Created:** [December 7, 2018, 12:55am UTC](https://discuss.elastic.co/t/aggregation-with-nested-fields-newbie-problem/159828 "2018-12-07T00:55:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rxtx](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@rxtx](https://discuss.elastic.co/u/rxtx)\
**Post date:** [December 7, 2018, 12:55am UTC](https://discuss.elastic.co/t/aggregation-with-nested-fields-newbie-problem/159828/1 "2018-12-07T00:55:44Z")

</div>

Hi,

I'm still quite new to elasticsearch and have found myself needing to use a nested index. I've tried to distill the problem into something simple that I could post here.

This comes from a database where each record has anywhere from 1 to a few hundred children. Each child has a unique name that comes from a pool of about 3000+ different names.

Mapping (from kibana console)

```
PUT test_nest
{
    "mappings": {
        "_doc": {
            "dynamic": "false",
            "properties": {
                "full_name": {
                    "type": "text"
                },
                "v": {
                    "type": "nested",
                    "properties": {
                        "name": {
                            "type": "keyword"
                        },
                        "value": {
                            "type": "float"
                        }
                    }
                }
            }
        }
    }
}

```

Sample data

```
 POST _bulk
{"index": {"_index": "test_nest", "_type": "_doc", "_id": "1"}}
{"full_name": "Three", "v": [{"name": "test_vol", "value": 3}, {"name": "duration", "value": 30}]}
{"index": {"_index": "test_nest", "_type": "_doc", "_id": "2"}}
{"full_name": "One", "v": [{"name": "test_vol", "value": 1}, {"name": "duration", "value": 10}]}
{"index": {"_index": "test_nest", "_type": "_doc", "_id": "3"}}
{"full_name": "Two", "v": [{"name": "test_vol", "value": 2}, {"name": "duration", "value": 20}]}

```

I'm trying to get an average value of all the **v.name** == **test\_vol**. The query I use below does not work since it averages all the **v.value** entries.

I'm looking on how to limit the scope of the **v.value** entries to only those matching **test\_vol**. Sorry if this is a newbie question but I'm at my wits end.

Aggregation Query

```
GET test_nest/_search
{
    "size": 0,
    "query": {
        "nested": {
            "path": "v",
            "query": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "v.name": "test_vol"
                            }
                        }
                        
                    ]
                }
            }
        }
    },
    "aggs": {
        "avg_test_vol": {
            "nested": {
                "path": "v"
            },
            "aggs": {
                "thing": {
                    "avg": {
                        "field": "v.value"
                    }
                }
            }
        }
    }
}

```

Results of query. I'd want to get an average of **2**. Instead I get **11**.

```
{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "avg_test_vol": {
            "doc_count": 6,
            "thing": {
                "value": 11
            }
        }
    }
}

```

Thanks in advance for any hints on how to scope the documents used for the aggregation,

Rick

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [December 7, 2018, 9:24am UTC](https://discuss.elastic.co/t/aggregation-with-nested-fields-newbie-problem/159828/2 "2018-12-07T09:24:03Z")

</div>

try

```auto
GET test_nest/_search
{
  "size": 0,
  "query": {
    "nested": {
      "path": "v",
      "query": {
        "match": {
          "v.name": "test_vol"
        }
      }
    }
  },
  "aggs": {
    "avg_test_vol": {
      "nested": {
        "path": "v"
      },
      "aggs": {
        "inner": {
          "filter": {
            "match": {
              "v.name": "test_vol"
            }
          }, 
          "aggs": {
            "thing": {
              "avg": {
                "field": "v.value"
              }
            }
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![rxtx](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@rxtx](https://discuss.elastic.co/u/rxtx)\
**Post date:** [December 7, 2018, 1:16pm UTC](https://discuss.elastic.co/t/aggregation-with-nested-fields-newbie-problem/159828/3 "2018-12-07T13:16:19Z")

</div>

Perfect. Thank you very much Alexander!

---

<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:** [January 4, 2019, 1:23pm UTC](https://discuss.elastic.co/t/aggregation-with-nested-fields-newbie-problem/159828/4 "2019-01-04T13:23:08Z")

</div>

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