Skip to content

Commit 492d75c

Browse files
committed
start database logic
1 parent da97cf3 commit 492d75c

File tree

1 file changed

+69
-8
lines changed

1 file changed

+69
-8
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,40 @@
127127

128128

129129
def index_by_collection_id(collection_id: str) -> str:
130-
"""Translate a collection id into an ES index name."""
130+
"""
131+
Translate a collection id into an Elasticsearch index name.
132+
133+
Args:
134+
- collection_id (str): The collection id to translate into an index name.
135+
136+
Returns:
137+
- str: The index name derived from the collection id.
138+
"""
131139
return f"{ITEMS_INDEX_PREFIX}{collection_id}"
132140

133141

134142
def indices(collection_ids: Optional[List[str]]) -> str:
135-
"""Get a comma-separated string value of indexes for a given list of collection ids."""
143+
"""
144+
Get a comma-separated string of index names for a given list of collection ids.
145+
146+
Args:
147+
collection_ids: A list of collection ids.
148+
149+
Returns:
150+
A string of comma-separated index names. If `collection_ids` is None, returns the default indices.
151+
"""
136152
if collection_ids is None:
137153
return DEFAULT_INDICES
138154
else:
139155
return ",".join([f"{ITEMS_INDEX_PREFIX}{c.strip()}" for c in collection_ids])
140156

141157

142158
async def create_collection_index() -> None:
143-
"""Create the index for Collections."""
159+
"""Create the index for Collections in Elasticsearch.
160+
161+
This function creates the Elasticsearch index for the `Collections` with the predefined mapping.
162+
If the index already exists, the function ignores the error and continues execution.
163+
"""
144164
client = AsyncElasticsearchSettings().create_client
145165

146166
await client.indices.create(
@@ -152,7 +172,16 @@ async def create_collection_index() -> None:
152172

153173

154174
async def create_item_index(collection_id: str):
155-
"""Create the index for Items."""
175+
"""
176+
Create the index for Items.
177+
178+
Args:
179+
- collection_id (str): Collection identifier.
180+
181+
Returns:
182+
None
183+
184+
"""
156185
client = AsyncElasticsearchSettings().create_client
157186

158187
await client.indices.create(
@@ -165,25 +194,57 @@ async def create_item_index(collection_id: str):
165194

166195

167196
async def delete_item_index(collection_id: str):
168-
"""Delete the index for Items."""
197+
"""Delete the index for items in a collection.
198+
199+
Args:
200+
collection_id (str): The ID of the collection whose items index will be deleted.
201+
"""
169202
client = AsyncElasticsearchSettings().create_client
170203

171204
await client.indices.delete(index=index_by_collection_id(collection_id))
172205
await client.close()
173206

174207

175208
def bbox2polygon(b0: float, b1: float, b2: float, b3: float) -> List[List[List[float]]]:
176-
"""Transform bbox to polygon."""
209+
"""
210+
Transforms a bounding box represented by its four coordinates `b0`, `b1`, `b2`, and `b3` into a polygon.
211+
212+
:param b0: The x-coordinate of the lower-left corner of the bounding box.
213+
:param b1: The y-coordinate of the lower-left corner of the bounding box.
214+
:param b2: The x-coordinate of the upper-right corner of the bounding box.
215+
:param b3: The y-coordinate of the upper-right corner of the bounding box.
216+
:return: A polygon represented as a list of lists of coordinates.
217+
"""
177218
return [[[b0, b1], [b2, b1], [b2, b3], [b0, b3], [b0, b1]]]
178219

179220

180221
def mk_item_id(item_id: str, collection_id: str):
181-
"""Make the Elasticsearch document _id value from the Item id and collection."""
222+
"""Create the document id for an Item in Elasticsearch.
223+
224+
Args:
225+
item_id (str): The id of the Item.
226+
collection_id (str): The id of the Collection that the Item belongs to.
227+
228+
Returns:
229+
str: The document id for the Item, combining the Item id and the Collection id, separated by a `|` character.
230+
"""
182231
return f"{item_id}|{collection_id}"
183232

184233

185234
def mk_actions(collection_id: str, processed_items: List[Item]):
186-
"""Make the Elasticsearch bulk action for a list of Items."""
235+
"""Create Elasticsearch bulk actions for a list of processed items.
236+
237+
Args:
238+
collection_id (str): The identifier for the collection the items belong to.
239+
processed_items (List[Item]): The list of processed items to be bulk indexed.
240+
241+
Returns:
242+
List[Dict[str, Union[str, Dict]]]: The list of bulk actions to be executed,
243+
each action being a dictionary with the following keys:
244+
- `_index`: the index to store the document in.
245+
- `_id`: the document's identifier.
246+
- `_source`: the source of the document.
247+
"""
187248
return [
188249
{
189250
"_index": index_by_collection_id(collection_id),

0 commit comments

Comments
 (0)