Skip to content

Commit

Permalink
Moving items if collection id is changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysrevans3 committed Dec 11, 2023
1 parent dca31ba commit c023546
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,53 @@ async def find_collection(self, collection_id: str) -> Collection:

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 updated.
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)

await self.client.reindex(
body={
"dest": {"index": f"items_{collection['id']}"},
"source": {"index": f"items_{collection_id}"},
"script": {
"lang": "painless",
"source": f"""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,
document=collection,
refresh=refresh,
)

async def delete_collection(self, collection_id: str, refresh: bool = False):
"""Delete a collection from the database.
Expand Down

0 comments on commit c023546

Please sign in to comment.