Skip to content

Commit f14649f

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
add use_datetime to apply_datetime_filter
1 parent cf256e9 commit f14649f

File tree

1 file changed

+81
-54
lines changed

1 file changed

+81
-54
lines changed

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 81 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Database logic."""
2-
2+
from stac_fastapi.core.utilities import get_bool_env
33
import asyncio
44
import logging
55
from base64 import urlsafe_b64decode, urlsafe_b64encode
@@ -301,41 +301,22 @@ def apply_datetime_filter(
301301
if not datetime_search:
302302
return search, datetime_search
303303

304-
if "eq" in datetime_search:
305-
# For exact matches, include:
306-
# 1. Items with matching exact datetime
307-
# 2. Items with datetime:null where the time falls within their range
308-
should = [
309-
Q(
304+
# USE_DATETIME env var
305+
# True: Only search by datetime and ignore start/end datetime
306+
# False: Search by datetime, if null search by start/end datetime
307+
USE_DATETIME = get_bool_env("USE_DATETIME", default=False)
308+
309+
if USE_DATETIME:
310+
if "eq" in datetime_search:
311+
filter_query = Q(
310312
"bool",
311313
filter=[
312314
Q("exists", field="properties.datetime"),
313315
Q("term", **{"properties__datetime": datetime_search["eq"]}),
314316
],
315-
),
316-
Q(
317-
"bool",
318-
must_not=[Q("exists", field="properties.datetime")],
319-
filter=[
320-
Q("exists", field="properties.start_datetime"),
321-
Q("exists", field="properties.end_datetime"),
322-
Q(
323-
"range",
324-
properties__start_datetime={"lte": datetime_search["eq"]},
325-
),
326-
Q(
327-
"range",
328-
properties__end_datetime={"gte": datetime_search["eq"]},
329-
),
330-
],
331-
),
332-
]
333-
else:
334-
# For date ranges, include:
335-
# 1. Items with datetime in the range
336-
# 2. Items with datetime:null that overlap the search range
337-
should = [
338-
Q(
317+
)
318+
else:
319+
filter_query = Q(
339320
"bool",
340321
filter=[
341322
Q("exists", field="properties.datetime"),
@@ -347,29 +328,75 @@ def apply_datetime_filter(
347328
},
348329
),
349330
],
350-
),
351-
Q(
352-
"bool",
353-
must_not=[Q("exists", field="properties.datetime")],
354-
filter=[
355-
Q("exists", field="properties.start_datetime"),
356-
Q("exists", field="properties.end_datetime"),
357-
Q(
358-
"range",
359-
properties__start_datetime={"lte": datetime_search["lte"]},
360-
),
361-
Q(
362-
"range",
363-
properties__end_datetime={"gte": datetime_search["gte"]},
364-
),
365-
],
366-
),
367-
]
368-
369-
return (
370-
search.query(Q("bool", should=should, minimum_should_match=1)),
371-
datetime_search,
372-
)
331+
)
332+
return search.query(filter_query), datetime_search
333+
else:
334+
if "eq" in datetime_search:
335+
should = [
336+
Q(
337+
"bool",
338+
filter=[
339+
Q("exists", field="properties.datetime"),
340+
Q("term", **{"properties__datetime": datetime_search["eq"]}),
341+
],
342+
),
343+
Q(
344+
"bool",
345+
must_not=[Q("exists", field="properties.datetime")],
346+
filter=[
347+
Q("exists", field="properties.start_datetime"),
348+
Q("exists", field="properties.end_datetime"),
349+
Q(
350+
"range",
351+
properties__start_datetime={"lte": datetime_search["eq"]},
352+
),
353+
Q(
354+
"range",
355+
properties__end_datetime={"gte": datetime_search["eq"]},
356+
),
357+
],
358+
),
359+
]
360+
else:
361+
# For date ranges, include:
362+
# 1. Items with datetime in the range
363+
# 2. Items with datetime:null that overlap the search range
364+
should = [
365+
Q(
366+
"bool",
367+
filter=[
368+
Q("exists", field="properties.datetime"),
369+
Q(
370+
"range",
371+
properties__datetime={
372+
"gte": datetime_search["gte"],
373+
"lte": datetime_search["lte"],
374+
},
375+
),
376+
],
377+
),
378+
Q(
379+
"bool",
380+
must_not=[Q("exists", field="properties.datetime")],
381+
filter=[
382+
Q("exists", field="properties.start_datetime"),
383+
Q("exists", field="properties.end_datetime"),
384+
Q(
385+
"range",
386+
properties__start_datetime={"lte": datetime_search["lte"]},
387+
),
388+
Q(
389+
"range",
390+
properties__end_datetime={"gte": datetime_search["gte"]},
391+
),
392+
],
393+
),
394+
]
395+
396+
return (
397+
search.query(Q("bool", should=should, minimum_should_match=1)),
398+
datetime_search,
399+
)
373400

374401
@staticmethod
375402
def apply_bbox_filter(search: Search, bbox: List):

0 commit comments

Comments
 (0)