Browse Source

Made all multi-bucket aggs return consistent response format

Closes #4926
uboness 11 years ago
parent
commit
dd389d1cc5

+ 15 - 13
docs/reference/search/aggregations/bucket/datehistogram-aggregation.asciidoc

@@ -111,19 +111,21 @@ Response:
 --------------------------------------------------
 {
     "aggregations": {
-        "articles_over_time": [
-            {
-                "key_as_string": "2013-02-02",
-                "key": 1328140800000,
-                "doc_count": 1
-            },
-            {
-                "key_as_string": "2013-03-02",
-                "key": 1330646400000,
-                "doc_count": 2
-            },
-            ...
-        ]
+        "articles_over_time": {
+            "buckets": [
+                {
+                    "key_as_string": "2013-02-02",
+                    "key": 1328140800000,
+                    "doc_count": 1
+                },
+                {
+                    "key_as_string": "2013-03-02",
+                    "key": 1330646400000,
+                    "doc_count": 2
+                },
+                ...
+            ]
+        }
     }
 }
 --------------------------------------------------

+ 14 - 12
docs/reference/search/aggregations/bucket/daterange-aggregation.asciidoc

@@ -34,18 +34,20 @@ Response:
     ...
 
     "aggregations": {
-        "range": [
-            {
-                "to": 1.3437792E+12,
-                "to_as_string": "08-2012",
-                "doc_count": 7
-            },
-            {
-                "from": 1.3437792E+12,
-                "from_as_string": "08-2012",
-                "doc_count": 2
-            }
-        ]
+        "range": {
+            "buckets": [
+                {
+                    "to": 1.3437792E+12,
+                    "to_as_string": "08-2012",
+                    "doc_count": 7
+                },
+                {
+                    "from": 1.3437792E+12,
+                    "from_as_string": "08-2012",
+                    "doc_count": 2
+                }
+            ]
+        }
     }
 }
 --------------------------------------------------

+ 20 - 18
docs/reference/search/aggregations/bucket/geodistance-aggregation.asciidoc

@@ -28,24 +28,26 @@ Response:
 --------------------------------------------------
 {
     "aggregations": {
-        "rings": [
-            {
-                "unit": "km",
-                "to": 100.0,
-                "doc_count": 3
-            },
-            {
-                "unit": "km",
-                "from": 100.0,
-                "to": 300.0,
-                "doc_count": 1
-            },
-            {
-                "unit": "km",
-                "from": 300.0,
-                "doc_count": 7
-            }
-        ]
+        "rings" : {
+            "buckets": [
+                {
+                    "unit": "km",
+                    "to": 100.0,
+                    "doc_count": 3
+                },
+                {
+                    "unit": "km",
+                    "from": 100.0,
+                    "to": 300.0,
+                    "doc_count": 1
+                },
+                {
+                    "unit": "km",
+                    "from": 300.0,
+                    "doc_count": 7
+                }
+            ]
+        }
     }
 }
 --------------------------------------------------

+ 75 - 67
docs/reference/search/aggregations/bucket/histogram-aggregation.asciidoc

@@ -34,20 +34,22 @@ And the following may be the response:
 --------------------------------------------------
 {
     "aggregations": {
-        "prices": [
-            {
-                "key": 0,
-                "doc_count": 2
-            },
-            {
-                "key": 50,
-                "doc_count": 4
-            },
-            {
-                "key": 150,
-                "doc_count": 3
-            }
-        ]
+        "prices" : {
+            "buckets": [
+                {
+                    "key": 0,
+                    "doc_count": 2
+                },
+                {
+                    "key": 50,
+                    "doc_count": 4
+                },
+                {
+                    "key": 150,
+                    "doc_count": 3
+                }
+            ]
+        }
     }
 }
 --------------------------------------------------
@@ -75,24 +77,26 @@ Response:
 --------------------------------------------------
 {
     "aggregations": {
-        "prices": [
-            {
-                "key": 0,
-                "doc_count": 2
-            },
-            {
-                "key": 50,
-                "doc_count": 4
-            },
-            {
-                "key" : 100,
-                "doc_count" : 0
-            },
-            {
-                "key": 150,
-                "doc_count": 3
-            }
-        ]
+        "prices" : {
+            "buckets": [
+                {
+                    "key": 0,
+                    "doc_count": 2
+                },
+                {
+                    "key": 50,
+                    "doc_count": 4
+                },
+                {
+                    "key" : 100,
+                    "doc_count" : 0
+                },
+                {
+                    "key": 150,
+                    "doc_count": 3
+                }
+            ]
+        }
     }
 }
 --------------------------------------------------
