| 
3 | 3 | import asyncio  | 
4 | 4 | import logging  | 
5 | 5 | from base64 import urlsafe_b64decode, urlsafe_b64encode  | 
 | 6 | +from collections.abc import Iterable  | 
6 | 7 | from copy import deepcopy  | 
7 |  | -from typing import Any, Dict, Iterable, List, Optional, Tuple, Type  | 
 | 8 | +from typing import Any, Dict, List, Optional, Tuple, Type  | 
8 | 9 | 
 
  | 
9 | 10 | import attr  | 
10 | 11 | import orjson  | 
@@ -679,14 +680,21 @@ async def async_prep_create_item(  | 
679 | 680 | 
  | 
680 | 681 |         """  | 
681 | 682 |         await self.check_collection_exists(collection_id=item["collection"])  | 
 | 683 | +        alias = index_alias_by_collection_id(item["collection"])  | 
 | 684 | +        doc_id = mk_item_id(item["id"], item["collection"])  | 
682 | 685 | 
 
  | 
683 |  | -        if not exist_ok and await self.client.exists(  | 
684 |  | -            index=index_alias_by_collection_id(item["collection"]),  | 
685 |  | -            id=mk_item_id(item["id"], item["collection"]),  | 
686 |  | -        ):  | 
687 |  | -            raise ConflictError(  | 
688 |  | -                f"Item {item['id']} in collection {item['collection']} already exists"  | 
689 |  | -            )  | 
 | 686 | +        if not exist_ok:  | 
 | 687 | +            alias_exists = await self.client.indices.exists_alias(name=alias)  | 
 | 688 | + | 
 | 689 | +            if alias_exists:  | 
 | 690 | +                alias_info = await self.client.indices.get_alias(name=alias)  | 
 | 691 | +                indices = list(alias_info.keys())  | 
 | 692 | + | 
 | 693 | +                for index in indices:  | 
 | 694 | +                    if await self.client.exists(index=index, id=doc_id):  | 
 | 695 | +                        raise ConflictError(  | 
 | 696 | +                            f"Item {item['id']} in collection {item['collection']} already exists"  | 
 | 697 | +                        )  | 
690 | 698 | 
 
  | 
691 | 699 |         return self.item_serializer.stac_to_db(item, base_url)  | 
692 | 700 | 
 
  | 
@@ -903,7 +911,6 @@ async def json_patch_item(  | 
903 | 911 |                 "add",  | 
904 | 912 |                 "replace",  | 
905 | 913 |             ]:  | 
906 |  | - | 
907 | 914 |                 if operation.path == "collection" and collection_id != operation.value:  | 
908 | 915 |                     await self.check_collection_exists(collection_id=operation.value)  | 
909 | 916 |                     new_collection_id = operation.value  | 
@@ -957,8 +964,8 @@ async def json_patch_item(  | 
957 | 964 |                     "script": {  | 
958 | 965 |                         "lang": "painless",  | 
959 | 966 |                         "source": (  | 
960 |  | -                            f"""ctx._id = ctx._id.replace('{collection_id}', '{new_collection_id}');"""  # noqa: E702  | 
961 |  | -                            f"""ctx._source.collection = '{new_collection_id}';"""  # noqa: E702  | 
 | 967 | +                            f"""ctx._id = ctx._id.replace('{collection_id}', '{new_collection_id}');"""  | 
 | 968 | +                            f"""ctx._source.collection = '{new_collection_id}';"""  | 
962 | 969 |                         ),  | 
963 | 970 |                     },  | 
964 | 971 |                 },  | 
@@ -1180,7 +1187,7 @@ async def update_collection(  | 
1180 | 1187 |                     "source": {"index": f"{ITEMS_INDEX_PREFIX}{collection_id}"},  | 
1181 | 1188 |                     "script": {  | 
1182 | 1189 |                         "lang": "painless",  | 
1183 |  | -                        "source": f"""ctx._id = ctx._id.replace('{collection_id}', '{collection["id"]}'); ctx._source.collection = '{collection["id"]}' ;""",  # noqa: E702  | 
 | 1190 | +                        "source": f"""ctx._id = ctx._id.replace('{collection_id}', '{collection["id"]}'); ctx._source.collection = '{collection["id"]}' ;""",  | 
1184 | 1191 |                     },  | 
1185 | 1192 |                 },  | 
1186 | 1193 |                 wait_for_completion=True,  | 
 | 
0 commit comments