Skip to content

Commit 1aede04

Browse files
committed
update methods
1 parent 8467be3 commit 1aede04

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ async def create_collection_index() -> None:
7979

8080
await client.options(ignore_status=400).indices.create(
8181
index=f"{COLLECTIONS_INDEX}-000001",
82-
aliases={COLLECTIONS_INDEX: {}},
82+
body={"aliases": {COLLECTIONS_INDEX: {}}},
8383
)
8484
await client.close()
8585

@@ -99,7 +99,7 @@ async def create_item_index(collection_id: str):
9999

100100
await client.options(ignore_status=400).indices.create(
101101
index=f"{index_by_collection_id(collection_id)}-000001",
102-
aliases={index_alias_by_collection_id(collection_id): {}},
102+
body={"aliases": {index_alias_by_collection_id(collection_id): {}}},
103103
)
104104
await client.close()
105105

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import attr
1111
from opensearchpy import exceptions, helpers
12-
from opensearchpy.exceptions import TransportError
1312
from opensearchpy.helpers.query import Q
1413
from opensearchpy.helpers.search import Search
1514
from starlette.requests import Request
@@ -80,24 +79,21 @@ async def create_collection_index() -> None:
8079
"""
8180
client = AsyncSearchSettings().create_client
8281

83-
search_body: Dict[str, Any] = {
84-
"aliases": {COLLECTIONS_INDEX: {}},
85-
}
86-
8782
index = f"{COLLECTIONS_INDEX}-000001"
8883

89-
try:
90-
await client.indices.create(index=index, body=search_body)
91-
except TransportError as e:
92-
if e.status_code == 400:
93-
pass # Ignore 400 status codes
94-
else:
95-
raise e
96-
84+
exists = await client.indices.exists(index=index)
85+
if not exists:
86+
await client.indices.create(
87+
index=index,
88+
body={
89+
"aliases": {COLLECTIONS_INDEX: {}},
90+
"mappings": ES_COLLECTIONS_MAPPINGS,
91+
},
92+
)
9793
await client.close()
9894

9995

100-
async def create_item_index(collection_id: str):
96+
async def create_item_index(collection_id: str) -> None:
10197
"""
10298
Create the index for Items. The settings of the index template will be used implicitly.
10399
@@ -109,24 +105,22 @@ async def create_item_index(collection_id: str):
109105
110106
"""
111107
client = AsyncSearchSettings().create_client
112-
search_body: Dict[str, Any] = {
113-
"aliases": {index_alias_by_collection_id(collection_id): {}},
114-
}
115108

116-
try:
109+
index_name = f"{index_by_collection_id(collection_id)}-000001"
110+
exists = await client.indices.exists(index=index_name)
111+
if not exists:
117112
await client.indices.create(
118-
index=f"{index_by_collection_id(collection_id)}-000001", body=search_body
113+
index=index_name,
114+
body={
115+
"aliases": {index_alias_by_collection_id(collection_id): {}},
116+
"mappings": ES_ITEMS_MAPPINGS,
117+
"settings": ES_ITEMS_SETTINGS,
118+
},
119119
)
120-
except TransportError as e:
121-
if e.status_code == 400:
122-
pass # Ignore 400 status codes
123-
else:
124-
raise e
125-
126120
await client.close()
127121

128122

129-
async def delete_item_index(collection_id: str):
123+
async def delete_item_index(collection_id: str) -> None:
130124
"""Delete the index for items in a collection.
131125
132126
Args:

0 commit comments

Comments
 (0)