Skip to content

Commit

Permalink
Aggregations: Fixed Histogram key_as_string bug
Browse files Browse the repository at this point in the history
The key as string field in the response for the histogram aggregation will now only show if format is specified on the request.

Closes #6655
  • Loading branch information
colings86 committed Jul 16, 2014
1 parent 12321bd commit 2eb9355
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 1 deletion.
129 changes: 129 additions & 0 deletions rest-api-spec/test/search.aggregation/10_histogram.yaml
@@ -0,0 +1,129 @@
---
"Basic test":
- do:
index:
index: test_1
type: test
id: 1
body: { "number" : 1 }

- do:
index:
index: test_1
type: test
id: 2
body: { "number" : 51 }

- do:
index:
index: test_1
type: test
id: 3
body: { "number" : 101 }

- do:
index:
index: test_1
type: test
id: 4
body: { "number" : 151 }

- do:
indices.refresh: {}

- do:
search:
body: { "aggs" : { "histo" : { "histogram" : { "field" : "number", "interval" : 50 } } } }

- match: { hits.total: 4 }

- length: { aggregations.histo.buckets: 4 }

- match: { aggregations.histo.buckets.0.key: 0 }

- is_false: aggregations.histo.buckets.0.key_as_string

- match: { aggregations.histo.buckets.0.doc_count: 1 }

- match: { aggregations.histo.buckets.1.key: 50 }

- is_false: aggregations.histo.buckets.1.key_as_string

- match: { aggregations.histo.buckets.1.doc_count: 1 }

- match: { aggregations.histo.buckets.2.key: 100 }

- is_false: aggregations.histo.buckets.2.key_as_string

- match: { aggregations.histo.buckets.2.doc_count: 1 }

- match: { aggregations.histo.buckets.3.key: 150 }

- is_false: aggregations.histo.buckets.3.key_as_string

- match: { aggregations.histo.buckets.3.doc_count: 1 }

---
"Format test":
- do:
index:
index: test_1
type: test
id: 1
body: { "number" : 1 }

- do:
index:
index: test_1
type: test
id: 2
body: { "number" : 51 }

- do:
index:
index: test_1
type: test
id: 3
body: { "number" : 101 }

- do:
index:
index: test_1
type: test
id: 4
body: { "number" : 151 }

- do:
indices.refresh: {}

- do:
search:
body: { "aggs" : { "histo" : { "histogram" : { "field" : "number", "interval" : 50, "format" : "Value is ##0.0" } } } }

- match: { hits.total: 4 }

- length: { aggregations.histo.buckets: 4 }

- match: { aggregations.histo.buckets.0.key: 0 }

- match: { aggregations.histo.buckets.0.key_as_string: "Value is 0.0" }

- match: { aggregations.histo.buckets.0.doc_count: 1 }

- match: { aggregations.histo.buckets.1.key: 50 }

- match: { aggregations.histo.buckets.1.key_as_string: "Value is 50.0" }

- match: { aggregations.histo.buckets.1.doc_count: 1 }

- match: { aggregations.histo.buckets.2.key: 100 }

- match: { aggregations.histo.buckets.2.key_as_string: "Value is 100.0" }

- match: { aggregations.histo.buckets.2.doc_count: 1 }

- match: { aggregations.histo.buckets.3.key: 150 }

- match: { aggregations.histo.buckets.3.key_as_string: "Value is 150.0" }

- match: { aggregations.histo.buckets.3.doc_count: 1 }
Expand Up @@ -120,7 +120,7 @@ <B extends Bucket> B reduce(List<B> buckets, BigArrays bigArrays) {
}

void toXContent(XContentBuilder builder, Params params, boolean keyed, @Nullable ValueFormatter formatter) throws IOException {
if (formatter != null) {
if (formatter != null && formatter != ValueFormatter.RAW) {
Text keyTxt = new StringText(formatter.format(key));
if (keyed) {
builder.startObject(keyTxt.string());
Expand Down

0 comments on commit 2eb9355

Please sign in to comment.