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,
@ -126,6 +127,7 @@ Response:
] ]
} }
} }
}
-------------------------------------------------- --------------------------------------------------
Like with the normal <<search-aggregations-bucket-histogram-aggregation,histogram>>, both document level scripts and Like with the normal <<search-aggregations-bucket-histogram-aggregation,histogram>>, both document level scripts and

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",
@ -48,6 +49,7 @@ Response:
] ]
} }
} }
}
-------------------------------------------------- --------------------------------------------------
[[date-format-pattern]] [[date-format-pattern]]

View File

@ -28,7 +28,8 @@ Response:
-------------------------------------------------- --------------------------------------------------
{ {
"aggregations": { "aggregations": {
"rings": [ "rings" : {
"buckets": [
{ {
"unit": "km", "unit": "km",
"to": 100.0, "to": 100.0,
@ -48,6 +49,7 @@ Response:
] ]
} }
} }
}
-------------------------------------------------- --------------------------------------------------
The specified field must be of type `geo_point` (which can only be set explicitly in the mappings). And it can also hold an array of `geo_point` fields, in which case all will be taken into account during aggregation. The origin point can accept all formats supported by the `geo_point` <<mapping-geo-point-type,type>>: The specified field must be of type `geo_point` (which can only be set explicitly in the mappings). And it can also hold an array of `geo_point` fields, in which case all will be taken into account during aggregation. The origin point can accept all formats supported by the `geo_point` <<mapping-geo-point-type,type>>:

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
@ -50,6 +51,7 @@ And the following may be the response:
] ]
} }
} }
}
-------------------------------------------------- --------------------------------------------------
The response above shows that none of the aggregated products has a price that falls within the range of `[100 - 150)`. By default, the response will only contain the non-empty buckets, though it is possible to also return those, by setting the `empty_buckets` flag to `true`: The response above shows that none of the aggregated products has a price that falls within the range of `[100 - 150)`. By default, the response will only contain the non-empty buckets, though it is possible to also return those, by setting the `empty_buckets` flag to `true`:
@ -75,7 +77,8 @@ Response:
-------------------------------------------------- --------------------------------------------------
{ {
"aggregations": { "aggregations": {
"prices": [ "prices" : {
"buckets": [
{ {
"key": 0, "key": 0,
"doc_count": 2 "doc_count": 2
@ -95,6 +98,7 @@ Response:
] ]
} }
} }
}
-------------------------------------------------- --------------------------------------------------
==== Order ==== Order
@ -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
@ -214,6 +219,7 @@ NOTE: The special value `0` can be used to add empty buckets to the response b
} }
} }
} }
}
-------------------------------------------------- --------------------------------------------------
==== Response Format ==== Response Format
@ -242,6 +248,7 @@ Response:
{ {
"aggregations": { "aggregations": {
"prices": { "prices": {
"buckets": {
"0": { "0": {
"key": 0, "key": 0,
"doc_count": 2 "doc_count": 2
@ -257,4 +264,5 @@ 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",
@ -44,6 +45,7 @@ Response:
] ]
} }
} }
}
-------------------------------------------------- --------------------------------------------------
IP ranges can also be defined as CIDR masks: IP ranges can also be defined as CIDR masks:
@ -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,
@ -91,4 +94,5 @@ 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
@ -48,6 +49,7 @@ Response:
] ]
} }
} }
}
-------------------------------------------------- --------------------------------------------------
==== Keyed Response ==== Keyed Response
@ -82,6 +84,7 @@ Response:
"aggregations": { "aggregations": {
"price_ranges" : { "price_ranges" : {
"buckets": {
"*-50.0": { "*-50.0": {
"to": 50, "to": 50,
"doc_count": 2 "doc_count": 2
@ -98,6 +101,7 @@ Response:
} }
} }
} }
}
-------------------------------------------------- --------------------------------------------------
It is also possible to customize the key for each range: It is also possible to customize the key for each range:
@ -200,7 +204,8 @@ Response:
-------------------------------------------------- --------------------------------------------------
{ {
"aggregations": { "aggregations": {
"price_ranges": [ "price_ranges" : {
"buckets": [
{ {
"to": 50, "to": 50,
"doc_count": 2, "doc_count": 2,
@ -238,6 +243,7 @@ Response:
] ]
} }
} }
}
-------------------------------------------------- --------------------------------------------------
If a sub aggregation is also based on the same value source as the range aggregation (like the `stats` aggregation in the example above) it is possible to leave out the value source definition for it. The following will return the same response as above: If a sub aggregation is also based on the same value source as the range aggregation (like the `stats` aggregation in the example above) it is possible to leave out the value source definition for it. The following will return the same response as above:

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();
} }
} }