Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unsupported characters from Elasticsearch index name #156

Merged
merged 2 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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