Jump to: Project Introduction | Quick Start | Table of Contents
The following organizations have contributed time and/or funding to support the development of this project:
Project Introduction - What is SFEOS?SFEOS (stac-fastapi-elasticsearch-opensearch) is a high-performance, scalable API implementation for serving SpatioTemporal Asset Catalog (STAC) data - an enhanced GeoJSON format designed specifically for geospatial assets like satellite imagery, aerial photography, and other Earth observation data. This project enables organizations to:
This implementation builds on the STAC-FastAPI framework, providing a production-ready solution specifically optimized for Elasticsearch and OpenSearch databases. It's ideal for organizations managing large geospatial data catalogs who need efficient discovery and access capabilities through standardized APIs.
Common Deployment Patternsstac-fastapi-elasticsearch-opensearch can be deployed in several ways depending on your needs:
The implementation is flexible and can scale from small local deployments to large production environments serving millions of geospatial assets.
This project is built on the following technologies: STAC, stac-fastapi, FastAPI, Elasticsearch, Python, OpenSearch
This project is organized into several packages, each with a specific purpose:
stac_fastapi_core: Core functionality that's database-agnostic, including API models, extensions, and shared utilities. This package provides the foundation for building STAC API implementations with any database backend. See stac-fastapi-mongo for a working example.
sfeos_helpers: Shared helper functions and utilities used by both the Elasticsearch and OpenSearch backends. This package includes:
database
: Specialized modules for index, document, and database utility operationsaggregation
: Elasticsearch/OpenSearch-specific aggregation functionalitystac_fastapi_elasticsearch: Complete implementation of the STAC API using Elasticsearch as the backend database. This package depends on both stac_fastapi_core
and sfeos_helpers
.
stac_fastapi_opensearch: Complete implementation of the STAC API using OpenSearch as the backend database. This package depends on both stac_fastapi_core
and sfeos_helpers
.
The /examples
directory contains several useful examples and reference implementations:
These examples provide practical reference implementations for various deployment scenarios and features.
enable_direct_response
option is provided by the stac-fastapi core library (introduced in stac-fastapi 5.2.0) and is available in this project starting from v4.0.0.ENABLE_DIRECT_RESPONSE=true
to enable this feature.false
for safety.This section helps you get up and running with stac-fastapi-elasticsearch-opensearch quickly.
For versions 4.0.0a1 and newer (PEP 625 compliant naming):
pip install stac-fastapi-elasticsearch # Elasticsearch backend pip install stac-fastapi-opensearch # Opensearch backend pip install stac-fastapi-core # Core library
For versions 4.0.0a0 and older:
pip install stac-fastapi.elasticsearch # Elasticsearch backend pip install stac-fastapi.opensearch # Opensearch backend pip install stac-fastapi.core # Core library
Important Note: Starting with version 4.0.0a1, package names have changed from using periods (e.g.,
stac-fastapi.core
) to using hyphens (e.g.,stac-fastapi-core
) to comply with PEP 625. The internal package structure uses underscores, but users should install with hyphens as shown above. Please update your requirements files accordingly.
There are two main ways to run the API locally:
Using Pre-built Docker ImagesWe provide ready-to-use Docker images through GitHub Container Registry:
Pull and run the images:
# For Elasticsearch backend docker pull ghcr.io/stac-utils/stac-fastapi-es:latest # For OpenSearch backend docker pull ghcr.io/stac-utils/stac-fastapi-os:latest
Prerequisites: Ensure Docker Compose or Podman Compose is installed on your machine.
Start the API:
docker compose up elasticsearch app-elasticsearch
Configuration: By default, Docker Compose uses Elasticsearch 8.x and OpenSearch 2.11.1. To use different versions, create a .env
file:
ELASTICSEARCH_VERSION=8.11.0 OPENSEARCH_VERSION=2.11.1 ENABLE_DIRECT_RESPONSE=false
Compatibility: The most recent Elasticsearch 7.x versions should also work. See the opensearch-py docs for compatibility information.
You can customize additional settings in your .env
file:
ES_HOST
Hostname for external Elasticsearch/OpenSearch. localhost
Optional ES_PORT
Port for Elasticsearch/OpenSearch. 9200
(ES) / 9202
(OS) Optional ES_USE_SSL
Use SSL for connecting to Elasticsearch/OpenSearch. true
Optional ES_VERIFY_CERTS
Verify SSL certificates when connecting. true
Optional ES_API_KEY
API Key for external Elasticsearch/OpenSearch. N/A Optional ES_TIMEOUT
Client timeout for Elasticsearch/OpenSearch. DB client default Optional STAC_FASTAPI_TITLE
Title of the API in the documentation. stac-fastapi-<backend>
Optional STAC_FASTAPI_DESCRIPTION
Description of the API in the documentation. N/A Optional STAC_FASTAPI_VERSION
API version. 2.1
Optional STAC_FASTAPI_LANDING_PAGE_ID
Landing page ID stac-fastapi
Optional APP_HOST
Server bind address. 0.0.0.0
Optional APP_PORT
Server port. 8000
Optional ENVIRONMENT
Runtime environment. local
Optional WEB_CONCURRENCY
Number of worker processes. 10
Optional RELOAD
Enable auto-reload for development. true
Optional STAC_FASTAPI_RATE_LIMIT
API rate limit per client. 200/minute
Optional BACKEND
Tests-related variable elasticsearch
or opensearch
based on the backend Optional ELASTICSEARCH_VERSION
Version of Elasticsearch to use. 8.11.0
Optional OPENSEARCH_VERSION
OpenSearch version 2.11.1
Optional ENABLE_DIRECT_RESPONSE
Enable direct response for maximum performance (disables all FastAPI dependencies, including authentication, custom status codes, and validation) false
Optional 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 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 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
Note
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.
Creating a Collection:
curl -X "POST" "http://localhost:8080/collections" \ -H 'Content-Type: application/json; charset=utf-8' \ -d $'{ "id": "my_collection" }'
Adding an Item to a Collection:
curl -X "POST" "http://localhost:8080/collections/my_collection/items" \ -H 'Content-Type: application/json; charset=utf-8' \ -d @item.json
Searching for Items:
curl -X "GET" "http://localhost:8080/search" \ -H 'Content-Type: application/json; charset=utf-8' \ -d $'{ "collections": ["my_collection"], "limit": 10 }'
Filtering by Bbox:
curl -X "GET" "http://localhost:8080/search" \ -H 'Content-Type: application/json; charset=utf-8' \ -d $'{ "collections": ["my_collection"], "bbox": [-180, -90, 180, 90] }'
Filtering by Datetime:
curl -X "GET" "http://localhost:8080/search" \ -H 'Content-Type: application/json; charset=utf-8' \ -d $'{ "collections": ["my_collection"], "datetime": "2020-01-01T00:00:00Z/2020-12-31T23:59:59Z" }'
API Title and Description: By default set to stac-fastapi-<backend>
. Customize these by setting:
STAC_FASTAPI_TITLE
: Changes the API title in the documentationSTAC_FASTAPI_DESCRIPTION
: Changes the API description in the documentationDatabase Indices: By default, the API reads from and writes to:
collections
index for collectionsitems_<collection name>
indices for itemsSTAC_COLLECTIONS_INDEX
and STAC_ITEMS_INDEX_PREFIX
environment variablesRoot Path Configuration: The application root path is the base URL by default.
STAC_FASTAPI_ROOT_PATH
to match the Gateway API stage name (e.g., /v1
)limit
: Controls the number of collections returned per pagetoken
: Used to retrieve subsequent pages of resultslinks
field in the response contains a next
link with the token for the next page of results.curl -X "GET" "http://localhost:8080/collections?limit=1&token=example_token"
Overview: The data_loader.py
script provides a convenient way to load STAC items into the database.
Usage:
python3 data_loader.py --base-url http://localhost:8080
Options:
--base-url TEXT Base URL of the STAC API [required]
--collection-id TEXT ID of the collection to which items are added
--use-bulk Use bulk insert method for items
--data-dir PATH Directory containing collection.json and feature
collection file
--help Show this message and exit.
Example Workflows:
python3 data_loader.py --base-url http://localhost:8080
python3 data_loader.py --base-url http://localhost:8080 --collection-id my-collection
python3 data_loader.py --base-url http://localhost:8080 --use-bulk
sfeos_helpers
package contains shared mapping definitions used by both Elasticsearch and OpenSearch backendsOverview: Snapshots provide a way to backup and restore your indices.
Creating a Snapshot Repository:
curl -X "PUT" "http://localhost:9200/_snapshot/my_fs_backup" \ -H 'Content-Type: application/json; charset=utf-8' \ -d $'{ "type": "fs", "settings": { "location": "/usr/share/elasticsearch/snapshots/my_fs_backup" } }'
Creating a Snapshot:
curl -X "PUT" "http://localhost:9200/_snapshot/my_fs_backup/my_snapshot_2?wait_for_completion=true" \ -H 'Content-Type: application/json; charset=utf-8' \ -d $'{ "metadata": { "taken_because": "dump of all items", "taken_by": "pvarner" }, "include_global_state": false, "ignore_unavailable": false, "indices": "items_my-collection" }'
Viewing Snapshots:
# View a specific snapshot curl http://localhost:9200/_snapshot/my_fs_backup/my_snapshot_2 # View all snapshots curl http://localhost:9200/_snapshot/my_fs_backup/_all
Restoring a Snapshot:
curl -X "POST" "http://localhost:9200/_snapshot/my_fs_backup/my_snapshot_2/_restore?wait_for_completion=true" \ -H 'Content-Type: application/json; charset=utf-8' \ -d $'{ "include_aliases": false, "include_global_state": false, "ignore_unavailable": true, "rename_replacement": "items_$1-copy", "indices": "items_*", "rename_pattern": "items_(.+)" }'
Updating Collection References:
curl -X "POST" "http://localhost:9200/items_my-collection-copy/_update_by_query" \ -H 'Content-Type: application/json; charset=utf-8' \ -d $'{ "query": { "match_all": {} }, "script": { "lang": "painless", "params": { "collection": "my-collection-copy" }, "source": "ctx._source.collection = params.collection" } }'
Creating a New Collection:
curl -X "POST" "http://localhost:8080/collections" \ -H 'Content-Type: application/json' \ -d $'{ "id": "my-collection-copy" }'
Overview: Reindexing allows you to copy documents from one index to another, optionally transforming them in the process.
Use Cases:
Example: Reindexing with Transformation:
curl -X "POST" "http://localhost:9200/_reindex" \ -H 'Content-Type: application/json' \ -d $'{ "source": { "index": "items_my-collection-lower_my-collection-hex-000001" }, "dest": { "index": "items_my-collection-lower_my-collection-hex-000002" }, "script": { "source": "ctx._source.id = ctx._source.id.toLowerCase()", "lang": "painless" } }'
Updating Aliases:
curl -X "POST" "http://localhost:9200/_aliases" \ -H 'Content-Type: application/json' \ -d $'{ "actions": [ { "remove": { "index": "*", "alias": "items_my-collection" } }, { "add": { "index": "items_my-collection-lower_my-collection-hex-000002", "alias": "items_my-collection" } } ] }'
STAC_FASTAPI_ROUTE_DEPENDENCIES
environment variable.Supported Aggregations:
Endpoint Locations:
/aggregations
/<collection_id>/aggregations
Implementation Details: The sfeos_helpers.aggregation
package provides specialized functionality for both Elasticsearch and OpenSearch backends.
Documentation: Detailed information about supported aggregations can be found in the aggregation docs.
Overview: Rate limiting is an optional security feature that controls API request frequency on a remote address basis.
Configuration: Enabled by setting the STAC_FASTAPI_RATE_LIMIT
environment variable:
STAC_FASTAPI_RATE_LIMIT=500/minute
Functionality:
Examples: Implementation examples are available in the examples/rate_limit directory.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4