Skip to content

Upgrade stac-fastapi to v2.4.9 #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed

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

## [v1.0.0]

Expand Down
6 changes: 3 additions & 3 deletions stac_fastapi/elasticsearch/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"attrs",
"pydantic[dotenv]<2",
"stac_pydantic==2.0.*",
"stac-fastapi.types==2.4.8",
"stac-fastapi.api==2.4.8",
"stac-fastapi.extensions==2.4.8",
"stac-fastapi.types==2.4.9",
"stac-fastapi.api==2.4.9",
"stac-fastapi.extensions==2.4.9",
"elasticsearch[async]==8.11.0",
"elasticsearch-dsl==8.11.0",
"pystac[validation]",
Expand Down
16 changes: 12 additions & 4 deletions stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from stac_fastapi.elasticsearch.session import Session
from stac_fastapi.extensions.third_party.bulk_transactions import (
BaseBulkTransactionsClient,
BulkTransactionMethod,
Items,
)
from stac_fastapi.types import stac as stac_types
Expand Down Expand Up @@ -568,7 +569,7 @@ async def create_item(
if item["type"] == "FeatureCollection":
bulk_client = BulkTransactionsClient()
processed_items = [
bulk_client.preprocess_item(item, base_url) for item in item["features"] # type: ignore
bulk_client.preprocess_item(item, base_url, BulkTransactionMethod.INSERT) for item in item["features"] # type: ignore
]

await self.database.bulk_async(
Expand Down Expand Up @@ -718,17 +719,23 @@ def __attrs_post_init__(self):
settings = ElasticsearchSettings()
self.client = settings.create_client

def preprocess_item(self, item: stac_types.Item, base_url) -> stac_types.Item:
def preprocess_item(
self, item: stac_types.Item, base_url, method: BulkTransactionMethod
) -> stac_types.Item:
"""Preprocess an item to match the data model.

Args:
item: The item to preprocess.
base_url: The base URL of the request.
method: The bulk transaction method.

Returns:
The preprocessed item.
"""
return self.database.sync_prep_create_item(item=item, base_url=base_url)
exist_ok = method == BulkTransactionMethod.UPSERT
return self.database.sync_prep_create_item(
item=item, base_url=base_url, exist_ok=exist_ok
)

@overrides
def bulk_item_insert(
Expand All @@ -751,7 +758,8 @@ def bulk_item_insert(
base_url = ""

processed_items = [
self.preprocess_item(item, base_url) for item in items.items.values()
self.preprocess_item(item, base_url, items.method)
for item in items.items.values()
]

# not a great way to get the collection_id-- should be part of the method signature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,13 +591,16 @@ async def check_collection_exists(self, collection_id: str):
if not await self.client.exists(index=COLLECTIONS_INDEX, id=collection_id):
raise NotFoundError(f"Collection {collection_id} does not exist")

async def prep_create_item(self, item: Item, base_url: str) -> Item:
async def prep_create_item(
self, item: Item, base_url: str, exist_ok: bool = False
) -> Item:
"""
Preps an item for insertion into the database.

Args:
item (Item): The item to be prepped for insertion.
base_url (str): The base URL used to create the item's self URL.
exist_ok (bool): Indicates whether the item can exist already.

Returns:
Item: The prepped item.
Expand All @@ -608,7 +611,7 @@ async def prep_create_item(self, item: Item, base_url: str) -> Item:
"""
await self.check_collection_exists(collection_id=item["collection"])

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

return self.item_serializer.stac_to_db(item, base_url)

def sync_prep_create_item(self, item: Item, base_url: str) -> Item:
def sync_prep_create_item(
self, item: Item, base_url: str, exist_ok: bool = False
) -> Item:
"""
Prepare an item for insertion into the database.

This method performs pre-insertion preparation on the given `item`,
such as checking if the collection the item belongs to exists,
and verifying that an item with the same ID does not already exist in the database.
and optionally verifying that an item with the same ID does not already exist in the database.

Args:
item (Item): The item to be inserted into the database.
base_url (str): The base URL used for constructing URLs for the item.
exist_ok (bool): Indicates whether the item can exist already.

Returns:
Item: The item after preparation is done.
Expand All @@ -642,7 +648,7 @@ def sync_prep_create_item(self, item: Item, base_url: str) -> Item:
if not self.sync_client.exists(index=COLLECTIONS_INDEX, id=collection_id):
raise NotFoundError(f"Collection {collection_id} does not exist")

if self.sync_client.exists(
if not exist_ok and self.sync_client.exists(
index=index_by_collection_id(collection_id),
id=mk_item_id(item_id, collection_id),
):
Expand Down