Skip to content

Commit fc63010

Browse files
committed
Merge branch 'main' into no_indices
2 parents 03a9c76 + 03f3cba commit fc63010

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/transactions.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ def _create_item_index(self):
5454
def create_item(self, model: stac_types.Item, **kwargs):
5555
"""Create item."""
5656
base_url = str(kwargs["request"].base_url)
57+
self._create_item_index()
58+
59+
# If a feature collection is posted
60+
if model["type"] == "FeatureCollection":
61+
bulk_client = BulkTransactionsClient()
62+
processed_items = [
63+
bulk_client._preprocess_item(item, base_url)
64+
for item in model["features"]
65+
]
66+
return_msg = f"Successfully added {len(processed_items)} items."
67+
bulk_client.bulk_sync(processed_items)
68+
69+
return return_msg
70+
71+
# If a single item is posted
5772
item_links = ItemLinks(
5873
collection_id=model["collection"], item_id=model["id"], base_url=base_url
5974
).create_links()
@@ -79,8 +94,6 @@ def create_item(self, model: stac_types.Item, **kwargs):
7994
if "created" not in model["properties"]:
8095
model["properties"]["created"] = str(now)
8196

82-
self._create_item_index()
83-
8497
self.client.index(
8598
index="stac_items", doc_type="_doc", id=model["id"], document=model
8699
)
@@ -101,6 +114,7 @@ def create_collection(self, model: stac_types.Collection, **kwargs):
101114
self.client.index(
102115
index="stac_collections", doc_type="_doc", id=model["id"], document=model
103116
)
117+
return CollectionSerializer.db_to_stac(model, base_url)
104118

105119
def update_item(self, model: stac_types.Item, **kwargs):
106120
"""Update item."""
@@ -208,6 +222,13 @@ def _preprocess_item(self, model: stac_types.Item, base_url) -> stac_types.Item:
208222
wave.update({k: v})
209223
return model
210224

225+
def bulk_sync(self, processed_items):
226+
"""Elasticsearch bulk insertion."""
227+
actions = [
228+
{"_index": "stac_items", "_source": item} for item in processed_items
229+
]
230+
helpers.bulk(self.client, actions)
231+
211232
def bulk_item_insert(self, items: Items, **kwargs) -> str:
212233
"""Bulk item insertion using es."""
213234
self._create_item_index()
@@ -220,21 +241,6 @@ def bulk_item_insert(self, items: Items, **kwargs) -> str:
220241
]
221242
return_msg = f"Successfully added {len(processed_items)} items."
222243

223-
# helpers.bulk(
224-
# self.client,
225-
# processed_items,
226-
# index="stac_items",
227-
# doc_type="_doc",
228-
# request_timeout=200,
229-
# )
230-
231-
def bulk_sync(processed_items):
232-
actions = [
233-
{"_index": "stac_items", "_source": item} for item in processed_items
234-
]
235-
236-
helpers.bulk(self.client, actions)
237-
238-
bulk_sync(processed_items)
244+
self.bulk_sync(processed_items)
239245

240246
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)