Skip to content

Commit 61c01cb

Browse files
authored
Merge pull request #172 from StijnCaerts/stac_fastapi_2.4.9
Upgrade stac-fastapi to v2.4.9
2 parents 2570285 + 37b508f commit 61c01cb

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1616
### Fixed
1717

1818
- Exclude unset fields in search response [#166](https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/166)
19+
- Upgrade stac-fastapi to v2.4.9 [#172](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/172)
1920

2021
## [v1.0.0]
2122

stac_fastapi/elasticsearch/setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"attrs",
1111
"pydantic[dotenv]<2",
1212
"stac_pydantic==2.0.*",
13-
"stac-fastapi.types==2.4.8",
14-
"stac-fastapi.api==2.4.8",
15-
"stac-fastapi.extensions==2.4.8",
13+
"stac-fastapi.types==2.4.9",
14+
"stac-fastapi.api==2.4.9",
15+
"stac-fastapi.extensions==2.4.9",
1616
"elasticsearch[async]==8.11.0",
1717
"elasticsearch-dsl==8.11.0",
1818
"pystac[validation]",

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from stac_fastapi.elasticsearch.session import Session
2727
from stac_fastapi.extensions.third_party.bulk_transactions import (
2828
BaseBulkTransactionsClient,
29+
BulkTransactionMethod,
2930
Items,
3031
)
3132
from stac_fastapi.types import stac as stac_types
@@ -568,7 +569,7 @@ async def create_item(
568569
if item["type"] == "FeatureCollection":
569570
bulk_client = BulkTransactionsClient()
570571
processed_items = [
571-
bulk_client.preprocess_item(item, base_url) for item in item["features"] # type: ignore
572+
bulk_client.preprocess_item(item, base_url, BulkTransactionMethod.INSERT) for item in item["features"] # type: ignore
572573
]
573574

574575
await self.database.bulk_async(
@@ -718,17 +719,23 @@ def __attrs_post_init__(self):
718719
settings = ElasticsearchSettings()
719720
self.client = settings.create_client
720721

721-
def preprocess_item(self, item: stac_types.Item, base_url) -> stac_types.Item:
722+
def preprocess_item(
723+
self, item: stac_types.Item, base_url, method: BulkTransactionMethod
724+
) -> stac_types.Item:
722725
"""Preprocess an item to match the data model.
723726
724727
Args:
725728
item: The item to preprocess.
726729
base_url: The base URL of the request.
730+
method: The bulk transaction method.
727731
728732
Returns:
729733
The preprocessed item.
730734
"""
731-
return self.database.sync_prep_create_item(item=item, base_url=base_url)
735+
exist_ok = method == BulkTransactionMethod.UPSERT
736+
return self.database.sync_prep_create_item(
737+
item=item, base_url=base_url, exist_ok=exist_ok
738+
)
732739

733740
@overrides
734741
def bulk_item_insert(
@@ -751,7 +758,8 @@ def bulk_item_insert(
751758
base_url = ""
752759

753760
processed_items = [
754-
self.preprocess_item(item, base_url) for item in items.items.values()
761+
self.preprocess_item(item, base_url, items.method)
762+
for item in items.items.values()
755763
]
756764

757765
# not a great way to get the collection_id-- should be part of the method signature

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,13 +591,16 @@ async def check_collection_exists(self, collection_id: str):
591591
if not await self.client.exists(index=COLLECTIONS_INDEX, id=collection_id):
592592
raise NotFoundError(f"Collection {collection_id} does not exist")
593593

594-
async def prep_create_item(self, item: Item, base_url: str) -> Item:
594+
async def prep_create_item(
595+
self, item: Item, base_url: str, exist_ok: bool = False
596+
) -> Item:
595597
"""
596598
Preps an item for insertion into the database.
597599
598600
Args:
599601
item (Item): The item to be prepped for insertion.
600602
base_url (str): The base URL used to create the item's self URL.
603+
exist_ok (bool): Indicates whether the item can exist already.
601604
602605
Returns:
603606
Item: The prepped item.
@@ -608,7 +611,7 @@ async def prep_create_item(self, item: Item, base_url: str) -> Item:
608611
"""
609612
await self.check_collection_exists(collection_id=item["collection"])
610613

611-
if await self.client.exists(
614+
if not exist_ok and await self.client.exists(
612615
index=index_by_collection_id(item["collection"]),
613616
id=mk_item_id(item["id"], item["collection"]),
614617
):
@@ -618,17 +621,20 @@ async def prep_create_item(self, item: Item, base_url: str) -> Item:
618621

619622
return self.item_serializer.stac_to_db(item, base_url)
620623

621-
def sync_prep_create_item(self, item: Item, base_url: str) -> Item:
624+
def sync_prep_create_item(
625+
self, item: Item, base_url: str, exist_ok: bool = False
626+
) -> Item:
622627
"""
623628
Prepare an item for insertion into the database.
624629
625630
This method performs pre-insertion preparation on the given `item`,
626631
such as checking if the collection the item belongs to exists,
627-
and verifying that an item with the same ID does not already exist in the database.
632+
and optionally verifying that an item with the same ID does not already exist in the database.
628633
629634
Args:
630635
item (Item): The item to be inserted into the database.
631636
base_url (str): The base URL used for constructing URLs for the item.
637+
exist_ok (bool): Indicates whether the item can exist already.
632638
633639
Returns:
634640
Item: The item after preparation is done.
@@ -642,7 +648,7 @@ def sync_prep_create_item(self, item: Item, base_url: str) -> Item:
642648
if not self.sync_client.exists(index=COLLECTIONS_INDEX, id=collection_id):
643649
raise NotFoundError(f"Collection {collection_id} does not exist")
644650

645-
if self.sync_client.exists(
651+
if not exist_ok and self.sync_client.exists(
646652
index=index_by_collection_id(collection_id),
647653
id=mk_item_id(item_id, collection_id),
648654
):

0 commit comments

Comments
 (0)