Skip to content

Commit 30ea1c7

Browse files
committed
shared delete items index
1 parent 9ab8e99 commit 30ea1c7

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
apply_free_text_filter_shared,
2626
apply_intersects_filter_shared,
2727
create_index_templates_shared,
28+
delete_item_index_shared,
2829
populate_sort_shared,
2930
)
3031
from stac_fastapi.sfeos_helpers.mappings import (
@@ -99,18 +100,13 @@ async def delete_item_index(collection_id: str):
99100
100101
Args:
101102
collection_id (str): The ID of the collection whose items index will be deleted.
102-
"""
103-
client = AsyncElasticsearchSettings().create_client
104103
105-
name = index_alias_by_collection_id(collection_id)
106-
resolved = await client.indices.resolve_index(name=name)
107-
if "aliases" in resolved and resolved["aliases"]:
108-
[alias] = resolved["aliases"]
109-
await client.indices.delete_alias(index=alias["indices"], name=alias["name"])
110-
await client.indices.delete(index=alias["indices"])
111-
else:
112-
await client.indices.delete(index=name)
113-
await client.close()
104+
Notes:
105+
This function delegates to the shared implementation in delete_item_index_shared.
106+
"""
107+
await delete_item_index_shared(
108+
settings=AsyncElasticsearchSettings(), collection_id=collection_id
109+
)
114110

115111

116112
@attr.s

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
apply_free_text_filter_shared,
2626
apply_intersects_filter_shared,
2727
create_index_templates_shared,
28+
delete_item_index_shared,
2829
populate_sort_shared,
2930
)
3031
from stac_fastapi.sfeos_helpers.mappings import (
@@ -116,18 +117,13 @@ async def delete_item_index(collection_id: str) -> None:
116117
117118
Args:
118119
collection_id (str): The ID of the collection whose items index will be deleted.
119-
"""
120-
client = AsyncSearchSettings().create_client
121120
122-
name = index_alias_by_collection_id(collection_id)
123-
resolved = await client.indices.resolve_index(name=name)
124-
if "aliases" in resolved and resolved["aliases"]:
125-
[alias] = resolved["aliases"]
126-
await client.indices.delete_alias(index=alias["indices"], name=alias["name"])
127-
await client.indices.delete(index=alias["indices"])
128-
else:
129-
await client.indices.delete(index=name)
130-
await client.close()
121+
Notes:
122+
This function delegates to the shared implementation in delete_item_index_shared.
123+
"""
124+
await delete_item_index_shared(
125+
settings=AsyncSearchSettings(), collection_id=collection_id
126+
)
131127

132128

133129
@attr.s

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/database_logic_helpers.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
ES_ITEMS_SETTINGS,
1010
ITEMS_INDEX_PREFIX,
1111
Geometry,
12+
index_alias_by_collection_id,
1213
)
1314

1415

@@ -123,3 +124,31 @@ def populate_sort_shared(sortby: List) -> Optional[Dict[str, Dict[str, str]]]:
123124
return {s.field: {"order": s.direction} for s in sortby}
124125
else:
125126
return None
127+
128+
129+
async def delete_item_index_shared(settings: Any, collection_id: str) -> None:
130+
"""Delete the index for items in a collection.
131+
132+
Args:
133+
settings (Any): The settings object containing the client configuration.
134+
Must have a create_client attribute that returns an Elasticsearch/OpenSearch client.
135+
collection_id (str): The ID of the collection whose items index will be deleted.
136+
137+
Returns:
138+
None: This function doesn't return any value but deletes an item index in the database.
139+
140+
Notes:
141+
This function deletes an item index and its alias. It first resolves the alias to find
142+
the actual index name, then deletes both the alias and the index.
143+
"""
144+
client = settings.create_client
145+
146+
name = index_alias_by_collection_id(collection_id)
147+
resolved = await client.indices.resolve_index(name=name)
148+
if "aliases" in resolved and resolved["aliases"]:
149+
[alias] = resolved["aliases"]
150+
await client.indices.delete_alias(index=alias["indices"], name=alias["name"])
151+
await client.indices.delete(index=alias["indices"])
152+
else:
153+
await client.indices.delete(index=name)
154+
await client.close()

0 commit comments

Comments
 (0)