Skip to content

Commit 6d00f62

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
dummy
1 parent 56af26d commit 6d00f62

File tree

2 files changed

+111
-70
lines changed

2 files changed

+111
-70
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,4 @@ The system uses a precise naming convention:
634634
- Ensures fair resource allocation among all clients
635635
636636
- **Examples**: Implementation examples are available in the [examples/rate_limit](examples/rate_limit) directory.
637+

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 110 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from stac_fastapi.core.base_database_logic import BaseDatabaseLogic
1818
from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer
19-
from stac_fastapi.core.utilities import bbox2polygon, get_max_limit
19+
from stac_fastapi.core.utilities import bbox2polygon, get_bool_env, get_max_limit
2020
from stac_fastapi.extensions.core.transaction.request import (
2121
PartialCollection,
2222
PartialItem,
@@ -301,75 +301,115 @@ 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(
310-
"bool",
311-
filter=[
312-
Q("exists", field="properties.datetime"),
313-
Q("term", **{"properties__datetime": datetime_search["eq"]}),
314-
],
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(
339-
"bool",
340-
filter=[
341-
Q("exists", field="properties.datetime"),
342-
Q(
343-
"range",
344-
properties__datetime={
345-
"gte": datetime_search["gte"],
346-
"lte": datetime_search["lte"],
347-
},
348-
),
349-
],
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-
)
304+
# USE_DATETIME env var
305+
# True: Search by datetime, if null search by start/end datetime
306+
# False: Always search only by start/end datetime
307+
USE_DATETIME = get_bool_env("USE_DATETIME", default=True)
308+
309+
if USE_DATETIME:
310+
if "eq" in datetime_search:
311+
# For exact matches, include:
312+
# 1. Items with matching exact datetime
313+
# 2. Items with datetime:null where the time falls within their range
314+
should = [
315+
Q(
316+
"bool",
317+
filter=[
318+
Q("exists", field="properties.datetime"),
319+
Q("term", **{"properties__datetime": datetime_search["eq"]}),
320+
],
321+
),
322+
Q(
323+
"bool",
324+
must_not=[Q("exists", field="properties.datetime")],
325+
filter=[
326+
Q("exists", field="properties.start_datetime"),
327+
Q("exists", field="properties.end_datetime"),
328+
Q(
329+
"range",
330+
properties__start_datetime={"lte": datetime_search["eq"]},
331+
),
332+
Q(
333+
"range",
334+
properties__end_datetime={"gte": datetime_search["eq"]},
335+
),
336+
],
337+
),
338+
]
339+
else:
340+
# For date ranges, include:
341+
# 1. Items with datetime in the range
342+
# 2. Items with datetime:null that overlap the search range
343+
should = [
344+
Q(
345+
"bool",
346+
filter=[
347+
Q("exists", field="properties.datetime"),
348+
Q(
349+
"range",
350+
properties__datetime={
351+
"gte": datetime_search["gte"],
352+
"lte": datetime_search["lte"],
353+
},
354+
),
355+
],
356+
),
357+
Q(
358+
"bool",
359+
must_not=[Q("exists", field="properties.datetime")],
360+
filter=[
361+
Q("exists", field="properties.start_datetime"),
362+
Q("exists", field="properties.end_datetime"),
363+
Q(
364+
"range",
365+
properties__start_datetime={"lte": datetime_search["lte"]},
366+
),
367+
Q(
368+
"range",
369+
properties__end_datetime={"gte": datetime_search["gte"]},
370+
),
371+
],
372+
),
373+
]
374+
375+
return (
376+
search.query(Q("bool", should=should, minimum_should_match=1)),
377+
datetime_search,
378+
)
379+
# else:
380+
# if "eq" in datetime_search:
381+
# filter_query = Q(
382+
# "bool",
383+
# filter=[
384+
# Q("exists", field="properties.start_datetime"),
385+
# Q("exists", field="properties.end_datetime"),
386+
# Q(
387+
# "range",
388+
# properties__start_datetime={"lte": datetime_search["eq"]},
389+
# ),
390+
# Q(
391+
# "range",
392+
# properties__end_datetime={"gte": datetime_search["eq"]},
393+
# ),
394+
# ],
395+
# )
396+
# else:
397+
# filter_query = Q(
398+
# "bool",
399+
# filter=[
400+
# Q("exists", field="properties.start_datetime"),
401+
# Q("exists", field="properties.end_datetime"),
402+
# Q(
403+
# "range",
404+
# properties__start_datetime={"lte": datetime_search["lte"]},
405+
# ),
406+
# Q(
407+
# "range",
408+
# properties__end_datetime={"gte": datetime_search["gte"]},
409+
# ),
410+
# ],
411+
# )
412+
# return search.query(filter_query), datetime_search
373413

374414
@staticmethod
375415
def apply_bbox_filter(search: Search, bbox: List):

0 commit comments

Comments
 (0)