11"""Search engine adapters for different implementations."""
22
33import uuid
4- from typing import Any , Dict
4+ from typing import Any , Dict , Literal , List
55
66from stac_fastapi .sfeos_helpers .database import (
77 index_alias_by_collection_id ,
@@ -39,26 +39,38 @@ async def create_simple_index(self, client: Any, collection_id: str) -> str:
3939 return index_name
4040
4141 async def create_datetime_index (
42- self , client : Any , collection_id : str , start_date : str
42+ self , client : Any , collection_id : str , start_datetime : str , datetime : str | None , end_datetime : str
4343 ) -> str :
4444 """Create a datetime-based index for the given collection.
4545
4646 Args:
4747 client: Search engine client instance.
4848 collection_id (str): Collection identifier.
49- start_date (str): Start date for the alias.
49+ start_datetime (str): Start datetime for the index alias.
50+ datetime (str | None): Datetime for the datetime alias (can be None).
51+ end_datetime (str): End datetime for the index alias.
5052
5153 Returns:
52- str: Created index alias name.
54+ str: Created start_datetime alias name.
5355 """
5456 index_name = self .create_index_name (collection_id )
55- alias_name = self .create_alias_name (collection_id , start_date )
57+ alias_start_date = self .create_alias_name (collection_id , "start_datetime" , start_datetime )
58+ alias_date = self .create_alias_name (collection_id , "datetime" , datetime )
59+ alias_end_date = self .create_alias_name (collection_id , "end_datetime" , end_datetime )
5660 collection_alias = index_alias_by_collection_id (collection_id )
61+
62+ aliases = {
63+ collection_alias : {},
64+ alias_start_date : {},
65+ alias_date : {},
66+ alias_end_date : {},
67+ }
68+
5769 await client .indices .create (
5870 index = index_name ,
59- body = self ._create_index_body ({ collection_alias : {}, alias_name : {}} ),
71+ body = self ._create_index_body (aliases ),
6072 )
61- return alias_name
73+ return alias_start_date
6274
6375 @staticmethod
6476 async def update_index_alias (client : Any , end_date : str , old_alias : str ) -> str :
@@ -84,23 +96,28 @@ async def update_index_alias(client: Any, end_date: str, old_alias: str) -> str:
8496 return new_alias
8597
8698 @staticmethod
87- async def change_alias_name (client : Any , old_alias : str , new_alias : str ) -> None :
88- """Change alias name from old to new.
99+ async def change_alias_name (client : Any , old_start_datetime_alias : str , aliases_to_change : List [ str ], aliases_to_create : List [ str ] ) -> None :
100+ """Change alias names by removing old aliases and adding new ones .
89101
90102 Args:
91103 client: Search engine client instance.
92- old_alias (str): Current alias name.
93- new_alias (str): New alias name.
104+ old_start_datetime_alias (str): Current start_datetime alias name to identify the index.
105+ aliases_to_change (List[str]): List of old alias names to remove.
106+ aliases_to_create (List[str]): List of new alias names to add.
94107
95108 Returns:
96109 None
97110 """
98- aliases_info = await client .indices .get_alias (name = old_alias )
111+ aliases_info = await client .indices .get_alias (name = old_start_datetime_alias )
112+ index_name = list (aliases_info .keys ())[0 ]
99113 actions = []
100114
101- for index_name in aliases_info . keys () :
115+ for old_alias in aliases_to_change :
102116 actions .append ({"remove" : {"index" : index_name , "alias" : old_alias }})
117+
118+ for new_alias in aliases_to_create :
103119 actions .append ({"add" : {"index" : index_name , "alias" : new_alias }})
120+
104121 await client .indices .update_aliases (body = {"actions" : actions })
105122
106123 @staticmethod
@@ -117,18 +134,21 @@ def create_index_name(collection_id: str) -> str:
117134 return f"{ ITEMS_INDEX_PREFIX } { cleaned .lower ()} _{ uuid .uuid4 ()} "
118135
119136 @staticmethod
120- def create_alias_name (collection_id : str , start_date : str ) -> str :
121- """Create index name from collection ID and uuid4.
137+ def create_alias_name (
138+ collection_id : str , name : Literal ["start_datetime" , "datetime" , "end_datetime" ], start_date : str
139+ ) -> str :
140+ """Create alias name from collection ID and date.
122141
123142 Args:
124143 collection_id (str): Collection identifier.
125- start_date (str): Start date for the alias.
144+ name (Literal["start_datetime", "datetime", "end_datetime"]): Type of alias to create.
145+ start_date (str): Date value for the alias.
126146
127147 Returns:
128- str: Alias name with initial date.
148+ str: Formatted alias name with prefix, type, collection ID, and date.
129149 """
130150 cleaned = collection_id .translate (_ES_INDEX_NAME_UNSUPPORTED_CHARS_TABLE )
131- return f"{ ITEMS_INDEX_PREFIX } { cleaned .lower ()} _{ start_date } "
151+ return f"{ ITEMS_INDEX_PREFIX } { name } _ { cleaned .lower ()} _{ start_date } "
132152
133153 @staticmethod
134154 def _create_index_body (aliases : Dict [str , Dict ]) -> Dict [str , Any ]:
@@ -148,19 +168,19 @@ def _create_index_body(aliases: Dict[str, Dict]) -> Dict[str, Any]:
148168
149169 @staticmethod
150170 async def find_latest_item_in_index (client : Any , index_name : str ) -> dict [str , Any ]:
151- """Find the latest item date in the specified index.
171+ """Find the latest item in the specified index.
152172
153173 Args:
154174 client: Search engine client instance.
155175 index_name (str): Name of the index to query.
156176
157177 Returns:
158- datetime: Date of the latest item in the index.
178+ dict[str, Any]: Latest item document from the index with metadata .
159179 """
160180 query = {
161181 "size" : 1 ,
162- "sort" : [{"properties.datetime " : {"order" : "desc" }}],
163- "_source" : ["properties.datetime" ],
182+ "sort" : [{"properties.start_datetime " : {"order" : "desc" }}],
183+ "_source" : ["properties.start_datetime" , "properties. datetime" , "properties.end_datetime " ],
164184 }
165185
166186 response = await client .search (index = index_name , body = query )
0 commit comments