Skip to content

Instantly share code, notes, and snippets.

@diegoasth
Created October 8, 2014 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegoasth/406148525616f9f527ab to your computer and use it in GitHub Desktop.
Save diegoasth/406148525616f9f527ab to your computer and use it in GitHub Desktop.
test: "scripted_metric aggregation nested inside a date_histogram aggregation"
delete /test
PUT /test
{
"mappings": {
"doc": {
"properties": {
"timestamp": {
"type": "date"
}
}
}
}
}
POST /test/doc
{
"timestamp": "2014-10-07T00:01:00",
"kpiA": 1
}
POST /test/doc
{
"timestamp": "2014-10-07T00:02:00",
"kpiA": 2
}
POST /test/doc
{
"timestamp": "2014-10-07T00:03:00",
"kpiA": 3
}
POST /test/doc
{
"timestamp": "2014-10-07T00:04:00",
"kpiA": 4
}
POST /test/doc
{
"timestamp": "2014-10-07T01:01:00",
"kpiA": 1
}
POST /test/_search
{
"size": 0,
"aggs": {
"histo": {
"date_histogram": {
"field": "timestamp",
"interval": "1h",
"min_doc_count": 0,
"format": "yyyy-MM-dd HH:mm:sss"
},
"aggs": {
"sum_kpiA": {
"sum": {
"field": "kpiA"
}
},
"scripted_sum_kpiA": {
"scripted_metric": {
"init_script": "_agg['kpiA'] =0; ",
"map_script": "_agg.kpiA += doc['kpiA'].value; ",
"combine_script": "tempA=0; tempA += _agg.kpiA; return tempA;",
"reduce_script": " sumA=0; for (a in _aggs) { sumA+=a;}; return sumA;"
}
}
}
}
}
}
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 0,
"hits": []
},
"aggregations": {
"histo": {
"buckets": [
{
"key_as_string": "2014-10-07 00:00:000",
"key": 1412640000000,
"doc_count": 4,
"sum_kpiA": {
"value": 10
},
"scripted_sum_kpiA": {
"value": 11
}
},
{
"key_as_string": "2014-10-07 01:00:000",
"key": 1412643600000,
"doc_count": 1,
"sum_kpiA": {
"value": 1
},
"scripted_sum_kpiA": {
"value": 2
}
}
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment