# Composite Agg after Date Filter

**URL:** <https://discuss.elastic.co/t/composite-agg-after-date-filter/223578>\
**Category:** Elasticsearch\
**Created:** [March 13, 2020, 8:56pm UTC](https://discuss.elastic.co/t/composite-agg-after-date-filter/223578 "2020-03-13T20:56:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adaley222](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@adaley222](https://discuss.elastic.co/u/adaley222)\
**Post date:** [March 13, 2020, 8:56pm UTC](https://discuss.elastic.co/t/composite-agg-after-date-filter/223578/1 "2020-03-13T20:56:41Z")

</div>

I am trying to count documents over a week, for each hour/day of the week. So essentially, I'd like the output to be 7x24 =168 doc\_counts, for a specified date range.

The code below nearly gives me what I need, however, it only returns the top terms when I need all of them.

```
{"size": 0,
"aggs" : {
		"booked_filter" : {
            "filter" : { "term": {"type":"booked"} },
    "aggs": {
        "range": {
        	"date_range": {
            	"field": "start",
                "format": "date_hour_minute_second",
            	"ranges": [{ "to": "now", "from": "now-7d"}]},
	"aggs": {
        "day-of-week-count": {
            "terms": {
                "script": "return doc[\"start\"].value.getDayOfWeekEnum();"},
    "aggs": {
        "hour-of_day-count": {
            "terms": {
            	"script": "return doc[\"start\"].value.getHourOfDay();"
                				}
            				}
						}
					}
				}
			}
		}
    }
}
}

```

The documentation says that instead of just setting the size greater than the cardinality of the field, I should use a composite aggregation. This is what I've tried:

```
{"size": 0,
"aggs" : {
		"booked_filter" : {
            "filter" : { "term": {"type":"booked"} },
"aggs": {
    	"date-filter": {
    		"filter": { 
    			"range": {
    				"startu": {
    					"gte": 1583440081000,
    					"lte": 1584131281000}}}},
	"aggs": {
		"composite": {
			"sources": [
				{"day-of-week-count": { "terms": {"script": "return doc[\"start\"].value.getDayOfWeekEnum();"}}},
        		{"hour-count": {"terms": {"script": "return doc[\"start\"].value.getHourOfDay();"}}}]
					}
				}
			}
		}
    }
}

```

However, this gives me the following error:

`"[composite] aggregation cannot be used with a parent aggregation of type: [FilterAggregatorFactory]"

Am I approaching this entirely incorrectly? Or is this something Elasticsearch isn't able to do at the moment?

Adjusting the size of the first query should give me the info I need, but I'd like to know the proper way to handle a use case like this if there is one.

---

<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:** [April 10, 2020, 8:56pm UTC](https://discuss.elastic.co/t/composite-agg-after-date-filter/223578/2 "2020-04-10T20:56:43Z")

</div>

Closed on April 10, 2020, 8:56pm UTC

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