Skip to content

Commit f4a98e3

Browse files
committed
fix error causing illegal_argument_exception
1 parent 9b2c7f2 commit f4a98e3

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
import asyncio
44
import logging
55
from base64 import urlsafe_b64decode, urlsafe_b64encode
6+
from collections.abc import Iterable
67
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
89

910
import attr
1011
import orjson
1112
from fastapi import HTTPException
1213
from opensearchpy import exceptions, helpers
1314
from opensearchpy.helpers.query import Q
1415
from opensearchpy.helpers.search import Search
15-
from starlette.requests import Request
16-
1716
from stac_fastapi.core.base_database_logic import BaseDatabaseLogic
1817
from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer
1918
from stac_fastapi.core.utilities import MAX_LIMIT, bbox2polygon
@@ -26,7 +25,6 @@
2625
AsyncOpensearchSettings as AsyncSearchSettings,
2726
)
2827
from stac_fastapi.opensearch.config import OpensearchSettings as SyncSearchSettings
29-
from stac_fastapi.sfeos_helpers import filter as filter_module
3028
from stac_fastapi.sfeos_helpers.database import (
3129
apply_free_text_filter_shared,
3230
apply_intersects_filter_shared,
@@ -66,6 +64,9 @@
6664
from stac_fastapi.types.errors import ConflictError, NotFoundError
6765
from stac_fastapi.types.links import resolve_links
6866
from stac_fastapi.types.stac import Collection, Item
67+
from starlette.requests import Request
68+
69+
from stac_fastapi.sfeos_helpers import filter as filter_module
6970

7071
logger = logging.getLogger(__name__)
7172

@@ -387,19 +388,17 @@ def apply_bbox_filter(search: Search, bbox: List):
387388
a geo_shape filter is added to the search object, set to intersect with the specified polygon.
388389
"""
389390
return search.filter(
390-
Q(
391-
{
392-
"geo_shape": {
393-
"geometry": {
394-
"shape": {
395-
"type": "polygon",
396-
"coordinates": bbox2polygon(*bbox),
397-
},
398-
"relation": "intersects",
399-
}
391+
Q({
392+
"geo_shape": {
393+
"geometry": {
394+
"shape": {
395+
"type": "polygon",
396+
"coordinates": bbox2polygon(*bbox),
397+
},
398+
"relation": "intersects",
400399
}
401400
}
402-
)
401+
})
403402
)
404403

405404
@staticmethod
@@ -679,14 +678,21 @@ async def async_prep_create_item(
679678
680679
"""
681680
await self.check_collection_exists(collection_id=item["collection"])
681+
alias = index_alias_by_collection_id(item["collection"])
682+
doc_id = mk_item_id(item["id"], item["collection"])
682683

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-
)
684+
if not exist_ok:
685+
alias_exists = await self.client.indices.exists_alias(name=alias)
686+
687+
if alias_exists:
688+
alias_info = await self.client.indices.get_alias(name=alias)
689+
indices = list(alias_info.keys())
690+
691+
for index in indices:
692+
if await self.client.exists(index=index, id=doc_id):
693+
raise ConflictError(
694+
f"Item {item['id']} in collection {item['collection']} already exists"
695+
)
690696

691697
return self.item_serializer.stac_to_db(item, base_url)
692698

@@ -903,7 +909,6 @@ async def json_patch_item(
903909
"add",
904910
"replace",
905911
]:
906-
907912
if operation.path == "collection" and collection_id != operation.value:
908913
await self.check_collection_exists(collection_id=operation.value)
909914
new_collection_id = operation.value
@@ -957,8 +962,8 @@ async def json_patch_item(
957962
"script": {
958963
"lang": "painless",
959964
"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
965+
f"""ctx._id = ctx._id.replace('{collection_id}', '{new_collection_id}');"""
966+
f"""ctx._source.collection = '{new_collection_id}';"""
962967
),
963968
},
964969
},
@@ -1180,7 +1185,7 @@ async def update_collection(
11801185
"source": {"index": f"{ITEMS_INDEX_PREFIX}{collection_id}"},
11811186
"script": {
11821187
"lang": "painless",
1183-
"source": f"""ctx._id = ctx._id.replace('{collection_id}', '{collection["id"]}'); ctx._source.collection = '{collection["id"]}' ;""", # noqa: E702
1188+
"source": f"""ctx._id = ctx._id.replace('{collection_id}', '{collection["id"]}'); ctx._source.collection = '{collection["id"]}' ;""",
11841189
},
11851190
},
11861191
wait_for_completion=True,

0 commit comments

Comments
 (0)