Skip to content

Instantly share code, notes, and snippets.

@david206
Created September 4, 2017 12:53
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 david206/9cda9755543f1a953b6c12797d3a9ddf to your computer and use it in GitHub Desktop.
Save david206/9cda9755543f1a953b6c12797d3a9ddf to your computer and use it in GitHub Desktop.
Failed avg_bucket aggregation when buckets_path include nested bucket with same name as other bucket.
curl -XDELETE 'localhost:9200/docs?pretty'
curl -XPUT 'localhost:9200/docs?pretty' -H 'Content-Type: application/json' -d'
{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 1
}
},
"mapping": {
"doc": {
"_all": {
"enabled": false
},
"properties": {
"date": {
"type": "date",
"format": "yyyy-MM-dd"
},
"group": {
"type": "integer"
}
}
}
}
}
'
curl -XPOST 'localhost:9200/docs/doc?pretty' -H 'Content-Type: application/json' -d'
{
"date": "2017-01-01",
"group": 3
}
'
curl -XPOST 'localhost:9200/docs/doc?pretty' -H 'Content-Type: application/json' -d'
{
"date": "2017-02-01",
"group": 4
}
'
# the following aggregation will cause Error:
curl -XPOST 'localhost:9200/docs/doc/_search?pretty' -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"avg_monthly": {
"avg_bucket": {
"buckets_path": "monthly.groups"
}
},
"groups": {
"cardinality": {
"field": "group"
}
},
"monthly": {
"date_histogram": {
"field": "date",
"interval": "month"
},
"aggs": {
"groups": {
"cardinality": {
"field": "group"
}
}
}
}
}
}
'
# The error:
# {
# "error" : {
# "root_cause" : [ ],
# "type" : "reduce_search_phase_exception",
# "reason" : "[reduce] ",
# "phase" : "merge",
# "grouped" : true,
# "failed_shards" : [ ],
# "caused_by" : {
# "type" : "class_cast_exception",
# "reason" : "org.elasticsearch.search.aggregations.metrics.cardinality.InternalCardinality cannot be cast to org.elasticsearch.search.aggregations.InternalMultiBucketAggregation"
# }
# },
# "status" : 503
# }
# when renaming the outer, not nested "groups" aggregation to "groups2" it works.
curl -XPOST 'localhost:9200/docs/doc/_search?pretty' -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"avg_monthly": {
"avg_bucket": {
"buckets_path": "monthly.groups"
}
},
"groups2": {
"cardinality": {
"field": "group"
}
},
"monthly": {
"date_histogram": {
"field": "date",
"interval": "month"
},
"aggs": {
"groups": {
"cardinality": {
"field": "group"
}
}
}
}
}
}
'
# result:
# {
# "took" : 2,
# "timed_out" : false,
# "_shards" : {
# "total" : 1,
# "successful" : 1,
# "failed" : 0
# },
# "hits" : {
# "total" : 2,
# "max_score" : 0.0,
# "hits" : [ ]
# },
# "aggregations" : {
# "groups2" : {
# "value" : 2
# },
# "monthly" : {
# "buckets" : [
# {
# "key_as_string" : "2017-01-01T00:00:00.000Z",
# "key" : 1483228800000,
# "doc_count" : 1,
# "groups" : {
# "value" : 1
# }
# },
# {
# "key_as_string" : "2017-02-01T00:00:00.000Z",
# "key" : 1485907200000,
# "doc_count" : 1,
# "groups" : {
# "value" : 1
# }
# }
# ]
# },
# "avg_monthly" : {
# "value" : 1.0
# }
# }
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment