Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions elasticsearch_dsl/faceted_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ def get_value_filter(self, filter_value):
)


def _date_interval_year(d):
return (d + timedelta(days=366)).replace(day=1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change this to return d.replace(year=d.year+1, day=(28 if d.month == 2 and d.day == 29 else d.day)) since years are linear unlike months.

We should add a test case for leap day handling as well (Basically see that February 29th, 2020 increments to February 28th, 2021)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Updated to return d.replace(year=d.year+1, day=(28 if d.month == 2 and d.day == 29 else d.day))



def _date_interval_month(d):
return (d + timedelta(days=32)).replace(day=1)

Expand All @@ -188,6 +192,8 @@ class DateHistogramFacet(Facet):
agg_type = "date_histogram"

DATE_INTERVALS = {
"year": _date_interval_year,
"1Y": _date_interval_year,
"month": _date_interval_month,
"1M": _date_interval_month,
"week": _date_interval_week,
Expand Down
4 changes: 4 additions & 0 deletions tests/test_faceted_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def test_date_histogram_facet_with_1970_01_01_date():
@pytest.mark.parametrize(
["interval_type", "interval"],
[
("interval", "year"),
("calendar_interval", "year"),
("interval", "month"),
("calendar_interval", "month"),
("interval", "week"),
Expand All @@ -160,6 +162,8 @@ def test_date_histogram_facet_with_1970_01_01_date():
("fixed_interval", "day"),
("interval", "hour"),
("fixed_interval", "hour"),
("interval", "1Y"),
("calendar_interval", "1Y"),
("interval", "1M"),
("calendar_interval", "1M"),
("interval", "1w"),
Expand Down