Skip to content

Commit dd6335a

Browse files
YuriZmytrakovYuri Zmytrakovjonhealy1
authored
feat: Add env var ENV_MAX_LIMIT for items returned (#434)
**Description:** This PR adds a environment variable `ENV_MAX_LIMIT`, which allows overriding the default `MAX_LIMIT` value, which controls the `?limit` URL parameter used in STAC API queries, defining the maximum number of items and collections that can be returned in a single response. **PR Checklist:** - [x] Code is formatted and linted (run `pre-commit run --all-files`) - [x] Tests pass (run `make test`) - [x] Documentation has been updated to reflect changes, if applicable - [x] Changes are added to the changelog Co-authored-by: Yuri Zmytrakov <[email protected]> Co-authored-by: Jonathan Healy <[email protected]>
1 parent 77d77d4 commit dd6335a

File tree

6 files changed

+39
-5
lines changed

6 files changed

+39
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2222

2323
- Added `id` field as secondary sort to sort config to ensure unique pagination tokens. [#421](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/421)
2424
- Added default environment variable `STAC_ITEM_LIMIT` to SFEOS for result limiting of returned items and STAC collections [#419](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/419)
25+
- Added the `ENV_MAX_LIMIT` environment variable to SFEOS, allowing overriding of the `MAX_LIMIT`, which controls the `?limit` parameter for returned items and STAC collections. [#434](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/434)
2526
- Updated the `format_datetime_range` function to support milliseconds. [#423](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/423)
2627

2728
### Changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ You can customize additional settings in your `.env` file:
228228
| `DATABASE_REFRESH` | Controls whether database operations refresh the index immediately after changes. If set to `true`, changes will be immediately searchable. If set to `false`, changes may not be immediately visible but can improve performance for bulk operations. If set to `wait_for`, changes will wait for the next refresh cycle to become visible. | `false` | Optional |
229229
| `ENABLE_TRANSACTIONS_EXTENSIONS` | Enables or disables the Transactions and Bulk Transactions API extensions. If set to `false`, the POST `/collections` route and related transaction endpoints (including bulk transaction operations) will be unavailable in the API. This is useful for deployments where mutating the catalog via the API should be prevented. | `true` | Optional |
230230
| `STAC_ITEM_LIMIT` | Sets the environment variable for result limiting to SFEOS for the number of returned items and STAC collections. | `10` | Optional |
231+
| `ENV_MAX_LIMIT` | Configures the environment variable in SFEOS to override the default `MAX_LIMIT`, which controls the limit parameter for returned items and STAC collections. | `10,000` | Optional |
231232

232233
> [!NOTE]
233234
> The variables `ES_HOST`, `ES_PORT`, `ES_USE_SSL`, `ES_VERIFY_CERTS` and `ES_TIMEOUT` apply to both Elasticsearch and OpenSearch backends, so there is no need to rename the key names to `OS_` even if you're using OpenSearch.

stac_fastapi/core/stac_fastapi/core/utilities.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@
1010

1111
from stac_fastapi.types.stac import Item
1212

13-
MAX_LIMIT = 10000
13+
14+
def get_max_limit():
15+
"""
16+
Retrieve a MAX_LIMIT value from an environment variable.
17+
18+
Returns:
19+
int: The int value parsed from the environment variable.
20+
"""
21+
return int(os.getenv("ENV_MAX_LIMIT", 10000))
1422

1523

1624
def get_bool_env(name: str, default: Union[bool, str] = False) -> bool:

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from stac_fastapi.core.base_database_logic import BaseDatabaseLogic
1919
from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer
20-
from stac_fastapi.core.utilities import MAX_LIMIT, bbox2polygon
20+
from stac_fastapi.core.utilities import bbox2polygon, get_max_limit
2121
from stac_fastapi.elasticsearch.config import AsyncElasticsearchSettings
2222
from stac_fastapi.elasticsearch.config import (
2323
ElasticsearchSettings as SyncElasticsearchSettings,
@@ -543,7 +543,7 @@ async def execute_search(
543543
index_param = ITEM_INDICES
544544
query = add_collections_to_body(collection_ids, query)
545545

546-
max_result_window = MAX_LIMIT
546+
max_result_window = get_max_limit()
547547

548548
size_limit = min(limit + 1, max_result_window)
549549

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from stac_fastapi.core.base_database_logic import BaseDatabaseLogic
1818
from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer
19-
from stac_fastapi.core.utilities import MAX_LIMIT, bbox2polygon
19+
from stac_fastapi.core.utilities import bbox2polygon, get_max_limit
2020
from stac_fastapi.extensions.core.transaction.request import (
2121
PartialCollection,
2222
PartialItem,
@@ -540,7 +540,7 @@ async def execute_search(
540540

541541
search_body["sort"] = sort if sort else DEFAULT_SORT
542542

543-
max_result_window = MAX_LIMIT
543+
max_result_window = get_max_limit()
544544

545545
size_limit = min(limit + 1, max_result_window)
546546

stac_fastapi/tests/api/test_api.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,3 +1540,27 @@ async def test_collection_items_limit_env_variable(
15401540
assert resp.status_code == 200
15411541
resp_json = resp.json()
15421542
assert int(limit) == len(resp_json["features"])
1543+
1544+
1545+
@pytest.mark.asyncio
1546+
async def test_search_max_item_limit(
1547+
app_client, load_test_data, txn_client, monkeypatch
1548+
):
1549+
limit = "10"
1550+
monkeypatch.setenv("ENV_MAX_LIMIT", limit)
1551+
1552+
test_collection = load_test_data("test_collection.json")
1553+
await create_collection(txn_client, test_collection)
1554+
1555+
item = load_test_data("test_item.json")
1556+
1557+
for i in range(20):
1558+
test_item = item.copy()
1559+
test_item["id"] = f"test-item-collection-{i}"
1560+
await create_item(txn_client, test_item)
1561+
1562+
resp = await app_client.get("/search", params={"limit": 20})
1563+
1564+
assert resp.status_code == 200
1565+
resp_json = resp.json()
1566+
assert int(limit) == len(resp_json["features"])

0 commit comments

Comments
 (0)