Skip to content

Commit 2eafbaf

Browse files
matarresenik9000
authored andcommitted
Document aggregating by day of the week (#25602)
Add documentation for aggregating by day of the week. Closes #24660
1 parent baa87db commit 2eafbaf

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

docs/reference/aggregations/bucket/datehistogram-aggregation.asciidoc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,58 @@ By default the returned buckets are sorted by their `key` ascending, though the
397397
the `order` setting. Supports the same `order` functionality as the <<search-aggregations-bucket-terms-aggregation-order,`Terms Aggregation`>>.
398398

399399
deprecated[6.0.0, Use `_key` instead of `_time` to order buckets by their dates/keys]
400+
401+
=== Use of a script to aggregate by day of the week
402+
403+
There are some cases where date histogram can't help us, like for example, when we need
404+
to aggregate the results by day of the week.
405+
In this case to overcame the problem, we can use a script that returns the day of the week:
406+
407+
408+
[source,js]
409+
--------------------------------------------------
410+
POST /sales/_search?size=0
411+
{
412+
"aggs": {
413+
"dayOfWeek": {
414+
"terms": {
415+
"script": {
416+
"lang": "painless",
417+
"source": "doc['date'].value.dayOfWeek"
418+
}
419+
}
420+
}
421+
}
422+
}
423+
--------------------------------------------------
424+
// CONSOLE
425+
// TEST[setup:sales]
426+
427+
Response:
428+
429+
[source,js]
430+
--------------------------------------------------
431+
{
432+
...
433+
"aggregations": {
434+
"dayOfWeek": {
435+
"doc_count_error_upper_bound": 0,
436+
"sum_other_doc_count": 0,
437+
"buckets": [
438+
{
439+
"key": "7",
440+
"doc_count": 4
441+
},
442+
{
443+
"key": "4",
444+
"doc_count": 3
445+
}
446+
]
447+
}
448+
}
449+
}
450+
--------------------------------------------------
451+
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
452+
453+
The response will contain all the buckets having as key the relative day of
454+
the week: 1 for Monday, 2 for Tuesday... 7 for Sunday.

0 commit comments

Comments
 (0)