Skip to content

Commit b80cf30

Browse files
better queryables and illegal_argument_exception fix for OS items load (#427)
added missing copy(), updated DEFAULT_QUERYABLES so it contains only generic attributes **Description:** - Removed bug (missing copy()) causing default queryables to be enriched by queryables from previous queryables endpoint query. - Removed attributes cloud_cover, cloud_shadow_percentage, nodata_pixel_percentage since these are not generic for all collections ex. SAR data. - updated async_prep_create_item for OS item load so it works with multiple indices **PR Checklist:** - [x] Code is formatted and linted (run `pre-commit run --all-files`) - [ ] Tests pass (run `make test`) - [ ] Documentation has been updated to reflect changes, if applicable - [ ] Changes are added to the changelog --------- Co-authored-by: Jonathan Healy <[email protected]>
1 parent c5471c1 commit b80cf30

File tree

4 files changed

+28
-33
lines changed

4 files changed

+28
-33
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99

1010
### Changed
1111

12+
- Fixed a bug where missing `copy()` caused default queryables to be incorrectly enriched by results from previous queries. [#427](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/427)
13+
- Removed non-generic attributes (`cloud_cover`, `cloud_shadow_percentage`, `nodata_pixel_percentage`) not applicable to all collections (e.g., SAR data).[#427](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/427)
14+
- Updated `async_prep_create_item` to support OS item loading with multiple indices.[#427](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/427)
1215
- unified the type of queryables endpoint to `application/schema+json`. [#445](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/445)
1316
- updated `numReturned` & `numMatched` fields in itemCollection return to `numberReturned` & `numberMatched`. [#446](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/446)
1417

stac_fastapi/core/stac_fastapi/core/extensions/filter.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,6 @@
4141
"description": "Creation Timestamp",
4242
"$ref": "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/datetime.json#/properties/updated",
4343
},
44-
"cloud_cover": {
45-
"description": "Cloud Cover",
46-
"$ref": "https://stac-extensions.github.io/eo/v1.0.0/schema.json#/definitions/fields/properties/eo:cloud_cover",
47-
},
48-
"cloud_shadow_percentage": {
49-
"title": "Cloud Shadow Percentage",
50-
"description": "Cloud Shadow Percentage",
51-
"type": "number",
52-
"minimum": 0,
53-
"maximum": 100,
54-
},
55-
"nodata_pixel_percentage": {
56-
"title": "No Data Pixel Percentage",
57-
"description": "No Data Pixel Percentage",
58-
"type": "number",
59-
"minimum": 0,
60-
"maximum": 100,
61-
},
6244
}
6345
"""Queryables that are present in all collections."""
6446

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
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
@@ -679,14 +680,21 @@ async def async_prep_create_item(
679680
680681
"""
681682
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"])
682685

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+
)
690698

691699
return self.item_serializer.stac_to_db(item, base_url)
692700

@@ -903,7 +911,6 @@ async def json_patch_item(
903911
"add",
904912
"replace",
905913
]:
906-
907914
if operation.path == "collection" and collection_id != operation.value:
908915
await self.check_collection_exists(collection_id=operation.value)
909916
new_collection_id = operation.value
@@ -957,8 +964,8 @@ async def json_patch_item(
957964
"script": {
958965
"lang": "painless",
959966
"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}';"""
962969
),
963970
},
964971
},
@@ -1180,7 +1187,7 @@ async def update_collection(
11801187
"source": {"index": f"{ITEMS_INDEX_PREFIX}{collection_id}"},
11811188
"script": {
11821189
"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"]}' ;""",
11841191
},
11851192
},
11861193
wait_for_completion=True,

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/filter/client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Any, Dict, Optional, Tuple
55

66
import attr
7+
from fastapi import Request
78

89
from stac_fastapi.core.base_database_logic import BaseDatabaseLogic
910
from stac_fastapi.core.extensions.filter import ALL_QUERYABLES, DEFAULT_QUERYABLES
@@ -37,9 +38,11 @@ async def get_queryables(
3738
Returns:
3839
Dict[str, Any]: A dictionary containing the queryables for the given collection.
3940
"""
41+
request: Optional[Request] = kwargs.get("request")
42+
url_str: str = str(request.url) if request else ""
4043
queryables: Dict[str, Any] = {
41-
"$schema": "https://json-schema.org/draft/2019-09/schema",
42-
"$id": "https://stac-api.example.com/queryables",
44+
"$schema": "https://json-schema.org/draft-07/schema",
45+
"$id": f"{url_str}",
4346
"type": "object",
4447
"title": "Queryables for STAC API",
4548
"description": "Queryable names for the STAC API Item Search filter.",
@@ -49,7 +52,7 @@ async def get_queryables(
4952
if not collection_id:
5053
return queryables
5154

52-
properties: Dict[str, Any] = queryables["properties"]
55+
properties: Dict[str, Any] = queryables["properties"].copy()
5356
queryables.update(
5457
{
5558
"properties": properties,

0 commit comments

Comments
 (0)