Made all multi-bucket aggs return consistent response format

Closes #4926
This commit is contained in:
uboness 2014-01-28 17:46:26 +01:00
parent f9d1552282
commit dd389d1cc5
9 changed files with 240 additions and 214 deletions

View File

@ -111,7 +111,8 @@ Response:
-------------------------------------------------- --------------------------------------------------
{ {
"aggregations": { "aggregations": {
"articles_over_time": [ "articles_over_time": {
"buckets": [
{ {
"key_as_string": "2013-02-02", "key_as_string": "2013-02-02",
"key": 1328140800000, "key": 1328140800000,
@ -125,6 +126,7 @@ Response:
... ...
] ]
} }
}
} }
-------------------------------------------------- --------------------------------------------------

View File

@ -34,7 +34,8 @@ Response:
... ...
"aggregations": { "aggregations": {
"range": [ "range": {
"buckets": [
{ {
"to": 1.3437792E+12, "to": 1.3437792E+12,
"to_as_string": "08-2012", "to_as_string": "08-2012",
@ -47,6 +48,7 @@ Response:
} }
] ]
} }
}
} }
-------------------------------------------------- --------------------------------------------------

View File

@ -28,7 +28,8 @@ Response:
-------------------------------------------------- --------------------------------------------------
{ {
"aggregations": { "aggregations": {
"rings": [ "rings" : {
"buckets": [
{ {
"unit": "km", "unit": "km",
"to": 100.0, "to": 100.0,
@ -47,6 +48,7 @@ Response:
} }
] ]
} }
}
} }
-------------------------------------------------- --------------------------------------------------

View File

@ -34,7 +34,8 @@ And the following may be the response:
-------------------------------------------------- --------------------------------------------------
{ {
"aggregations": { "aggregations": {
"prices": [ "prices" : {
"buckets": [
{ {
"key": 0, "key": 0,
"doc_count": 2 "doc_count": 2
@ -49,6 +50,7 @@ And the following may be the response:
} }
] ]
} }
}
} }
-------------------------------------------------- --------------------------------------------------
@ -75,7 +77,8 @@ Response:
-------------------------------------------------- --------------------------------------------------
{ {
"aggregations": { "aggregations": {
"prices": [ "prices" : {
"buckets": [
{ {
"key": 0, "key": 0,
"doc_count": 2 "doc_count": 2
@ -94,6 +97,7 @@ Response:
} }
] ]
} }
}
} }
-------------------------------------------------- --------------------------------------------------
@ -187,6 +191,7 @@ NOTE: The special value `0` can be used to add empty buckets to the response b
{ {
"aggregations": { "aggregations": {
"prices": { "prices": {
"buckets": {
"0": { "0": {
"key": 0, "key": 0,
"doc_count": 2 "doc_count": 2
@ -213,6 +218,7 @@ NOTE: The special value `0` can be used to add empty buckets to the response b
} }
} }
} }
}
} }
-------------------------------------------------- --------------------------------------------------
@ -242,6 +248,7 @@ Response:
{ {
"aggregations": { "aggregations": {
"prices": { "prices": {
"buckets": {
"0": { "0": {
"key": 0, "key": 0,
"doc_count": 2 "doc_count": 2
@ -256,5 +263,6 @@ Response:
} }
} }
} }
}
} }
-------------------------------------------------- --------------------------------------------------

View File

@ -30,7 +30,8 @@ Response:
... ...
"aggregations": { "aggregations": {
"ip_ranges": [ "ip_ranges":
"buckets" : [
{ {
"to": 167772165, "to": 167772165,
"to_as_string": "10.0.0.5", "to_as_string": "10.0.0.5",
@ -43,6 +44,7 @@ Response:
} }
] ]
} }
}
} }
-------------------------------------------------- --------------------------------------------------
@ -71,7 +73,8 @@ Response:
-------------------------------------------------- --------------------------------------------------
{ {
"aggregations": { "aggregations": {
"ip_ranges": [ "ip_ranges": {
"buckets": [
{ {
"key": "10.0.0.0/25", "key": "10.0.0.0/25",
"from": 1.6777216E+8, "from": 1.6777216E+8,
@ -90,5 +93,6 @@ Response:
} }
] ]
} }
}
} }
-------------------------------------------------- --------------------------------------------------

View File

@ -31,7 +31,8 @@ Response:
... ...
"aggregations": { "aggregations": {
"price_ranges": [ "price_ranges" : {
"buckets": [
{ {
"to": 50, "to": 50,
"doc_count": 2 "doc_count": 2
@ -47,6 +48,7 @@ Response:
} }
] ]
} }
}
} }
-------------------------------------------------- --------------------------------------------------
@ -81,7 +83,8 @@ Response:
... ...
"aggregations": { "aggregations": {
"price_ranges": { "price_ranges" : {
"buckets": {
"*-50.0": { "*-50.0": {
"to": 50, "to": 50,
"doc_count": 2 "doc_count": 2
@ -97,6 +100,7 @@ Response:
} }
} }
} }
}
} }
-------------------------------------------------- --------------------------------------------------
@ -200,7 +204,8 @@ Response:
-------------------------------------------------- --------------------------------------------------
{ {
"aggregations": { "aggregations": {
"price_ranges": [ "price_ranges" : {
"buckets": [
{ {
"to": 50, "to": 50,
"doc_count": 2, "doc_count": 2,
@ -237,6 +242,7 @@ Response:
} }
] ]
} }
}
} }
-------------------------------------------------- --------------------------------------------------

View File

@ -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 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 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 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 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 provide Elasticsearch with a hint: than the other one, you have the ability to "hint" it to Elasticsearch:
[source,js] [source,js]
-------------------------------------------------- --------------------------------------------------

View File

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

View File

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