Skip to content

Commit 4c92093

Browse files
authored
Merge branch 'main' into stac_to_db
2 parents 018078d + 03f3cba commit 4c92093

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/transactions.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def _create_item_index(self):
5353

5454
def create_item(self, model: stac_types.Item, **kwargs):
5555
"""Create item."""
56-
base_url = str(kwargs["request"].base_url)
56+
base_url = str(kwargs["request"].base_url
5757

5858
self._create_item_index()
5959

@@ -87,6 +87,7 @@ def create_collection(self, model: stac_types.Collection, **kwargs):
8787
self.client.index(
8888
index="stac_collections", doc_type="_doc", id=model["id"], document=model
8989
)
90+
return CollectionSerializer.db_to_stac(model, base_url)
9091

9192
def update_item(self, model: stac_types.Item, **kwargs):
9293
"""Update item."""
@@ -159,6 +160,13 @@ def _preprocess_item(self, model: stac_types.Item, base_url) -> stac_types.Item:
159160
item = ItemSerializer.stac_to_db(model, base_url)
160161
return item
161162

163+
def bulk_sync(self, processed_items):
164+
"""Elasticsearch bulk insertion."""
165+
actions = [
166+
{"_index": "stac_items", "_source": item} for item in processed_items
167+
]
168+
helpers.bulk(self.client, actions)
169+
162170
def bulk_item_insert(self, items: Items, **kwargs) -> str:
163171
"""Bulk item insertion using es."""
164172
transactions_client = TransactionsClient()
@@ -172,13 +180,6 @@ def bulk_item_insert(self, items: Items, **kwargs) -> str:
172180
]
173181
return_msg = f"Successfully added {len(processed_items)} items."
174182

175-
def bulk_sync(processed_items):
176-
actions = [
177-
{"_index": "stac_items", "_source": item} for item in processed_items
178-
]
179-
180-
helpers.bulk(self.client, actions)
181-
182-
bulk_sync(processed_items)
183+
self.bulk_sync(processed_items)
183184

184185
return return_msg

stac_fastapi/elasticsearch/tests/clients/test_elasticsearch.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,31 @@ def test_bulk_item_insert(
290290
# )
291291

292292

293+
def test_feature_collection_insert(
294+
es_core: CoreCrudClient,
295+
es_transactions: TransactionsClient,
296+
es_bulk_transactions: BulkTransactionsClient,
297+
load_test_data: Callable,
298+
):
299+
coll = load_test_data("test_collection.json")
300+
es_transactions.create_collection(coll, request=MockStarletteRequest)
301+
302+
item = load_test_data("test_item.json")
303+
304+
features = []
305+
for _ in range(10):
306+
_item = deepcopy(item)
307+
_item["id"] = str(uuid.uuid4())
308+
features.append(_item)
309+
310+
feature_collection = {"type": "FeatureCollection", "features": features}
311+
312+
es_transactions.create_item(feature_collection, request=MockStarletteRequest)
313+
time.sleep(3)
314+
fc = es_core.item_collection(coll["id"], request=MockStarletteRequest)
315+
assert len(fc["features"]) >= 10
316+
317+
293318
@pytest.mark.skip(reason="Not working")
294319
def test_landing_page_no_collection_title(
295320
es_core: CoreCrudClient,

0 commit comments

Comments
 (0)