# Elasticsearch aggregation doesn't work with nested-type fields

**URL:** <https://discuss.elastic.co/t/elasticsearch-aggregation-doesnt-work-with-nested-type-fields/28476>\
**Category:** Elasticsearch\
**Created:** [September 1, 2015, 11:34pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-doesnt-work-with-nested-type-fields/28476 "2015-09-01T23:34:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![extesy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/extesy/32/4529_2.png) [@extesy](https://discuss.elastic.co/u/extesy)\
**Post date:** [September 1, 2015, 11:34pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-doesnt-work-with-nested-type-fields/28476/1 "2015-09-01T23:34:37Z")

</div>

Somehow I can't make elasticsearch aggregation+filter to work with nested fields. The data schema (relevant part) is like this:

```
"mappings": {
  "rb": {
    "properties": {
      "project": {
        "type": "nested",
        "properties": {
          "age": {
            "type": "long"
          },
          "name": {
            "type": "string",
            "index": "not_analyzed"
          }
        }
      }    
    }
  }
}

```

Essentially "rb" object contains a nested field called "project" which contains two more fields - "name" and "age". Query I'm running:

```
"aggs": {
  "root": {
    "aggs": {
      "group": {
        "aggs": {
          "filtered": {
            "aggs": {
              "order": {
                "percentiles": {
                  "field": "project.age",
                  "percents": ["50"]
                }
              }
            },
            "filter": {
              "range": {
                "last_updated": {
                  "gte": "2015-01-01",
                  "lt": "2015-07-01"
                }
              }
            }
          }
        },
        "terms": {
          "field": "project.name",
          "min_doc_count": 5,
          "order": {
            "filtered>order.50": "asc"
          },
          "shard_size": 10,
          "size": 10
        }
      }
    },
    "nested": {
      "path": "project"
    }
  }
}

```

This query is supposed to produce top 10 projects (project.name field) which match the date filter, sorted by their median age, ignoring projects with less than 5 mentions in the database. Median should be calculated only for projects matching the filter (date range).

Despite having more than a hundred thousands objects in the database, this query produces empty list. No errors, just empty response. I've tried it both on ES 1.6 and ES 2.0-beta. What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![remram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remram/32/4077_2.png) [@remram](https://discuss.elastic.co/u/remram)\
**Post date:** [September 2, 2015, 5:52am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-doesnt-work-with-nested-type-fields/28476/2 "2015-09-02T05:52:52Z")

</div>

try do it like this!

```
{
  "query": {
    "filtered": {
      "query": {...}
    }
  },
  "filter": {...},
  "aggregations": {
    "root": {
      "nested": {
        "path": "project"
      },
      "aggregations": {
        "group": {
          "filter": {...},
          "aggregations": {
            "project_name": {
              "terms": {
                "field": "project.name",
                ...
              }
            }
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![extesy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/extesy/32/4529_2.png) [@extesy](https://discuss.elastic.co/u/extesy)\
**Post date:** [September 2, 2015, 5:50pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-doesnt-work-with-nested-type-fields/28476/3 "2015-09-02T17:50:51Z")

</div>

Not exactly like this, but it worked when I moved filtered aggregation to the top level. I also had to increase shard\_size to 100, otherwise I got less than 10 values back:

```
"aggs": {
  "filtered": {
    "filter": {
      "range": {
        "last_updated": {
          "gte": "2015-01-01",
          "lt": "2015-07-01"
        }
      }
    },
    "aggs": {
      "root": {
        "nested": {
          "path": "project"
        },
        "aggs": {
          "group": {
            "terms": {
              "field": "project.name",
              "min_doc_count": 5,
              "order": {
                "order.50": "asc"
              },
              "shard_size": 100,
              "size": 10
            },
            "aggs": {
              "order": {
                "percentiles": {
                  "field": "project.age",
                  "percents": ["50"]
                }
              }
            }
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![extesy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/extesy/32/4529_2.png) [@extesy](https://discuss.elastic.co/u/extesy)\
**Post date:** [September 2, 2015, 5:56pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-doesnt-work-with-nested-type-fields/28476/4 "2015-09-02T17:56:06Z")

</div>

But now I have a bit more tricky question, involving two nested fields. I now have another field "comment" with exactly the same mapping as "project" - same nested format, same two subfields "name" and "age".

I need to change the query above to compute median on "project.age" but order by "comment.name" or vice versa. The purpose is to get top projects with lowest median comment time. But since both fields are nested and I can only specify one of them in nested aggregation, when I change order field from "project.age" to "comment.age" I don't get anything back.

Do I need to somehow utilize elasticsearch 2.0 sibling aggregations here or there's a simplier soluiton?

---

<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:** [July 5, 2017, 11:52pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-doesnt-work-with-nested-type-fields/28476/5 "2017-07-05T23:52:34Z")

</div>


