Skip to content

Commit 8e18bda

Browse files
committed
Merge branch 'main' of github.com:stac-utils/stac-fastapi-elasticsearch into simple_patch
2 parents cc5ed61 + ec928c0 commit 8e18bda

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

CHANGELOG.md

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

1313
- Simplified Patch class and updated patch script creation including adding nest creation for merge patch [#420](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/420)
14+
- 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)
1415

1516
## [v6.2.0] - 2025-08-27
1617

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ You can customize additional settings in your `.env` file:
227227
| `RAISE_ON_BULK_ERROR` | Controls whether bulk insert operations raise exceptions on errors. If set to `true`, the operation will stop and raise an exception when an error occurs. If set to `false`, errors will be logged, and the operation will continue. **Note:** STAC Item and ItemCollection validation errors will always raise, regardless of this flag. | `false` | Optional |
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 |
230+
| `STAC_ITEM_LIMIT` | Sets the environment variable for result limiting to SFEOS for the number of returned items and STAC collections. | `10` | Optional |
230231

231232
> [!NOTE]
232233
> 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/core.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Core client."""
22

33
import logging
4+
import os
45
from datetime import datetime as datetime_type
56
from datetime import timezone
67
from enum import Enum
@@ -234,7 +235,7 @@ async def all_collections(self, **kwargs) -> stac_types.Collections:
234235
"""
235236
request = kwargs["request"]
236237
base_url = str(request.base_url)
237-
limit = int(request.query_params.get("limit", 10))
238+
limit = int(request.query_params.get("limit", os.getenv("STAC_ITEM_LIMIT", 10)))
238239
token = request.query_params.get("token")
239240

240241
collections, next_token = await self.database.get_all_collections(
@@ -285,7 +286,7 @@ async def item_collection(
285286
collection_id: str,
286287
bbox: Optional[BBox] = None,
287288
datetime: Optional[str] = None,
288-
limit: Optional[int] = 10,
289+
limit: Optional[int] = None,
289290
token: Optional[str] = None,
290291
**kwargs,
291292
) -> stac_types.ItemCollection:
@@ -295,7 +296,7 @@ async def item_collection(
295296
collection_id (str): The identifier of the collection to read items from.
296297
bbox (Optional[BBox]): The bounding box to filter items by.
297298
datetime (Optional[str]): The datetime range to filter items by.
298-
limit (int): The maximum number of items to return. The default value is 10.
299+
limit (int): The maximum number of items to return.
299300
token (str): A token used for pagination.
300301
request (Request): The incoming request.
301302
@@ -341,6 +342,7 @@ async def item_collection(
341342

342343
search = self.database.apply_bbox_filter(search=search, bbox=bbox)
343344

345+
limit = int(request.query_params.get("limit", os.getenv("STAC_ITEM_LIMIT", 10)))
344346
items, maybe_count, next_token = await self.database.execute_search(
345347
search=search,
346348
limit=limit,
@@ -393,7 +395,7 @@ async def get_search(
393395
ids: Optional[List[str]] = None,
394396
bbox: Optional[BBox] = None,
395397
datetime: Optional[str] = None,
396-
limit: Optional[int] = 10,
398+
limit: Optional[int] = None,
397399
query: Optional[str] = None,
398400
token: Optional[str] = None,
399401
fields: Optional[List[str]] = None,
@@ -426,6 +428,7 @@ async def get_search(
426428
Raises:
427429
HTTPException: If any error occurs while searching the catalog.
428430
"""
431+
limit = int(request.query_params.get("limit", os.getenv("STAC_ITEM_LIMIT", 10)))
429432
base_args = {
430433
"collections": collections,
431434
"ids": ids,

stac_fastapi/tests/api/test_api.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,3 +1470,73 @@ def create_items(date_prefix: str, start_day: int, count: int) -> dict:
14701470
f"/collections/{collection_id}/items/{base_item['id']}", json=item_data
14711471
)
14721472
assert response.json()["properties"]["platform"] == "Updated platform via PUT"
1473+
1474+
1475+
@pytest.mark.asyncio
1476+
async def test_collections_limit_env_variable(app_client, txn_client, load_test_data):
1477+
limit = "5"
1478+
os.environ["STAC_ITEM_LIMIT"] = limit
1479+
item = load_test_data("test_collection.json")
1480+
1481+
for i in range(10):
1482+
test_collection = item.copy()
1483+
test_collection["id"] = f"test-collection-env-{i}"
1484+
test_collection["title"] = f"Test Collection Env {i}"
1485+
await create_collection(txn_client, test_collection)
1486+
1487+
resp = await app_client.get("/collections")
1488+
assert resp.status_code == 200
1489+
resp_json = resp.json()
1490+
assert int(limit) == len(resp_json["collections"])
1491+
1492+
1493+
@pytest.mark.asyncio
1494+
async def test_search_collection_limit_env_variable(
1495+
app_client, txn_client, load_test_data
1496+
):
1497+
limit = "5"
1498+
os.environ["STAC_ITEM_LIMIT"] = limit
1499+
1500+
test_collection = load_test_data("test_collection.json")
1501+
test_collection_id = "test-collection-search-limit"
1502+
test_collection["id"] = test_collection_id
1503+
await create_collection(txn_client, test_collection)
1504+
1505+
item = load_test_data("test_item.json")
1506+
item["collection"] = test_collection_id
1507+
1508+
for i in range(10):
1509+
test_item = item.copy()
1510+
test_item["id"] = f"test-item-search-{i}"
1511+
await create_item(txn_client, test_item)
1512+
1513+
resp = await app_client.get("/search", params={"collections": [test_collection_id]})
1514+
assert resp.status_code == 200
1515+
resp_json = resp.json()
1516+
assert int(limit) == len(resp_json["features"])
1517+
1518+
1519+
@pytest.mark.asyncio
1520+
async def test_collection_items_limit_env_variable(
1521+
app_client, txn_client, load_test_data
1522+
):
1523+
limit = "5"
1524+
os.environ["STAC_ITEM_LIMIT"] = limit
1525+
1526+
test_collection = load_test_data("test_collection.json")
1527+
test_collection_id = "test-collection-items-limit"
1528+
test_collection["id"] = test_collection_id
1529+
await create_collection(txn_client, test_collection)
1530+
1531+
item = load_test_data("test_item.json")
1532+
item["collection"] = test_collection_id
1533+
1534+
for i in range(10):
1535+
test_item = item.copy()
1536+
test_item["id"] = f"test-item-collection-{i}"
1537+
await create_item(txn_client, test_item)
1538+
1539+
resp = await app_client.get(f"/collections/{test_collection_id}/items")
1540+
assert resp.status_code == 200
1541+
resp_json = resp.json()
1542+
assert int(limit) == len(resp_json["features"])

0 commit comments

Comments
 (0)