|
@@ -153,3 +153,109 @@ POST /sales/_search?size=0
|
|
|
|
|
|
<1> This date will be converted to `2016-02-15T00:00:00.000+01:00`.
|
|
|
<2> `now/d` will be rounded to the beginning of the day in the CET time zone.
|
|
|
+
|
|
|
+==== Keyed Response
|
|
|
+
|
|
|
+Setting the `keyed` flag to `true` will associate a unique string key with each bucket and return the ranges as a hash rather than an array:
|
|
|
+
|
|
|
+[source,js]
|
|
|
+--------------------------------------------------
|
|
|
+POST /sales/_search?size=0
|
|
|
+{
|
|
|
+ "aggs": {
|
|
|
+ "range": {
|
|
|
+ "date_range": {
|
|
|
+ "field": "date",
|
|
|
+ "format": "MM-yyy",
|
|
|
+ "ranges": [
|
|
|
+ { "to": "now-10M/M" },
|
|
|
+ { "from": "now-10M/M" }
|
|
|
+ ],
|
|
|
+ "keyed": true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+--------------------------------------------------
|
|
|
+// CONSOLE
|
|
|
+// TEST[setup:sales s/now-10M\/M/10-2015/]
|
|
|
+
|
|
|
+Response:
|
|
|
+
|
|
|
+[source,js]
|
|
|
+--------------------------------------------------
|
|
|
+{
|
|
|
+ ...
|
|
|
+ "aggregations": {
|
|
|
+ "range": {
|
|
|
+ "buckets": {
|
|
|
+ "*-10-2015": {
|
|
|
+ "to": 1.4436576E12,
|
|
|
+ "to_as_string": "10-2015",
|
|
|
+ "doc_count": 7
|
|
|
+ },
|
|
|
+ "10-2015-*": {
|
|
|
+ "from": 1.4436576E12,
|
|
|
+ "from_as_string": "10-2015",
|
|
|
+ "doc_count": 0
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+--------------------------------------------------
|
|
|
+// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
|
|
|
+
|
|
|
+It is also possible to customize the key for each range:
|
|
|
+
|
|
|
+[source,js]
|
|
|
+--------------------------------------------------
|
|
|
+POST /sales/_search?size=0
|
|
|
+{
|
|
|
+ "aggs": {
|
|
|
+ "range": {
|
|
|
+ "date_range": {
|
|
|
+ "field": "date",
|
|
|
+ "format": "MM-yyy",
|
|
|
+ "ranges": [
|
|
|
+ { "from": "01-2015", "to": "03-2015", "key": "quarter_01" },
|
|
|
+ { "from": "03-2015", "to": "06-2015", "key": "quarter_02" }
|
|
|
+ ],
|
|
|
+ "keyed": true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+--------------------------------------------------
|
|
|
+// CONSOLE
|
|
|
+// TEST[setup:sales]
|
|
|
+
|
|
|
+Response:
|
|
|
+
|
|
|
+[source,js]
|
|
|
+--------------------------------------------------
|
|
|
+{
|
|
|
+ ...
|
|
|
+ "aggregations": {
|
|
|
+ "range": {
|
|
|
+ "buckets": {
|
|
|
+ "quarter_01": {
|
|
|
+ "from": 1.4200704E12,
|
|
|
+ "from_as_string": "01-2015",
|
|
|
+ "to": 1.425168E12,
|
|
|
+ "to_as_string": "03-2015",
|
|
|
+ "doc_count": 5
|
|
|
+ },
|
|
|
+ "quarter_02": {
|
|
|
+ "from": 1.425168E12,
|
|
|
+ "from_as_string": "03-2015",
|
|
|
+ "to": 1.4331168E12,
|
|
|
+ "to_as_string": "06-2015",
|
|
|
+ "doc_count": 2
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+--------------------------------------------------
|
|
|
+// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
|