How to make weekday start on Sunday in date histogram aggregation

We need to have a date histogram aggregation with week interval, with week starting on Sunday instead of the Monday which Elasticsearch defaults to. I'm running Elasticsearch version 5.3.0.

I found this Github issue which added pre_offset and post_offset, but it does not seem to work for me: https://github.com/elastic/elasticsearch/issues/1599

Here is the aggs portion of my query:

    "aggs": {
        "a1": {
            "date_histogram": {
                "field": "postedDate",
                "interval": "week",
                "pre_offset": "-1d",
                "post_offset": "-1d",
                "order": {
                    "_key": "asc"
                }
            }
        }
    }

Running the query fails with:
Error: [illegal_argument_exception] [date_histogram] unknown field [pre_offset], parser not found

The query runs fine without the pre_offset/post_offset fields. Did I put those properties in the right place? They don't seem to be documented anywhere aside from the aforementioned GitHub issue. Is it still supported, and if not, how else can one set the week start to be Sunday instead of Monday?

Hello

Looking at https://github.com/zombodb/zombodb/issues/51, (I know it is not a ES repo but still valid here) which is referenced on the Github issue you mentioned, it looks that those options were not included or deprecated in favour of offset.

Additionally changes on the PR that closed the issue ou mentioned are no longer present on 5.3.0 https://github.com/elastic/elasticsearch/tree/v5.3.0/core/src/main/java/org/elasticsearch/common/joda

Finally looking at doc:
https://www.elastic.co/guide/en/elasticsearch/reference/5.3/search-aggregations-bucket-datehistogram-aggregation.html#_offset

You have the offset so you can do an offset of -1d and therefore it would go from sunday to sunday instead of from Monday to Monday. Does something like this make sense for you:

"aggs": {
        "a1": {
            "date_histogram": {
                "field": "postedDate",
                "interval": "week",
                "offset": "-1d",
                "order": {
                    "_key": "asc"
                }
            }
        }
    }
2 Likes

The offset works. Thanks!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.