Skip to content

Commit c82163c

Browse files
committed
cr
1 parent 8d393de commit c82163c

File tree

17 files changed

+2050
-1241
lines changed

17 files changed

+2050
-1241
lines changed

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ async def post_search(
830830

831831
datetime_parsed = format_datetime_range(date_str=search_request.datetime)
832832
try:
833-
search, datetime_search = self.database.apply_datetime_filter(
833+
search = self.database.apply_datetime_filter(
834834
search=search, datetime=datetime_parsed
835835
)
836836
except (ValueError, TypeError) as e:
@@ -910,7 +910,7 @@ async def post_search(
910910
token=token_param,
911911
sort=sort,
912912
collection_ids=getattr(search_request, "collections", None),
913-
datetime_search=datetime_search,
913+
datetime_search=datetime_parsed,
914914
)
915915

916916
fields = getattr(search_request, "fields", None)

stac_fastapi/elasticsearch/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ dependencies = [
3333
"elasticsearch[async]~=8.19.1",
3434
"uvicorn~=0.23.0",
3535
"starlette>=0.35.0,<0.36.0",
36-
"redis==6.4.0",
3736
]
3837

3938
[project.urls]

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ async def execute_search(
811811
token: Optional[str],
812812
sort: Optional[Dict[str, Dict[str, str]]],
813813
collection_ids: Optional[List[str]],
814-
datetime_search: Dict[str, Optional[str]],
814+
datetime_search: str,
815815
ignore_unavailable: bool = True,
816816
) -> Tuple[Iterable[Dict[str, Any]], Optional[int], Optional[str]]:
817817
"""Execute a search query with limit and other optional parameters.
@@ -822,7 +822,7 @@ async def execute_search(
822822
token (Optional[str]): The token used to return the next set of results.
823823
sort (Optional[Dict[str, Dict[str, str]]]): Specifies how the results should be sorted.
824824
collection_ids (Optional[List[str]]): The collection ids to search.
825-
datetime_search (Dict[str, Optional[str]]): Datetime range used for index selection.
825+
datetime_search (str): Datetime used for index selection.
826826
ignore_unavailable (bool, optional): Whether to ignore unavailable collections. Defaults to True.
827827
828828
Returns:
@@ -912,7 +912,7 @@ async def aggregate(
912912
geometry_geohash_grid_precision: int,
913913
geometry_geotile_grid_precision: int,
914914
datetime_frequency_interval: str,
915-
datetime_search,
915+
datetime_search: str,
916916
ignore_unavailable: Optional[bool] = True,
917917
):
918918
"""Return aggregations of STAC Items."""

stac_fastapi/opensearch/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ dependencies = [
3434
"opensearch-py[async]~=2.8.0",
3535
"uvicorn~=0.23.0",
3636
"starlette>=0.35.0,<0.36.0",
37-
"redis==6.4.0",
3837
]
3938

4039
[project.urls]

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ async def execute_search(
811811
token: Optional[str],
812812
sort: Optional[Dict[str, Dict[str, str]]],
813813
collection_ids: Optional[List[str]],
814-
datetime_search: Dict[str, Optional[str]],
814+
datetime_search: str,
815815
ignore_unavailable: bool = True,
816816
) -> Tuple[Iterable[Dict[str, Any]], Optional[int], Optional[str]]:
817817
"""Execute a search query with limit and other optional parameters.
@@ -822,7 +822,7 @@ async def execute_search(
822822
token (Optional[str]): The token used to return the next set of results.
823823
sort (Optional[Dict[str, Dict[str, str]]]): Specifies how the results should be sorted.
824824
collection_ids (Optional[List[str]]): The collection ids to search.
825-
datetime_search (Dict[str, Optional[str]]): Datetime range used for index selection.
825+
datetime_search (str): Datetime used for index selection.
826826
ignore_unavailable (bool, optional): Whether to ignore unavailable collections. Defaults to True.
827827
828828
Returns:
@@ -918,7 +918,7 @@ async def aggregate(
918918
geometry_geohash_grid_precision: int,
919919
geometry_geotile_grid_precision: int,
920920
datetime_frequency_interval: str,
921-
datetime_search,
921+
datetime_search: str,
922922
ignore_unavailable: Optional[bool] = True,
923923
):
924924
"""Return aggregations of STAC Items."""

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/aggregation/client.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,9 @@ async def aggregate(
313313
)
314314

315315
if aggregate_request.datetime:
316-
search, datetime_search = self.database.apply_datetime_filter(
316+
search = self.database.apply_datetime_filter(
317317
search=search, datetime=aggregate_request.datetime
318318
)
319-
else:
320-
datetime_search = {"gte": None, "lte": None}
321319

322320
if aggregate_request.bbox:
323321
bbox = aggregate_request.bbox
@@ -416,7 +414,7 @@ async def aggregate(
416414
geometry_geohash_grid_precision,
417415
geometry_geotile_grid_precision,
418416
datetime_frequency_interval,
419-
datetime_search,
417+
aggregate_request.datetime,
420418
)
421419
except Exception as error:
422420
if not isinstance(error, IndexError):

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/database/index.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def indices(collection_ids: Optional[List[str]]) -> str:
7171
def filter_indexes_by_datetime(
7272
collection_indexes: List[Tuple[Dict[str, str], ...]],
7373
datetime_search: Dict[str, Dict[str, Optional[str]]],
74+
use_datetime: bool,
7475
) -> List[str]:
7576
"""
7677
Filter Elasticsearch index aliases based on datetime search criteria.
@@ -85,6 +86,9 @@ def filter_indexes_by_datetime(
8586
datetime_search (Dict[str, Dict[str, Optional[str]]]): A dictionary with keys 'datetime',
8687
'start_datetime', and 'end_datetime', each containing 'gte' and 'lte' criteria as ISO format
8788
datetime strings or None.
89+
use_datetime (bool): Flag determining which datetime field to filter on:
90+
- True: Filters using 'datetime' alias.
91+
- False: Filters using 'start_datetime' and 'end_datetime' aliases.
8892
8993
Returns:
9094
List[str]: A list of start_datetime aliases that match all provided search criteria.
@@ -138,29 +142,30 @@ def check_criteria(
138142
end_datetime_alias = index_dict.get("end_datetime")
139143
datetime_alias = index_dict.get("datetime")
140144

141-
if not start_datetime_alias:
142-
continue
143-
144-
start_range = extract_date_from_alias(start_datetime_alias)
145-
end_date = extract_date_from_alias(end_datetime_alias)
146-
datetime_date = extract_date_from_alias(datetime_alias)
147-
148-
if not check_criteria(
149-
start_range[0], start_range[1], datetime_search.get("start_datetime", {})
150-
):
151-
continue
152-
if end_date:
145+
if start_datetime_alias:
146+
start_date = extract_date_from_alias(start_datetime_alias)
147+
if not check_criteria(
148+
start_date[0], start_date[1], datetime_search.get("start_datetime", {})
149+
):
150+
continue
151+
if end_datetime_alias:
152+
end_date = extract_date_from_alias(end_datetime_alias)
153153
if not check_criteria(
154154
end_date[0], end_date[1], datetime_search.get("end_datetime", {})
155155
):
156156
continue
157-
if datetime_date:
157+
if datetime_alias:
158+
datetime_date = extract_date_from_alias(datetime_alias)
158159
if not check_criteria(
159160
datetime_date[0], datetime_date[1], datetime_search.get("datetime", {})
160161
):
161162
continue
162163

163-
filtered_indexes.append(start_datetime_alias)
164+
primary_datetime_alias = (
165+
datetime_alias if use_datetime else start_datetime_alias
166+
)
167+
168+
filtered_indexes.append(primary_datetime_alias)
164169

165170
return filtered_indexes
166171

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/search_engine/index_operations.py

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import uuid
44
from typing import Any, Dict, List, Literal
55

6+
from stac_fastapi.core.utilities import get_bool_env
67
from stac_fastapi.sfeos_helpers.database import (
78
index_alias_by_collection_id,
89
index_by_collection_id,
@@ -18,6 +19,16 @@
1819
class IndexOperations:
1920
"""Base class for search engine adapters with common implementations."""
2021

22+
@property
23+
def use_datetime(self) -> bool:
24+
"""Get USE_DATETIME setting dynamically."""
25+
return get_bool_env("USE_DATETIME", default=True)
26+
27+
@property
28+
def primary_datetime_name(self) -> str:
29+
"""Get primary datetime field name based on current USE_DATETIME setting."""
30+
return "datetime" if self.use_datetime else "start_datetime"
31+
2132
async def create_simple_index(self, client: Any, collection_id: str) -> str:
2233
"""Create a simple index for the given collection.
2334
@@ -42,44 +53,48 @@ async def create_datetime_index(
4253
self,
4354
client: Any,
4455
collection_id: str,
45-
start_datetime: str,
46-
datetime: str,
47-
end_datetime: str,
56+
start_datetime: str | None,
57+
datetime: str | None,
58+
end_datetime: str | None,
4859
) -> str:
4960
"""Create a datetime-based index for the given collection.
5061
5162
Args:
5263
client: Search engine client instance.
5364
collection_id (str): Collection identifier.
54-
start_datetime (str): Start datetime for the index alias.
55-
datetime (str): Datetime for the datetime alias (can be None).
56-
end_datetime (str): End datetime for the index alias.
65+
start_datetime (str | None): Start datetime for the index alias.
66+
datetime (str | None): Datetime for the datetime alias.
67+
end_datetime (str | None): End datetime for the index alias.
5768
5869
Returns:
59-
str: Created start_datetime alias name.
70+
str: Created datetime alias name.
6071
"""
6172
index_name = self.create_index_name(collection_id)
62-
alias_start_date = self.create_alias_name(
63-
collection_id, "start_datetime", start_datetime
64-
)
65-
alias_date = self.create_alias_name(collection_id, "datetime", datetime)
66-
alias_end_date = self.create_alias_name(
67-
collection_id, "end_datetime", end_datetime
68-
)
6973
collection_alias = index_alias_by_collection_id(collection_id)
7074

7175
aliases: Dict[str, Any] = {
7276
collection_alias: {},
73-
alias_start_date: {},
74-
alias_date: {},
75-
alias_end_date: {},
7677
}
7778

79+
if start_datetime:
80+
alias_start_date = self.create_alias_name(
81+
collection_id, "start_datetime", start_datetime
82+
)
83+
alias_end_date = self.create_alias_name(
84+
collection_id, "end_datetime", end_datetime
85+
)
86+
aliases[alias_start_date] = {}
87+
aliases[alias_end_date] = {}
88+
created_alias = alias_start_date
89+
else:
90+
created_alias = self.create_alias_name(collection_id, "datetime", datetime)
91+
aliases[created_alias] = {}
92+
7893
await client.indices.create(
7994
index=index_name,
8095
body=self._create_index_body(aliases),
8196
)
82-
return alias_start_date
97+
return created_alias
8398

8499
@staticmethod
85100
async def update_index_alias(client: Any, end_date: str, old_alias: str) -> str:
@@ -126,12 +141,12 @@ async def change_alias_name(
126141
index_name = list(aliases_info.keys())[0]
127142
actions = []
128143

129-
for old_alias in aliases_to_change:
130-
actions.append({"remove": {"index": index_name, "alias": old_alias}})
131-
132144
for new_alias in aliases_to_create:
133145
actions.append({"add": {"index": index_name, "alias": new_alias}})
134146

147+
for old_alias in aliases_to_change:
148+
actions.append({"remove": {"index": index_name, "alias": old_alias}})
149+
135150
await client.indices.update_aliases(body={"actions": actions})
136151

137152
@staticmethod
@@ -182,8 +197,9 @@ def _create_index_body(aliases: Dict[str, Dict]) -> Dict[str, Any]:
182197
"settings": ES_ITEMS_SETTINGS,
183198
}
184199

185-
@staticmethod
186-
async def find_latest_item_in_index(client: Any, index_name: str) -> dict[str, Any]:
200+
async def find_latest_item_in_index(
201+
self, client: Any, index_name: str
202+
) -> dict[str, Any]:
187203
"""Find the latest item in the specified index.
188204
189205
Args:
@@ -195,7 +211,7 @@ async def find_latest_item_in_index(client: Any, index_name: str) -> dict[str, A
195211
"""
196212
query = {
197213
"size": 1,
198-
"sort": [{"properties.start_datetime": {"order": "desc"}}],
214+
"sort": [{f"properties.{self.primary_datetime_name}": {"order": "desc"}}],
199215
"_source": [
200216
"properties.start_datetime",
201217
"properties.datetime",

0 commit comments

Comments
 (0)