# How can I filtering of child aggs on parent agg's key（date\_histogram.key\_as\_string）

**URL:** <https://discuss.elastic.co/t/how-can-i-filtering-of-child-aggs-on-parent-aggs-key-date-histogram-key-as-string/286483>\
**Category:** Elasticsearch\
**Created:** [October 12, 2021, 11:33am UTC](https://discuss.elastic.co/t/how-can-i-filtering-of-child-aggs-on-parent-aggs-key-date-histogram-key-as-string/286483 "2021-10-12T11:33:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chenbihao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chenbihao/32/95744_2.png) [@chenbihao](https://discuss.elastic.co/u/chenbihao)\
**Post date:** [October 12, 2021, 11:33am UTC](https://discuss.elastic.co/t/how-can-i-filtering-of-child-aggs-on-parent-aggs-key-date-histogram-key-as-string/286483/1 "2021-10-12T11:33:51Z")

</div>

I want to Change `filter.range.exitTime.lte:"2021-08"`  
to `date_histogram.key_as_string`  
How do i make it

```auto
GET /workerentryexitrecord/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "project.id": "111111111111"
          }
        },
        {
          "match": {
            "isDelete": false
          }
        }
      ]
    }
  },
  "aggs": {
    "month_count": {
      "date_histogram": {
        "field": "entryTime",
        "interval": "month",
        "format": "8uuuu-MM",
        "min_doc_count": 0
      },
      "aggs": {
        "subBuckets": {
          "filter": {
            "range": {
              "exitTime": {
                "lte": "2021-08||/M" #Replace with 【date_histogram.key_as_string】
              }
            }
          },
          "aggs": {
            "month_exit": {
              "value_count": {
                "field": "exitTime"
              }
            }
          }
        }
      }
    }
  }
}

```

result:

```auto
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 22,
    "max_score" : 0.0,
    "hits" : []
  },
  "aggregations" : {
    "month_count" : {
      "meta" : { },
      "buckets" : [
        {
          "key_as_string" : "2021-07",
          "key" : 1625097600000,
          "doc_count" : 1,
          "subBuckets" : {
            "doc_count" : 1,
            "month_exit" : {
              "value" : 1
            }
          }
        },
        {
          "key_as_string" : "2021-08",
          "key" : 1627776000000,
          "doc_count" : 21,
          "subBuckets" : {
            "doc_count" : 9,
            "month_exit" : {
              "value" : 9
            }
          }
        }
      ]
    }
  }
}

```

---

<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:** [November 9, 2021, 11:34am UTC](https://discuss.elastic.co/t/how-can-i-filtering-of-child-aggs-on-parent-aggs-key-date-histogram-key-as-string/286483/2 "2021-11-09T11:34:48Z")

</div>

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