@@ -187,29 +191,31 @@ NOTE:   The special value `0` can be used to add empty buckets to the response b
 {
     "aggregations": {
         "prices": {
-            "0": {
-                "key": 0,
-                "doc_count": 2
-            },
-            "50": {
-                "key": 50,
-                "doc_count": 0
-            },
-            "150": {
-                "key": 150,
-                "doc_count": 3
-            },
-            "200": {
-                "key": 150,
-                "doc_count": 0
-            },
-            "250": {
-                "key": 150,
-                "doc_count": 0
-            },
-            "300": {
-                "key": 150,
-                "doc_count": 1
+            "buckets": {
+                "0": {
+                    "key": 0,
+                    "doc_count": 2
+                },
+                "50": {
+                    "key": 50,
+                    "doc_count": 0
+                },
+                "150": {
+                    "key": 150,
+                    "doc_count": 3
+                },
+                "200": {
+                    "key": 150,
+                    "doc_count": 0
+                },
+                "250": {
+                    "key": 150,
+                    "doc_count": 0
+                },
+                "300": {
+                    "key": 150,
+                    "doc_count": 1
+                }
             }
         }
    }
@@ -242,19 +248,21 @@ Response:
 {
     "aggregations": {
         "prices": {
-            "0": {
-                "key": 0,
-                "doc_count": 2
-            },
-            "50": {
-                "key": 50,
-                "doc_count": 4
-            },
-            "150": {
-                "key": 150,
-                "doc_count": 3
+            "buckets": {
+                "0": {
+                    "key": 0,
+                    "doc_count": 2
+                },
+                "50": {
+                    "key": 50,
+                    "doc_count": 4
+                },
+                "150": {
+                    "key": 150,
+                    "doc_count": 3
+                }
             }
         }
-   }
+    }
 }
 --------------------------------------------------

+ 34 - 30
docs/reference/search/aggregations/bucket/iprange-aggregation.asciidoc

@@ -30,18 +30,20 @@ Response:
     ...
 
     "aggregations": {
-        "ip_ranges": [
-            {
-                "to": 167772165,
-                "to_as_string": "10.0.0.5",
-                "doc_count": 4
-            },
-            {
-                "from": 167772165,
-                "from_as_string": "10.0.0.5",
-                "doc_count": 6
-            }
-        ]
+        "ip_ranges":
+            "buckets" : [
+                {
+                    "to": 167772165,
+                    "to_as_string": "10.0.0.5",
+                    "doc_count": 4
+                },
+                {
+                    "from": 167772165,
+                    "from_as_string": "10.0.0.5",
+                    "doc_count": 6
+                }
+            ]
+        }
     }
 }
 --------------------------------------------------
@@ -71,24 +73,26 @@ Response:
 --------------------------------------------------
 {
     "aggregations": {
-        "ip_ranges": [
-            {
-                "key": "10.0.0.0/25",
-                "from": 1.6777216E+8,
-                "from_as_string": "10.0.0.0",
-                "to": 167772287,
-                "to_as_string": "10.0.0.127",
-                "doc_count": 127
-            },
-            {
-                "key": "10.0.0.127/25",
-                "from": 1.6777216E+8,
-                "from_as_string": "10.0.0.0",
-                "to": 167772287,
-                "to_as_string": "10.0.0.127",
-                "doc_count": 127
-            }
-        ]
+        "ip_ranges": {
+            "buckets": [
+                {
+                    "key": "10.0.0.0/25",
+                    "from": 1.6777216E+8,
+                    "from_as_string": "10.0.0.0",
+                    "to": 167772287,
+                    "to_as_string": "10.0.0.127",
+                    "doc_count": 127
+                },
+                {
+                    "key": "10.0.0.127/25",
+                    "from": 1.6777216E+8,
+                    "from_as_string": "10.0.0.0",
+                    "to": 167772287,
+                    "to_as_string": "10.0.0.127",
+                    "doc_count": 127
+                }
+            ]
+        }
     }
 }
 --------------------------------------------------

+ 70 - 64
docs/reference/search/aggregations/bucket/range-aggregation.asciidoc

@@ -31,21 +31,23 @@ Response:
     ...
 
     "aggregations": {
-        "price_ranges": [
-            {
-                "to": 50,
-                "doc_count": 2
-            },
-            {
-                "from": 50,
-                "to": 100,
-                "doc_count": 4
-            },
-            {
-                "from": 100,
-                "doc_count": 4
-            }
-        ]
+        "price_ranges" : {
+            "buckets": [
+                {
+                    "to": 50,
+                    "doc_count": 2
+                },
+                {
+                    "from": 50,
+                    "to": 100,
+                    "doc_count": 4
+                },
+                {
+                    "from": 100,
+                    "doc_count": 4
+                }
+            ]
+        }
     }
 }
 --------------------------------------------------
