From bf1598d6e162a02195a7a38b28187670f20dc581 Mon Sep 17 00:00:00 2001 From: James Date: Fri, 2 Feb 2024 18:54:50 -0500 Subject: [PATCH] incorporate pr 177 --- CHANGELOG.md | 3 +- docker-compose.yml | 3 +- .../elasticsearch/config/config_opensearch.py | 9 ++++ .../stac_fastapi/elasticsearch/core.py | 1 - .../database_logic_opensearch.py | 47 +++++++++++++++++++ 5 files changed, 60 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e76fecef..36e46fdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added -- OpenSearch 2.11.1 support [#187](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/187) - Advanced comparison (LIKE, IN, BETWEEN) operators to the Filter extension [#178](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/178) +- Collection update endpoint no longer delete all sub items [#177](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/177) +- OpenSearch 2.11.1 support [#187](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/187) ### Changed diff --git a/docker-compose.yml b/docker-compose.yml index 2010cd08..916b9e82 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -43,7 +43,7 @@ services: - RELOAD=true - ENVIRONMENT=local - WEB_CONCURRENCY=10 - - ES_HOST=172.17.0.1 + - ES_HOST=opensearch - ES_PORT=9202 - ES_USE_SSL=false - ES_VERIFY_CERTS=false @@ -74,6 +74,7 @@ services: opensearch: container_name: os-container image: opensearchproject/opensearch:${OPENSEARCH_VERSION:-2.11.1} + hostname: opensearch environment: - discovery.type=single-node - plugins.security.disabled=true diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/config/config_opensearch.py b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/config/config_opensearch.py index 643b81ce..6ea49008 100644 --- a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/config/config_opensearch.py +++ b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/config/config_opensearch.py @@ -40,6 +40,15 @@ def _es_config() -> Dict[str, Any]: if (u := os.getenv("ES_USER")) and (p := os.getenv("ES_PASS")): config["http_auth"] = (u, p) + if api_key := os.getenv("ES_API_KEY"): + if isinstance(config["headers"], dict): + headers = {**config["headers"], "x-api-key": api_key} + + else: + config["headers"] = {"x-api-key": api_key} + + config["headers"] = headers + return config diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py index 202dff6c..ed6d7da9 100644 --- a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py +++ b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py @@ -112,7 +112,6 @@ async def all_collections(self, **kwargs) -> Collections: next_link = None if len(hits) == limit: last_hit = hits[-1] - logger.info(last_hit) next_search_after = last_hit["sort"] next_token = urlsafe_b64encode( ",".join(map(str, next_search_after)).encode() diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic/database_logic_opensearch.py b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic/database_logic_opensearch.py index bff62d80..6bc3ee95 100644 --- a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic/database_logic_opensearch.py +++ b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic/database_logic_opensearch.py @@ -771,6 +771,53 @@ async def find_collection(self, collection_id: str) -> Collection: raise NotFoundError(f"Collection {collection_id} not found") return collection["_source"] + + async def update_collection( + self, collection_id: str, collection: Collection, refresh: bool = False + ): + """Update a collection from the database. + + Args: + self: The instance of the object calling this function. + collection_id (str): The ID of the collection to be updated. + collection (Collection): The Collection object to be used for the update. + + Raises: + NotFoundError: If the collection with the given `collection_id` is not + found in the database. + + Notes: + This function updates the collection in the database using the specified + `collection_id` and with the collection specified in the `Collection` object. + If the collection is not found, a `NotFoundError` is raised. + """ + await self.find_collection(collection_id=collection_id) + + if collection_id != collection["id"]: + await self.create_collection(collection, refresh=refresh) + + await self.client.reindex( + body={ + "dest": {"index": f"{ITEMS_INDEX_PREFIX}{collection['id']}"}, + "source": {"index": f"{ITEMS_INDEX_PREFIX}{collection_id}"}, + "script": { + "lang": "painless", + "source": f"""ctx._id = ctx._id.replace('{collection_id}', '{collection["id"]}'); ctx._source.collection = '{collection["id"]}' ;""", + }, + }, + wait_for_completion=True, + refresh=refresh, + ) + + await self.delete_collection(collection_id) + + else: + await self.client.index( + index=COLLECTIONS_INDEX, + id=collection_id, + body=collection, + refresh=refresh, + ) async def delete_collection(self, collection_id: str, refresh: bool = False): """Delete a collection from the database.