-
Notifications
You must be signed in to change notification settings - Fork 25
Update datetime filter #396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5b7dd4e
75aee6e
ee0a7a7
6ecf35f
272d108
095f094
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -245,121 +245,97 @@ def apply_collections_filter(search: Search, collection_ids: List[str]): | |
@staticmethod | ||
def apply_datetime_filter( | ||
search: Search, interval: Optional[Union[DateTimeType, str]] | ||
): | ||
) -> Search: | ||
"""Apply a filter to search on datetime, start_datetime, and end_datetime fields. | ||
|
||
Args: | ||
search (Search): The search object to filter. | ||
interval: Optional[Union[DateTimeType, str]] | ||
search: The search object to filter. | ||
interval: Optional datetime interval to filter by. Can be: | ||
- A single datetime string (e.g., "2023-01-01T12:00:00") | ||
- A datetime range string (e.g., "2023-01-01/2023-12-31") | ||
- A datetime object | ||
- A tuple of (start_datetime, end_datetime) | ||
|
||
Returns: | ||
Search: The filtered search object. | ||
The filtered search object. | ||
""" | ||
if not interval: | ||
return search | ||
|
||
should = [] | ||
datetime_search = return_date(interval) | ||
try: | ||
datetime_search = return_date(interval) | ||
except (ValueError, TypeError) as e: | ||
# Handle invalid interval formats if return_date fails | ||
logger.error(f"Invalid interval format: {interval}, error: {e}") | ||
return search | ||
|
||
# If the request is a single datetime return | ||
# items with datetimes equal to the requested datetime OR | ||
# the requested datetime is between their start and end datetimes | ||
if "eq" in datetime_search: | ||
should.extend( | ||
[ | ||
Q( | ||
"bool", | ||
filter=[ | ||
Q( | ||
"term", | ||
properties__datetime=datetime_search["eq"], | ||
), | ||
], | ||
), | ||
Q( | ||
"bool", | ||
filter=[ | ||
Q( | ||
"range", | ||
properties__start_datetime={ | ||
"lte": datetime_search["eq"], | ||
}, | ||
), | ||
Q( | ||
"range", | ||
properties__end_datetime={ | ||
"gte": datetime_search["eq"], | ||
}, | ||
), | ||
], | ||
), | ||
] | ||
) | ||
|
||
# If the request is a date range return | ||
# items with datetimes within the requested date range OR | ||
# their startdatetime ithin the requested date range OR | ||
# their enddatetime ithin the requested date range OR | ||
# the requested daterange within their start and end datetimes | ||
# For exact matches, include: | ||
# 1. Items with matching exact datetime | ||
# 2. Items with datetime:null where the time falls within their range | ||
should = [ | ||
Q( | ||
"bool", | ||
filter=[ | ||
Q("exists", field="properties.datetime"), | ||
Q("term", **{"properties__datetime": datetime_search["eq"]}), | ||
], | ||
), | ||
Q( | ||
"bool", | ||
must_not=[Q("exists", field="properties.datetime")], | ||
filter=[ | ||
Q("exists", field="properties.start_datetime"), | ||
Q("exists", field="properties.end_datetime"), | ||
Q( | ||
"range", | ||
properties__start_datetime={"lte": datetime_search["eq"]}, | ||
), | ||
Q( | ||
"range", | ||
properties__end_datetime={"gte": datetime_search["eq"]}, | ||
), | ||
], | ||
), | ||
] | ||
else: | ||
should.extend( | ||
[ | ||
Q( | ||
"bool", | ||
filter=[ | ||
Q( | ||
"range", | ||
properties__datetime={ | ||
"gte": datetime_search["gte"], | ||
"lte": datetime_search["lte"], | ||
}, | ||
), | ||
], | ||
), | ||
Q( | ||
"bool", | ||
filter=[ | ||
Q( | ||
"range", | ||
properties__start_datetime={ | ||
"gte": datetime_search["gte"], | ||
"lte": datetime_search["lte"], | ||
}, | ||
), | ||
], | ||
), | ||
Q( | ||
"bool", | ||
filter=[ | ||
Q( | ||
"range", | ||
properties__end_datetime={ | ||
"gte": datetime_search["gte"], | ||
"lte": datetime_search["lte"], | ||
}, | ||
), | ||
], | ||
), | ||
Q( | ||
"bool", | ||
filter=[ | ||
Q( | ||
"range", | ||
properties__start_datetime={ | ||
"lte": datetime_search["gte"] | ||
}, | ||
), | ||
Q( | ||
"range", | ||
properties__end_datetime={ | ||
"gte": datetime_search["lte"] | ||
}, | ||
), | ||
], | ||
), | ||
] | ||
) | ||
|
||
search = search.query(Q("bool", filter=[Q("bool", should=should)])) | ||
|
||
return search | ||
# For date ranges, include: | ||
# 1. Items with datetime in the range | ||
# 2. Items with datetime:null that overlap the search range | ||
should = [ | ||
Q( | ||
"bool", | ||
filter=[ | ||
Q("exists", field="properties.datetime"), | ||
Q( | ||
"range", | ||
properties__datetime={ | ||
"gte": datetime_search["gte"], | ||
"lte": datetime_search["lte"], | ||
}, | ||
), | ||
], | ||
), | ||
Q( | ||
"bool", | ||
must_not=[Q("exists", field="properties.datetime")], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as below. |
||
filter=[ | ||
Q("exists", field="properties.start_datetime"), | ||
Q("exists", field="properties.end_datetime"), | ||
Q( | ||
"range", | ||
properties__start_datetime={"lte": datetime_search["lte"]}, | ||
), | ||
Q( | ||
"range", | ||
properties__end_datetime={"gte": datetime_search["gte"]}, | ||
), | ||
], | ||
Comment on lines
+323
to
+334
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this enough to give all possible combinations of datetime overlap? This looks like it will only return items whose date range entirely encapsulates the searched for date range. |
||
), | ||
] | ||
|
||
return search.query(Q("bool", should=should, minimum_should_match=1)) | ||
|
||
@staticmethod | ||
def apply_bbox_filter(search: Search, bbox: List): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current best practices recommends that you populate
datetime
even if you have a date range."The specification does allow one to set the datetime field to null, but it is strongly recommended to populate the single datetime field, as that is what many clients will search on. If it is at all possible to pick a nominal or representative datetime then that should be used."
So we should probably loosen the search (remove this line?) or update the recommended practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know. I think the best practices are recommended but may not always be relevant for all types of data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rhysrevans3 Can you look at this issue #396? I am not 100% sure on what the right approach should be.