Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from stac_fastapi.core.base_database_logic import BaseDatabaseLogic
from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer
from stac_fastapi.core.utilities import bbox2polygon, get_max_limit
from stac_fastapi.core.utilities import bbox2polygon, get_bool_env, get_max_limit
from stac_fastapi.elasticsearch.config import AsyncElasticsearchSettings
from stac_fastapi.elasticsearch.config import (
ElasticsearchSettings as SyncElasticsearchSettings,
Expand Down Expand Up @@ -289,26 +289,99 @@ def apply_datetime_filter(
Returns:
The filtered search object.
"""
# USE_DATETIME env var
# True: Search by datetime, if null search by start/end datetime
# False: Always search only by start/end datetime
USE_DATETIME = get_bool_env("USE_DATETIME", default=True)

datetime_search = return_date(datetime)

if not datetime_search:
return search, datetime_search

if "eq" in datetime_search:
# 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(
if USE_DATETIME:
if "eq" in datetime_search:
# 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:
# 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)),
datetime_search,
)
else:
if "eq" in datetime_search:
filter_query = Q(
"bool",
must_not=[Q("exists", field="properties.datetime")],
filter=[
Q("exists", field="properties.start_datetime"),
Q("exists", field="properties.end_datetime"),
Expand All @@ -321,29 +394,10 @@ def apply_datetime_filter(
properties__end_datetime={"gte": datetime_search["eq"]},
),
],
),
]
else:
# 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(
)
else:
filter_query = Q(
"bool",
must_not=[Q("exists", field="properties.datetime")],
filter=[
Q("exists", field="properties.start_datetime"),
Q("exists", field="properties.end_datetime"),
Expand All @@ -356,13 +410,8 @@ def apply_datetime_filter(
properties__end_datetime={"gte": datetime_search["gte"]},
),
],
),
]

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

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