@@ -81,19 +83,21 @@ Response:
     ...
 
     "aggregations": {
-        "price_ranges": {
-            "*-50.0": {
-                "to": 50,
-                "doc_count": 2
-            },
-            "50.0-100.0": {
-                "from": 50,
-                "to": 100,
-                "doc_count": 4
-            },
-            "100.0-*": {
-                "from": 100,
-                "doc_count": 4
+        "price_ranges" : {
+            "buckets": {
+                "*-50.0": {
+                    "to": 50,
+                    "doc_count": 2
+                },
+                "50.0-100.0": {
+                    "from": 50,
+                    "to": 100,
+                    "doc_count": 4
+                },
+                "100.0-*": {
+                    "from": 100,
+                    "doc_count": 4
+                }
             }
         }
     }
@@ -128,7 +132,7 @@ It is also possible to customize the key for each range:
 {
     "aggs" : {
         "price_ranges" : {
-            "range" : { 
+            "range" : {
                 "script" : "doc['price'].value",
                 "ranges" : [
                     { "to" : 50 },
@@ -200,42 +204,44 @@ Response:
 --------------------------------------------------
 {
     "aggregations": {
-        "price_ranges": [
-            {
-                "to": 50,
-                "doc_count": 2,
-                "price_stats": {
-                    "count": 2,
-                    "min": 20,
-                    "max": 47,
-                    "avg": 33.5,
-                    "sum": 67
-                }
-            },
-            {
-                "from": 50,
-                "to": 100,
-                "doc_count": 4,
-                "price_stats": {
-                    "count": 4,
-                    "min": 60,
-                    "max": 98,
-                    "avg": 82.5,
-                    "sum": 330
-                }
-            },
-            {
-                "from": 100,
-                "doc_count": 4,
-                "price_stats": {
-                    "count": 4,
-                    "min": 134,
-                    "max": 367,
-                    "avg": 216,
-                    "sum": 864
+        "price_ranges" : {
+            "buckets": [
+                {
+                    "to": 50,
+                    "doc_count": 2,
+                    "price_stats": {
+                        "count": 2,
+                        "min": 20,
+                        "max": 47,
+                        "avg": 33.5,
+                        "sum": 67
+                    }
+                },
+                {
+                    "from": 50,
+                    "to": 100,
+                    "doc_count": 4,
+                    "price_stats": {
+                        "count": 4,
+                        "min": 60,
+                        "max": 98,
+                        "avg": 82.5,
+                        "sum": 330
+                    }
+                },
+                {
+                    "from": 100,
+                    "doc_count": 4,
+                    "price_stats": {
+                        "count": 4,
+                        "min": 134,
+                        "max": 367,
+                        "avg": 216,
+                        "sum": 864
+                    }
                 }
-            }
-        ]
+            ]
+        }
     }
 }
 --------------------------------------------------

+ 2 - 2
docs/reference/search/aggregations/bucket/terms-aggregation.asciidoc

@@ -274,8 +274,8 @@ There are two mechanisms by which terms aggregations can be executed: either by
 data per-bucket (`map`), or by using ordinals of the field values instead of the values themselves (`ordinals`). Although the
 latter execution mode can be expected to be slightly faster, it is only available for use when the underlying data source exposes
 those terms ordinals. Moreover, it may actually be slower if most field values are unique. Elasticsearch tries to have sensible
-defaults when it comes to the execution mode that should be used, but in case you know that an execution mode may perform better
-than the other one, you have the ability to provide Elasticsearch with a hint:
+defaults when it comes to the execution mode that should be used, but in case you know that one execution mode may perform better
+than the other one, you have the ability to "hint" it to Elasticsearch:
 
 [source,js]
 --------------------------------------------------

+ 4 - 3
src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalHistogram.java

@@ -363,10 +363,11 @@ public class InternalHistogram<B extends InternalHistogram.Bucket> extends Inter
 
     @Override
     public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
+        builder.startObject(name);
         if (keyed) {
-            builder.startObject(name);
+            builder.startObject(CommonFields.BUCKETS);
         } else {
-            builder.startArray(name);
+            builder.startArray(CommonFields.BUCKETS);
         }
 
         for (B bucket : buckets) {
@@ -396,7 +397,7 @@ public class InternalHistogram<B extends InternalHistogram.Bucket> extends Inter
         } else {
             builder.endArray();
         }
-        return builder;
+        return builder.endObject();
     }
 
 }

+ 6 - 5
src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalRange.java

@@ -294,26 +294,27 @@ public class InternalRange<B extends InternalRange.Bucket> extends InternalAggre
             out.writeDouble(((Bucket) bucket).from);
             out.writeDouble(((Bucket) bucket).to);
             out.writeVLong(((Bucket) bucket).docCount);
-            ((Bucket) bucket).aggregations.writeTo(out);
+            bucket.aggregations.writeTo(out);
         }
     }
 
     @Override
     public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
+        builder.startObject(name);
         if (keyed) {
-            builder.startObject(name);
+            builder.startObject(CommonFields.BUCKETS);
         } else {
-            builder.startArray(name);
+            builder.startArray(CommonFields.BUCKETS);
         }
         for (B range : ranges) {
-            ((Bucket) range).toXContent(builder, params, formatter, keyed);
+            range.toXContent(builder, params, formatter, keyed);
         }
         if (keyed) {
             builder.endObject();
         } else {
             builder.endArray();
         }
-        return builder;
+        return builder.endObject();
     }
 
 }