Skip to content

Commit 8ed0928

Browse files
committed
Adding datetime extension to opensearch.
1 parent 7595509 commit 8ed0928

File tree

4 files changed

+110
-15
lines changed

4 files changed

+110
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
- use index templates for Collection and Item indices [#208](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/discussions/208)
1313
- Added API `title`, `version`, and `description` parameters from environment variables `STAC_FASTAPI_TITLE`, `STAC_FASTAPI_VERSION` and `STAC_FASTAPI_DESCRIPTION`, respectively. [#207](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/207)
1414
- Added a `STAC_FASTAPI_ROOT_PATH` environment variable to define the root path. Useful when working with an API gateway or load balancer. [#221](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/221)
15-
15+
- Extended Datetime Search to search on start_datetime and end_datetime as well as datetime fields. [#182](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/182)
1616

1717
### Changed
1818

@@ -62,8 +62,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
6262

6363
### Changed
6464

65-
- Extended Datetime Search to search on start_datetime and end_datetime as well as datetime fields. [#182](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/182)
66-
6765
- Elasticsearch drivers from 7.17.9 to 8.11.0 [#169](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/169)
6866
- Collection update endpoint no longer delete all sub items [#177](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/177)
6967

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ def apply_datetime_filter(search: Search, datetime_search: dict):
409409
"""
410410
should = []
411411

412+
# If the request is a single datetime return
413+
# items with datetimes equal to the requested datetime OR
414+
# the requested datetime is between their start and end datetimes
412415
if "eq" in datetime_search:
413416
should.extend(
414417
[
@@ -441,6 +444,11 @@ def apply_datetime_filter(search: Search, datetime_search: dict):
441444
]
442445
)
443446

447+
# If the request is a date range return
448+
# items with datetimes within the requested date range OR
449+
# their startdatetime ithin the requested date range OR
450+
# their enddatetime ithin the requested date range OR
451+
# the requested daterange within their start and end datetimes
444452
else:
445453
should.extend(
446454
[

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Database logic."""
2+
23
import asyncio
34
import logging
45
import os
@@ -425,7 +426,7 @@ def apply_collections_filter(search: Search, collection_ids: List[str]):
425426

426427
@staticmethod
427428
def apply_datetime_filter(search: Search, datetime_search):
428-
"""Apply a filter to search based on datetime field.
429+
"""Apply a filter to search based on datetime field, start_datetime, and end_datetime fields.
429430
430431
Args:
431432
search (Search): The search object to filter.
@@ -434,17 +435,109 @@ def apply_datetime_filter(search: Search, datetime_search):
434435
Returns:
435436
Search: The filtered search object.
436437
"""
438+
should = []
439+
440+
# If the request is a single datetime return
441+
# items with datetimes equal to the requested datetime OR
442+
# the requested datetime is between their start and end datetimes
437443
if "eq" in datetime_search:
438-
search = search.filter(
439-
"term", **{"properties__datetime": datetime_search["eq"]}
444+
should.extend(
445+
[
446+
Q(
447+
"bool",
448+
filter=[
449+
Q(
450+
"term",
451+
properties__datetime=datetime_search["eq"],
452+
),
453+
],
454+
),
455+
Q(
456+
"bool",
457+
filter=[
458+
Q(
459+
"range",
460+
properties__start_datetime={
461+
"lte": datetime_search["eq"],
462+
},
463+
),
464+
Q(
465+
"range",
466+
properties__end_datetime={
467+
"gte": datetime_search["eq"],
468+
},
469+
),
470+
],
471+
),
472+
]
440473
)
474+
475+
# If the request is a date range return
476+
# items with datetimes within the requested date range OR
477+
# their startdatetime ithin the requested date range OR
478+
# their enddatetime ithin the requested date range OR
479+
# the requested daterange within their start and end datetimes
441480
else:
442-
search = search.filter(
443-
"range", properties__datetime={"lte": datetime_search["lte"]}
444-
)
445-
search = search.filter(
446-
"range", properties__datetime={"gte": datetime_search["gte"]}
481+
should.extend(
482+
[
483+
Q(
484+
"bool",
485+
filter=[
486+
Q(
487+
"range",
488+
properties__datetime={
489+
"gte": datetime_search["gte"],
490+
"lte": datetime_search["lte"],
491+
},
492+
),
493+
],
494+
),
495+
Q(
496+
"bool",
497+
filter=[
498+
Q(
499+
"range",
500+
properties__start_datetime={
501+
"gte": datetime_search["gte"],
502+
"lte": datetime_search["lte"],
503+
},
504+
),
505+
],
506+
),
507+
Q(
508+
"bool",
509+
filter=[
510+
Q(
511+
"range",
512+
properties__end_datetime={
513+
"gte": datetime_search["gte"],
514+
"lte": datetime_search["lte"],
515+
},
516+
),
517+
],
518+
),
519+
Q(
520+
"bool",
521+
filter=[
522+
Q(
523+
"range",
524+
properties__start_datetime={
525+
"lte": datetime_search["gte"]
526+
},
527+
),
528+
Q(
529+
"range",
530+
properties__end_datetime={
531+
"gte": datetime_search["lte"]
532+
},
533+
),
534+
],
535+
),
536+
]
447537
)
538+
539+
search = search.query(Q("bool", filter=[Q("bool", should=should)]))
540+
448541
return search
449542

450543
@staticmethod

stac_fastapi/tests/api/test_api.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,6 @@ async def test_datetime_interval(app_client, txn_client, ctx):
491491

492492
await create_item(txn_client, third_item)
493493

494-
print("CREATED ITEMS")
495-
496494
dt_formats = [
497495
"2020-02-06T12:30:22+00:00/2020-02-13T12:30:22+00:00",
498496
"2020-02-12T12:30:22.00Z/2020-02-20T12:30:22.00Z",
@@ -565,8 +563,6 @@ async def test_datetime_bad_interval(app_client, txn_client, ctx):
565563

566564
await create_item(txn_client, third_item)
567565

568-
print("CREATED ITEMS")
569-
570566
dt_formats = [
571567
"1920-02-04T12:30:22+00:00/1920-02-06T12:30:22+00:00",
572568
"1920-02-04T12:30:22.00Z/1920-02-06T12:30:22.00Z",

0 commit comments

Comments
 (0)