Skip to content

Commit 23dca8a

Browse files
authored
Merge pull request #152 from StijnCaerts/es-alias
Use Elasticsearch aliases on indices
2 parents 8fc2d0d + eade0b4 commit 23dca8a

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1616
- Examples folder with example docker setup for running sfes from pip [#147](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/147)
1717

1818
### Changed
19+
20+
- Use aliases on Elasticsearch indices, add number suffix in index name. [#152](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/152)
21+
1922
### Fixed
2023

2124
- Corrected the closing of client connections in ES index management functions [#132](https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/132)

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
":",
4040
}
4141

42-
DEFAULT_INDICES = f"*,-*kibana*,-{COLLECTIONS_INDEX}"
42+
ITEM_INDICES = f"{ITEMS_INDEX_PREFIX}*,-*kibana*,-{COLLECTIONS_INDEX}*"
4343

4444
DEFAULT_SORT = {
4545
"properties.datetime": {"order": "desc"},
@@ -164,7 +164,7 @@ def indices(collection_ids: Optional[List[str]]) -> str:
164164
A string of comma-separated index names. If `collection_ids` is None, returns the default indices.
165165
"""
166166
if collection_ids is None:
167-
return DEFAULT_INDICES
167+
return ITEM_INDICES
168168
else:
169169
return ",".join([index_by_collection_id(c) for c in collection_ids])
170170

@@ -178,7 +178,8 @@ async def create_collection_index() -> None:
178178
client = AsyncElasticsearchSettings().create_client
179179

180180
await client.indices.create(
181-
index=COLLECTIONS_INDEX,
181+
index=f"{COLLECTIONS_INDEX}-000001",
182+
aliases={COLLECTIONS_INDEX: {}},
182183
mappings=ES_COLLECTIONS_MAPPINGS,
183184
ignore=400, # ignore 400 already exists code
184185
)
@@ -197,9 +198,11 @@ async def create_item_index(collection_id: str):
197198
198199
"""
199200
client = AsyncElasticsearchSettings().create_client
201+
index_name = index_by_collection_id(collection_id)
200202

201203
await client.indices.create(
202-
index=index_by_collection_id(collection_id),
204+
index=f"{index_by_collection_id(collection_id)}-000001",
205+
aliases={index_name: {}},
203206
mappings=ES_ITEMS_MAPPINGS,
204207
settings=ES_ITEMS_SETTINGS,
205208
ignore=400, # ignore 400 already exists code
@@ -215,7 +218,14 @@ async def delete_item_index(collection_id: str):
215218
"""
216219
client = AsyncElasticsearchSettings().create_client
217220

218-
await client.indices.delete(index=index_by_collection_id(collection_id))
221+
name = index_by_collection_id(collection_id)
222+
resolved = await client.indices.resolve_index(name=name)
223+
if "aliases" in resolved and resolved["aliases"]:
224+
[alias] = resolved["aliases"]
225+
await client.indices.delete_alias(index=alias["indices"], name=alias["name"])
226+
await client.indices.delete(index=alias["indices"])
227+
else:
228+
await client.indices.delete(index=name)
219229
await client.close()
220230

221231

@@ -773,14 +783,11 @@ async def bulk_async(
773783
`mk_actions` function is called to generate a list of actions for the bulk insert. If `refresh` is set to True, the
774784
index is refreshed after the bulk insert. The function does not return any value.
775785
"""
776-
await asyncio.get_event_loop().run_in_executor(
777-
None,
778-
lambda: helpers.bulk(
779-
self.sync_client,
780-
mk_actions(collection_id, processed_items),
781-
refresh=refresh,
782-
raise_on_error=False,
783-
),
786+
await helpers.async_bulk(
787+
self.client,
788+
mk_actions(collection_id, processed_items),
789+
refresh=refresh,
790+
raise_on_error=False,
784791
)
785792

786793
def bulk_sync(
@@ -811,7 +818,7 @@ def bulk_sync(
811818
async def delete_items(self) -> None:
812819
"""Danger. this is only for tests."""
813820
await self.client.delete_by_query(
814-
index=DEFAULT_INDICES,
821+
index=ITEM_INDICES,
815822
body={"query": {"match_all": {}}},
816823
wait_for_completion=True,
817824
)

0 commit comments

Comments
 (0)