Created
September 4, 2017 12:54
-
-
Save david206/7cf7af8eb27974e4cfe19763c858ed2a 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. it works with terms aggregation instead of date_histogram, and it works when renaming one of the identically named buckets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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