|
1 |
| -"""Shared code for elasticsearch/ opensearch database logic.""" |
| 1 | +"""Shared code for elasticsearch/ opensearch database logic. |
| 2 | +
|
| 3 | +This module contains shared functions used by both the Elasticsearch and OpenSearch |
| 4 | +implementations of STAC FastAPI for database operations. It helps reduce code duplication |
| 5 | +and ensures consistent behavior between the two implementations. |
| 6 | +
|
| 7 | +The sfeos_helpers package is organized as follows: |
| 8 | +- database_logic_helpers.py: Shared database operations (this file) |
| 9 | +- filter.py: Shared filter extension implementation |
| 10 | +- mappings.py: Shared constants and mapping definitions |
| 11 | +- utilities.py: Shared utility functions |
| 12 | +
|
| 13 | +When adding new functionality to this package, consider: |
| 14 | +1. Will this code be used by both Elasticsearch and OpenSearch implementations? |
| 15 | +2. Is the functionality stable and unlikely to diverge between implementations? |
| 16 | +3. Is the function well-documented with clear input/output contracts? |
| 17 | +
|
| 18 | +Function Naming Conventions: |
| 19 | +- All shared functions should end with `_shared` to clearly indicate they're meant to be used by both implementations |
| 20 | +- Function names should be descriptive and indicate their purpose |
| 21 | +- Parameter names should be consistent across similar functions |
| 22 | +""" |
2 | 23 |
|
3 | 24 | from typing import Any, Dict, List, Optional
|
4 | 25 |
|
|
12 | 33 | )
|
13 | 34 | from stac_fastapi.sfeos_helpers.utilities import index_alias_by_collection_id
|
14 | 35 |
|
| 36 | +# ============================================================================ |
| 37 | +# Index Management Functions |
| 38 | +# ============================================================================ |
| 39 | + |
15 | 40 |
|
16 | 41 | async def create_index_templates_shared(settings: Any) -> None:
|
17 | 42 | """Create index templates for Elasticsearch/OpenSearch Collection and Item indices.
|
@@ -49,6 +74,39 @@ async def create_index_templates_shared(settings: Any) -> None:
|
49 | 74 | await client.close()
|
50 | 75 |
|
51 | 76 |
|
| 77 | +async def delete_item_index_shared(settings: Any, collection_id: str) -> None: |
| 78 | + """Delete the index for items in a collection. |
| 79 | +
|
| 80 | + Args: |
| 81 | + settings (Any): The settings object containing the client configuration. |
| 82 | + Must have a create_client attribute that returns an Elasticsearch/OpenSearch client. |
| 83 | + collection_id (str): The ID of the collection whose items index will be deleted. |
| 84 | +
|
| 85 | + Returns: |
| 86 | + None: This function doesn't return any value but deletes an item index in the database. |
| 87 | +
|
| 88 | + Notes: |
| 89 | + This function deletes an item index and its alias. It first resolves the alias to find |
| 90 | + the actual index name, then deletes both the alias and the index. |
| 91 | + """ |
| 92 | + client = settings.create_client |
| 93 | + |
| 94 | + name = index_alias_by_collection_id(collection_id) |
| 95 | + resolved = await client.indices.resolve_index(name=name) |
| 96 | + if "aliases" in resolved and resolved["aliases"]: |
| 97 | + [alias] = resolved["aliases"] |
| 98 | + await client.indices.delete_alias(index=alias["indices"], name=alias["name"]) |
| 99 | + await client.indices.delete(index=alias["indices"]) |
| 100 | + else: |
| 101 | + await client.indices.delete(index=name) |
| 102 | + await client.close() |
| 103 | + |
| 104 | + |
| 105 | +# ============================================================================ |
| 106 | +# Query Building Functions |
| 107 | +# ============================================================================ |
| 108 | + |
| 109 | + |
52 | 110 | def apply_free_text_filter_shared(
|
53 | 111 | search: Any, free_text_queries: Optional[List[str]]
|
54 | 112 | ) -> Any:
|
@@ -126,32 +184,9 @@ def populate_sort_shared(sortby: List) -> Optional[Dict[str, Dict[str, str]]]:
|
126 | 184 | return None
|
127 | 185 |
|
128 | 186 |
|
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() |
| 187 | +# ============================================================================ |
| 188 | +# Mapping Functions |
| 189 | +# ============================================================================ |
155 | 190 |
|
156 | 191 |
|
157 | 192 | async def get_queryables_mapping_shared(
|
|
0 commit comments