Skip to content

Commit 149be4e

Browse files
committed
share queryables mapping
1 parent b956bae commit 149be4e

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
apply_intersects_filter_shared,
2727
create_index_templates_shared,
2828
delete_item_index_shared,
29+
get_queryables_mapping_shared,
2930
populate_sort_shared,
3031
)
3132
from stac_fastapi.sfeos_helpers.mappings import (
@@ -219,23 +220,12 @@ async def get_queryables_mapping(self, collection_id: str = "*") -> dict:
219220
Returns:
220221
dict: A dictionary containing the Queryables mappings.
221222
"""
222-
queryables_mapping = {}
223-
224223
mappings = await self.client.indices.get_mapping(
225224
index=f"{ITEMS_INDEX_PREFIX}{collection_id}",
226225
)
227-
228-
for mapping in mappings.values():
229-
fields = mapping["mappings"].get("properties", {})
230-
properties = fields.pop("properties", {}).get("properties", {}).keys()
231-
232-
for field_key in fields:
233-
queryables_mapping[field_key] = field_key
234-
235-
for property_key in properties:
236-
queryables_mapping[property_key] = f"properties.{property_key}"
237-
238-
return queryables_mapping
226+
return await get_queryables_mapping_shared(
227+
collection_id=collection_id, mappings=mappings
228+
)
239229

240230
@staticmethod
241231
def make_search():

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
apply_intersects_filter_shared,
2727
create_index_templates_shared,
2828
delete_item_index_shared,
29+
get_queryables_mapping_shared,
2930
populate_sort_shared,
3031
)
3132
from stac_fastapi.sfeos_helpers.mappings import (
@@ -238,23 +239,12 @@ async def get_queryables_mapping(self, collection_id: str = "*") -> dict:
238239
Returns:
239240
dict: A dictionary containing the Queryables mappings.
240241
"""
241-
queryables_mapping = {}
242-
243242
mappings = await self.client.indices.get_mapping(
244243
index=f"{ITEMS_INDEX_PREFIX}{collection_id}",
245244
)
246-
247-
for mapping in mappings.values():
248-
fields = mapping["mappings"].get("properties", {})
249-
properties = fields.pop("properties", {}).get("properties", {}).keys()
250-
251-
for field_key in fields:
252-
queryables_mapping[field_key] = field_key
253-
254-
for property_key in properties:
255-
queryables_mapping[property_key] = f"properties.{property_key}"
256-
257-
return queryables_mapping
245+
return await get_queryables_mapping_shared(
246+
collection_id=collection_id, mappings=mappings
247+
)
258248

259249
@staticmethod
260250
def make_search():

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/database_logic_helpers.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,35 @@ async def delete_item_index_shared(settings: Any, collection_id: str) -> None:
152152
else:
153153
await client.indices.delete(index=name)
154154
await client.close()
155+
156+
157+
async def get_queryables_mapping_shared(
158+
mappings: Dict[str, Dict[str, Any]], collection_id: str = "*"
159+
) -> Dict[str, str]:
160+
"""Retrieve mapping of Queryables for search.
161+
162+
Args:
163+
mappings (Dict[str, Dict[str, Any]]): The mapping information returned from
164+
Elasticsearch/OpenSearch client's indices.get_mapping() method.
165+
Expected structure is {index_name: {"mappings": {...}}}.
166+
collection_id (str, optional): The id of the Collection the Queryables
167+
belongs to. Defaults to "*".
168+
169+
Returns:
170+
Dict[str, str]: A dictionary containing the Queryables mappings, where keys are
171+
field names and values are the corresponding paths in the Elasticsearch/OpenSearch
172+
document structure.
173+
"""
174+
queryables_mapping = {}
175+
176+
for mapping in mappings.values():
177+
fields = mapping["mappings"].get("properties", {})
178+
properties = fields.pop("properties", {}).get("properties", {}).keys()
179+
180+
for field_key in fields:
181+
queryables_mapping[field_key] = field_key
182+
183+
for property_key in properties:
184+
queryables_mapping[property_key] = f"properties.{property_key}"
185+
186+
return queryables_mapping

0 commit comments

Comments
 (0)