Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed

- Improved datetime query handling to only check start and end datetime values when datetime is None [#396](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/396)
- Optimize data_loader.py script [#395](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/395)

### Removed
Expand Down
186 changes: 81 additions & 105 deletions stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")],
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"]},
),
],
),
]

return search.query(Q("bool", should=should, minimum_should_match=1))

@staticmethod
def apply_bbox_filter(search: Search, bbox: List):
Expand Down
Loading