Skip to content

Commit

Permalink
Merge pull request #156 from StijnCaerts/index_name_compat
Browse files Browse the repository at this point in the history
Remove unsupported characters from Elasticsearch index name
  • Loading branch information
jonhealy1 authored Oct 15, 2023
2 parents cb04a17 + 9b7319e commit b324592
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Corrected the closing of client connections in ES index management functions [#132](https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/132)
- Corrected the automatic converstion of float values to int when building Filter Clauses [#135](https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/135)
- Remove unsupported characters from Elasticsearch index names [#153](https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/153)

## [v0.3.0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@

COLLECTIONS_INDEX = os.getenv("STAC_COLLECTIONS_INDEX", "collections")
ITEMS_INDEX_PREFIX = os.getenv("STAC_ITEMS_INDEX_PREFIX", "items_")
ES_INDEX_NAME_UNSUPPORTED_CHARS = {
"\\",
"/",
"*",
"?",
'"',
"<",
">",
"|",
" ",
",",
"#",
":",
}

DEFAULT_INDICES = f"*,-*kibana*,-{COLLECTIONS_INDEX}"

Expand Down Expand Up @@ -136,7 +150,7 @@ def index_by_collection_id(collection_id: str) -> str:
Returns:
str: The index name derived from the collection id.
"""
return f"{ITEMS_INDEX_PREFIX}{collection_id}"
return f"{ITEMS_INDEX_PREFIX}{''.join(c for c in collection_id.lower() if c not in ES_INDEX_NAME_UNSUPPORTED_CHARS)}"


def indices(collection_ids: Optional[List[str]]) -> str:
Expand All @@ -152,7 +166,7 @@ def indices(collection_ids: Optional[List[str]]) -> str:
if collection_ids is None:
return DEFAULT_INDICES
else:
return ",".join([f"{ITEMS_INDEX_PREFIX}{c.strip()}" for c in collection_ids])
return ",".join([index_by_collection_id(c) for c in collection_ids])


async def create_collection_index() -> None:
Expand Down
12 changes: 12 additions & 0 deletions stac_fastapi/elasticsearch/tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ async def test_create_item_missing_collection(app_client, ctx):
assert resp.status_code == 404


async def test_create_uppercase_collection_with_item(app_client, ctx, txn_client):
"""Test creation of a collection and item with uppercase collection ID (transactions extension)"""
collection_id = "UPPERCASE"
ctx.item["collection"] = collection_id
ctx.collection["id"] = collection_id
resp = await app_client.post("/collections", json=ctx.collection)
assert resp.status_code == 200
await refresh_indices(txn_client)
resp = await app_client.post(f"/collections/{collection_id}/items", json=ctx.item)
assert resp.status_code == 200


async def test_update_item_already_exists(app_client, ctx):
"""Test updating an item which already exists (transactions extension)"""

Expand Down

0 comments on commit b324592

Please sign in to comment.