What you want is a date_histogram-facet.
Here's an example that should cover the various ways I can interpret what you want:
export ELASTICSEARCH_ENDPOINT="http://localhost:9200"
# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"type","_id":1}}
{"created_date":"2013-09-30T12:00:00Z","key":"foo","count":12}
{"index":{"_index":"play","_type":"type","_id":2}}
{"created_date":"2013-09-30T13:00:00Z","key":"bar","count":14}
{"index":{"_index":"play","_type":"type","_id":3}}
{"created_date":"2013-10-01T12:00:00Z","key":"foo","count":42}
{"index":{"_index":"play","_type":"type","_id":4}}
{"created_date":"2013-10-01T14:00:00Z","key":"foo","count":13}
'
# Do searches
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
"facets": {
"foos_per_interval": {
"date_histogram": {
"key_field": "created_date",
"value_field": "count",
"interval": "day"
},
"facet_filter": {
"term": {
"key": "foo"
}
}
}
}
}
'
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"hits" : {
"total" : 4,
"max_score" : 1.0,
"hits" : [ {
"_index" : "play",
"_type" : "type",
"_id" : "1",
"_score" : 1.0, "_source" : {"created_date":"2013-09-30T12:00:00Z","key":"foo","count":12}
}, {
"_index" : "play",
"_type" : "type",
"_id" : "2",
"_score" : 1.0, "_source" : {"created_date":"2013-09-30T13:00:00Z","key":"bar","count":14}
}, {
"_index" : "play",
"_type" : "type",
"_id" : "3",
"_score" : 1.0, "_source" : {"created_date":"2013-10-01T12:00:00Z","key":"foo","count":42}
}, {
"_index" : "play",
"_type" : "type",
"_id" : "4",
"_score" : 1.0, "_source" : {"created_date":"2013-10-01T14:00:00Z","key":"foo","count":13}
} ]
},
"facets" : {
"foos_per_interval" : {
"_type" : "date_histogram",
"entries" : [ {
"time" : 1380499200000,
"count" : 1,
"min" : 12.0,
"max" : 12.0,
"total" : 12.0,
"total_count" : 1,
"mean" : 12.0
}, {
"time" : 1380585600000,
"count" : 2,
"min" : 13.0,
"max" : 42.0,
"total" : 55.0,
"total_count" : 2,
"mean" : 27.5
} ]
}
}
}