Skip to content

Commit 6b01898

Browse files
committed
implement the use of index templates (#208)
1 parent 666f980 commit 6b01898

File tree

7 files changed

+114
-12
lines changed

7 files changed

+114
-12
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from stac_fastapi.elasticsearch.database_logic import (
1515
DatabaseLogic,
1616
create_collection_index,
17+
create_index_templates,
1718
)
1819
from stac_fastapi.extensions.core import (
1920
ContextExtension,
@@ -73,6 +74,7 @@
7374

7475
@app.on_event("startup")
7576
async def _startup_event() -> None:
77+
await create_index_templates()
7678
await create_collection_index()
7779

7880

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,36 @@ def indices(collection_ids: Optional[List[str]]) -> str:
171171
return ",".join([index_by_collection_id(c) for c in collection_ids])
172172

173173

174+
async def create_index_templates() -> None:
175+
"""
176+
Create index templates for the Collection and Item indices.
177+
178+
Returns:
179+
None
180+
181+
"""
182+
client = AsyncElasticsearchSettings().create_client
183+
await client.indices.put_template(
184+
name=f"template_{COLLECTIONS_INDEX}",
185+
body={
186+
"index_patterns": [f"{COLLECTIONS_INDEX}*"],
187+
"mappings": ES_COLLECTIONS_MAPPINGS,
188+
},
189+
)
190+
await client.indices.put_template(
191+
name=f"template_{ITEMS_INDEX_PREFIX}",
192+
body={
193+
"index_patterns": [f"{ITEMS_INDEX_PREFIX}*"],
194+
"settings": ES_ITEMS_SETTINGS,
195+
"mappings": ES_ITEMS_MAPPINGS,
196+
},
197+
)
198+
await client.close()
199+
200+
174201
async def create_collection_index() -> None:
175202
"""
176-
Create the index for a Collection.
203+
Create the index for a Collection. The settings of the index template will be used implicitly.
177204
178205
Returns:
179206
None
@@ -184,14 +211,13 @@ async def create_collection_index() -> None:
184211
await client.options(ignore_status=400).indices.create(
185212
index=f"{COLLECTIONS_INDEX}-000001",
186213
aliases={COLLECTIONS_INDEX: {}},
187-
mappings=ES_COLLECTIONS_MAPPINGS,
188214
)
189215
await client.close()
190216

191217

192218
async def create_item_index(collection_id: str):
193219
"""
194-
Create the index for Items.
220+
Create the index for Items. The settings of the index template will be used implicitly.
195221
196222
Args:
197223
collection_id (str): Collection identifier.
@@ -206,8 +232,6 @@ async def create_item_index(collection_id: str):
206232
await client.options(ignore_status=400).indices.create(
207233
index=f"{index_by_collection_id(collection_id)}-000001",
208234
aliases={index_name: {}},
209-
mappings=ES_ITEMS_MAPPINGS,
210-
settings=ES_ITEMS_SETTINGS,
211235
)
212236
await client.close()
213237

stac_fastapi/opensearch/stac_fastapi/opensearch/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from stac_fastapi.opensearch.database_logic import (
2424
DatabaseLogic,
2525
create_collection_index,
26+
create_index_templates,
2627
)
2728

2829
settings = OpensearchSettings()
@@ -73,6 +74,7 @@
7374

7475
@app.on_event("startup")
7576
async def _startup_event() -> None:
77+
await create_index_templates()
7678
await create_collection_index()
7779

7880

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,44 @@ def indices(collection_ids: Optional[List[str]]) -> str:
173173
return ",".join([index_by_collection_id(c) for c in collection_ids])
174174

175175

176+
async def create_index_templates() -> None:
177+
"""
178+
Create index templates for the Collection and Item indices.
179+
180+
Returns:
181+
None
182+
183+
"""
184+
client = AsyncSearchSettings().create_client
185+
await client.indices.put_template(
186+
name=f"template_{COLLECTIONS_INDEX}",
187+
body={
188+
"index_patterns": [f"{COLLECTIONS_INDEX}*"],
189+
"mappings": ES_COLLECTIONS_MAPPINGS,
190+
},
191+
)
192+
await client.indices.put_template(
193+
name=f"template_{ITEMS_INDEX_PREFIX}",
194+
body={
195+
"index_patterns": [f"{ITEMS_INDEX_PREFIX}*"],
196+
"settings": ES_ITEMS_SETTINGS,
197+
"mappings": ES_ITEMS_MAPPINGS,
198+
},
199+
)
200+
await client.close()
201+
202+
176203
async def create_collection_index() -> None:
177204
"""
178-
Create the index for a Collection.
205+
Create the index for a Collection. The settings of the index template will be used implicitly.
179206
180207
Returns:
181208
None
182209
183210
"""
184211
client = AsyncSearchSettings().create_client
185212

186-
search_body = {
187-
"mappings": ES_COLLECTIONS_MAPPINGS,
213+
search_body: dict[str, Any] = {
188214
"aliases": {COLLECTIONS_INDEX: {}},
189215
}
190216

@@ -203,7 +229,7 @@ async def create_collection_index() -> None:
203229

204230
async def create_item_index(collection_id: str):
205231
"""
206-
Create the index for Items.
232+
Create the index for Items. The settings of the index template will be used implicitly.
207233
208234
Args:
209235
collection_id (str): Collection identifier.
@@ -214,10 +240,8 @@ async def create_item_index(collection_id: str):
214240
"""
215241
client = AsyncSearchSettings().create_client
216242
index_name = index_by_collection_id(collection_id)
217-
search_body = {
243+
search_body: dict[str, Any] = {
218244
"aliases": {index_name: {}},
219-
"mappings": ES_ITEMS_MAPPINGS,
220-
"settings": ES_ITEMS_SETTINGS,
221245
}
222246

223247
try:

stac_fastapi/tests/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from stac_fastapi.opensearch.database_logic import (
2424
DatabaseLogic,
2525
create_collection_index,
26+
create_index_templates,
2627
)
2728
else:
2829
from stac_fastapi.elasticsearch.config import (
@@ -32,6 +33,7 @@
3233
from stac_fastapi.elasticsearch.database_logic import (
3334
DatabaseLogic,
3435
create_collection_index,
36+
create_index_templates,
3537
)
3638

3739
from stac_fastapi.extensions.core import ( # FieldsExtension,
@@ -215,6 +217,7 @@ async def app():
215217

216218
@pytest_asyncio.fixture(scope="session")
217219
async def app_client(app):
220+
await create_index_templates()
218221
await create_collection_index()
219222

220223
async with AsyncClient(app=app, base_url="http://test-server") as c:

stac_fastapi/tests/database/__init__.py

Whitespace-only changes.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import os
2+
import uuid
3+
from copy import deepcopy
4+
5+
import pytest
6+
7+
from ..conftest import MockRequest, database
8+
9+
if os.getenv("BACKEND", "elasticsearch").lower() == "opensearch":
10+
from stac_fastapi.opensearch.database_logic import (
11+
COLLECTIONS_INDEX,
12+
ES_COLLECTIONS_MAPPINGS,
13+
ES_ITEMS_MAPPINGS,
14+
index_by_collection_id,
15+
)
16+
else:
17+
from stac_fastapi.elasticsearch.database_logic import (
18+
COLLECTIONS_INDEX,
19+
ES_COLLECTIONS_MAPPINGS,
20+
ES_ITEMS_MAPPINGS,
21+
index_by_collection_id,
22+
)
23+
24+
25+
@pytest.mark.asyncio
26+
async def test_index_mapping_collections(ctx):
27+
response = await database.client.indices.get_mapping(index=COLLECTIONS_INDEX)
28+
actual_mappings = next(iter(response.body.values()))["mappings"]
29+
assert (
30+
actual_mappings["dynamic_templates"]
31+
== ES_COLLECTIONS_MAPPINGS["dynamic_templates"]
32+
)
33+
34+
35+
@pytest.mark.asyncio
36+
async def test_index_mapping_items(ctx, txn_client):
37+
collection = deepcopy(ctx.collection)
38+
collection["id"] = str(uuid.uuid4())
39+
await txn_client.create_collection(collection, request=MockRequest)
40+
response = await database.client.indices.get_mapping(
41+
index=index_by_collection_id(collection["id"])
42+
)
43+
actual_mappings = next(iter(response.body.values()))["mappings"]
44+
assert (
45+
actual_mappings["dynamic_templates"] == ES_ITEMS_MAPPINGS["dynamic_templates"]
46+
)
47+
await txn_client.delete_collection(collection["id"])

0 commit comments

Comments
 (0)