# Aggregations and filters both based on children documents

**URL:** https://discuss.elastic.co/t/aggregations-and-filters-both-based-on-children-documents/27055
**Category:** Elasticsearch
**Created:** [August 7, 2015, 4:36pm UTC](https://discuss.elastic.co/t/aggregations-and-filters-both-based-on-children-documents/27055 "2015-08-07T16:36:52Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![CraigL](https://avatars.discourse-cdn.com/v4/letter/c/e9c0ed/32.png) [@CraigL](https://discuss.elastic.co/u/CraigL)
#### Post date: [August 7, 2015, 4:36pm UTC](https://discuss.elastic.co/t/aggregations-and-filters-both-based-on-children-documents/27055/1 "2015-08-07T16:36:52Z")

</div>

Hello,

I'm trying to do some aggregations with children documents, but I also need to filter the results. The basic structure is:

```
document
   - folder { id }

```

So, for example, I want to get a count of documents that have a folder id of 100, 101 and 102.

The following works fine:

```
"aggs": {
  "to-children": {
    "children": {
      "type": "folder"
    },
    "aggs": {
      "x-value": {
        "terms": {
          "field": "folder_id_fi",
          "size": 0
        }
      }
    }
  }
}

```

But when I add the filter in like this:

```
{
  "aggs": {
    "to-children": {
      "children": {
        "type": "folder"
      },
      "aggs": {
        "x-value": {
          "filter": {
            "terms": {
              "folder_id_fi": [100, 101, 102]
            }
          },
          "aggs": {
            "x-value-field": {
              "terms": {
                "field": "folder_id_fi",
                "size": 0
              }
            }
          }
        }
      }
    }
  }
}

```

I get this response:

```
"aggregations": {
   "to-children": {
      "doc_count": 16852243,
      "x-value": {
         "doc_count": 0,
         "x-value-field": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": []
         }
      }
   }
}

```

So, it looks like the filter didn't work (the doc\_count is the same as before the filter was added) and the bucket list is empty.

1.6.0 is the version of ES we're currently using (1.7.1 soon).

Thanks,

CraigL

---

<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:56pm UTC](https://discuss.elastic.co/t/aggregations-and-filters-both-based-on-children-documents/27055/2 "2017-07-05T23:56:48Z")

</div>


