Skip to content

Commit c023546

Browse files
committed
Moving items if collection id is changed.
1 parent dca31ba commit c023546

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,53 @@ async def find_collection(self, collection_id: str) -> Collection:
734734

735735
return collection["_source"]
736736

737+
async def update_collection(
738+
self, collection_id: str, collection: Collection, refresh: bool = False
739+
):
740+
"""Update a collection from the database.
741+
742+
Args:
743+
self: The instance of the object calling this function.
744+
collection_id (str): The ID of the collection to be updated.
745+
collection (Collection): The Collection object to be updated.
746+
747+
Raises:
748+
NotFoundError: If the collection with the given `collection_id` is not
749+
found in the database.
750+
751+
Notes:
752+
This function updates the collection in the database using the specified
753+
`collection_id` and with the collection specified in the `Collection` object.
754+
If the collection is not found, a `NotFoundError` is raised.
755+
"""
756+
await self.find_collection(collection_id=collection_id)
757+
758+
if collection_id != collection["id"]:
759+
await self.create_collection(collection)
760+
761+
await self.client.reindex(
762+
body={
763+
"dest": {"index": f"items_{collection['id']}"},
764+
"source": {"index": f"items_{collection_id}"},
765+
"script": {
766+
"lang": "painless",
767+
"source": f"""ctx._source.collection = '{collection["id"]}'""",
768+
},
769+
},
770+
wait_for_completion=True,
771+
refresh=refresh,
772+
)
773+
774+
await self.delete_collection(collection_id)
775+
776+
else:
777+
await self.client.index(
778+
index=COLLECTIONS_INDEX,
779+
id=collection_id,
780+
document=collection,
781+
refresh=refresh,
782+
)
783+
737784
async def delete_collection(self, collection_id: str, refresh: bool = False):
738785
"""Delete a collection from the database.
739786

0 commit comments

Comments
 (0)