Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 7 additions & 2 deletions haystack/utils/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,17 @@ def _greater_than(value: Any, filter_value: Any, strict_datetime_comparison: boo


def _parse_date(value: str) -> datetime:
"""Try parsing the value as an ISO format date, then fall back to dateutil.parser."""
"""
Try parsing the value as an ISO format date, then fall back to dateutil.parser.

Partial dates (e.g. "2024" or "2024-06") are completed to the start of the period, so "2024" becomes
2024-01-01.
"""
try:
return datetime.fromisoformat(value)
except (ValueError, TypeError):
try:
return dateutil.parser.parse(value)
return dateutil.parser.parse(value, default=datetime(1, 1, 1))
Comment thread
sjrl marked this conversation as resolved.
Resolved
except (ValueError, TypeError) as exc:
msg = (
"Can't compare strings using operators '>', '>=', '<', '<='. "
Expand Down
7 changes: 7 additions & 0 deletions releasenotes/notes/fix-partial-date-filter-13338.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Fixed the ``>``, ``>=``, ``<`` and ``<=`` filter operators in ``InMemoryDocumentStore`` and
``MetadataRouter`` completing partial dates such as ``"2024"`` with the current month and day,
which silently dropped matching documents. Partial dates are now completed to the start of the
period, so ``"2024"`` becomes 2024-01-01.
20 changes: 20 additions & 0 deletions test/utils/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,19 @@
True,
id=">= operator with equal datetime in different ISO 8601 format",
),
pytest.param(
# Partial dates are resolved to the start of the period, not filled from the current date.
{"field": "meta.date", "operator": ">=", "value": "1969"},
Document(meta={"date": "1969-01-01"}),
True,
id=">= operator with partial year filter value matches start of the year",
),
pytest.param(
{"field": "meta.date", "operator": ">=", "value": "1969-07"},
Document(meta={"date": "1969-07-01"}),
True,
id=">= operator with partial year-month filter value matches start of the month",
),
pytest.param(
{"field": "meta.date", "operator": ">=", "value": "1969-07-21T20:17:40+00:00"},
Document(meta={"date": "1969-07-21T22:17:40+02:00"}),
Expand Down Expand Up @@ -277,6 +290,13 @@
False,
id="< operator with equal ISO 8601 datetime Document value",
),
pytest.param(
# Partial dates are resolved to the start of the period, not filled from the current date.
{"field": "meta.date", "operator": "<", "value": "1969"},
Document(meta={"date": "1969-12-31"}),
False,
id="< operator with partial year filter value excludes same year",
),
pytest.param(
{"field": "meta.date", "operator": "<", "value": "1969-07-21T20:17:40"},
Document(meta={"date": "1972-12-11T19:54:58"}),
Expand Down
Loading