# Combining nested and non nested aggregations

**URL:** <https://discuss.elastic.co/t/combining-nested-and-non-nested-aggregations/82583>\
**Category:** Elasticsearch\
**Created:** [April 17, 2017, 2:20pm UTC](https://discuss.elastic.co/t/combining-nested-and-non-nested-aggregations/82583 "2017-04-17T14:20:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ervin\_Puskar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ervin_puskar/32/17418_2.png) [@Ervin\_Puskar](https://discuss.elastic.co/u/Ervin_Puskar)\
**Post date:** [April 17, 2017, 2:20pm UTC](https://discuss.elastic.co/t/combining-nested-and-non-nested-aggregations/82583/1 "2017-04-17T14:20:16Z")

</div>

Not sure if this is the right place to post but here goes...

How do I create a bucket aggregation (in this case terms aggregations), that keys off of a nested field AND a non nested field. I would like to the results to be a single bucketaggregation (with other sub aggregations included).

Example mapping:

```
"mappings": {
  "test": {
    "dynamic": "false",
    "properties": {
      "foo": {
        "dynamic": "false",
        "properties": {
          "bar": {
            "dynamic": "false",
            "properties": {
              "id": {
                "type": "long"
              }
            }
          },
          "nesty": {
            "type": "nested",
            "properties": {
              "bar": {
                "dynamic": "false",
                "properties": {
                  "id": {
                    "type": "long"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

Example documents

```
{ "foo":
  { "nesty": [
    { "bar":{"id":1}},
    { "bar":{"id":2}}
    ]
  }
}

{ "foo": { "bar" : {"id": 1} } }

```

What I expect back:

```
  "aggregations" : {
      "blah" : {  
          "buckets" : [ 
            {
                "key" : "1",
                "doc_count" : 2,
                ...
            },
            {
                "key" : "2",
                "doc_count" : 1,
                ...
            }
          ]
      }
  }

```

Here's what I have tried so far

```
GET _search
{
  "size": 0,
  "aggs": {
    "a": {
      "nested": {
        "path": "foo.nesty"
      },
      "aggs": {
        "a": {
          "terms": {
            "script": "if (doc['foo.bar.id'].value != 0) {return doc['foo.bar.id'].value} return doc['foo.nesty.bar.id'].value"
          }
        }
      }
    },
    "b": {
      "terms": {
        "script": {
          "inline": "if (doc['foo.bar.id'].value != 0) {return doc['foo.bar.id'].value} return doc['foo.nesty.bar.id'].value"
        }
      }
    }
  }
}

```

There doesn't seem to be a sibling pipeline aggregation that lets me combine the buckets from two aggregations either, so if I had two terms aggregations (one nested and one not) I can't seem to combine the buckets they return.

**Is there any way to access both a nested field and a non nested field in a script?**

---

<div class="post-metadata">

**Author:** ![Glen\_Smith](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/glen_smith/32/111656_2.png) [@Glen\_Smith](https://discuss.elastic.co/u/Glen_Smith)\
**Post date:** [April 18, 2017, 12:18am UTC](https://discuss.elastic.co/t/combining-nested-and-non-nested-aggregations/82583/2 "2017-04-18T00:18:50Z")

</div>

I think a better strategy would be to enable this at index-time:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html)

If you copy **[bar.id](http://bar.id)** and **[nesty.bar.id](http://nesty.bar.id)** to a common field, you can simply bucket on that field.

---

<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:** [May 16, 2017, 12:30am UTC](https://discuss.elastic.co/t/combining-nested-and-non-nested-aggregations/82583/3 "2017-05-16T00:30:02Z")

</div>

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