A Django app for interacting with OpenSearch clusters
Project description
django-opensearch-toolkit
A Django app to facilitate interacting with OpenSearch clusters, including connection management, migrations, and unit tests.
It is implemented as a thin wrapper over the opensearch-py library for connection management and DSL operations, and benefits from all functionality it provides. The only other dependency is Django itself.
Some key advantages to using this app:
- Configure connections to multiple clusters using the Django settings module
- This is analogous to how Django manages connections to multiple RDBMS databases.
- Define cluster state (e.g., ISM policies, index template mappings, indices) in code, via migration files
- This makes it easier to track and replicate these settings across environments (e.g., dev & prod clusters).
- This is analogous to how Django generates migration files for schema changes. The difference is that the migrations here are manually written, not auto-generated. They can be written using the DSL of the OpenSearch Python client.
- Run migrations against clusters using Django management commands
- This is analogous to running
python manage.py migratefor RDBMS databases. - Under the hood, it tracks the state of migrations in a hidden index in the cluster itself to avoid running migrations multiple times, similar to what Django does using tables in relational dbs.
- This is analogous to running
- Write cleaner unit tests with helpful test runners and mocks
Quick Start
- Install the package:
pip install django-opensearch-toolkit
- Add this app to your Django project's list of installed apps:
# settings.py
INSTALLED_APPS = [
...
"django_opensearch_toolkit",
...
]
- Define the cluster(s) to configure:
# settings.py
OPENSEARCH_CLUSTERS = {
# cluster_name -> configuration
# This dict is passed to opensearchpy.connection.configure() while the
# Django environment is being initialized.
"sample_app": {
"hosts": [
{
"host": "localhost",
"port": 9200,
}
],
"timeout": 30,
},
}
- Register migrations for each cluster
# settings.py
OPENSEARCH_MIGRATION_PATHS = {
# cluster_name -> module_path
# - Each module should define a variable named MIGRATIONS.
# - The module will be dynamically imported and the MIGRATIONS variable will be used.
"sample_app": "sample_app.opensearch_migrations",
}
-
Implement your migrations and ensure they are discoverable at the paths indicated in the previous step. See the
sample_project/sample_app/opensearch_migrations/__init__.pyfor an example.- NOTE: Currently, we only support a dependency chain, instead of a more generic dependency graph, like Django does for its migrations.
-
Display and run your migrations.
- This requires a running OpenSearch cluster at localhost:9200 (configured in step 3).
cd sample_project
python manage.py opensearch_displaymigrations sample_app
python manage.py opensearch_runmigrations sample_app
python manage.py opensearch_runmigrations sample_app --nodry
python manage.py opensearch_displaymigrations sample_app
Local Development
From the project root, run:
./scripts/setup_dev.sh # Creates a virtual environment in the project directory & downloads all requirements
source venv/bin/activate # Step into your virtual environment
make test # Confirm all tests pass
make check # Confirm all static checks pass
make integration-test # Run an integration test (requires docker daemon to be running)
deactivate # Leave your virtual environment
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_opensearch_toolkit-0.1.0.tar.gz.
File metadata
- Download URL: django_opensearch_toolkit-0.1.0.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a67fb2602db577404ae64518b23b5d99ff04b2744d3950d152926683aedbcf9
|
|
| MD5 |
37d782de5fd90ef053598a93ec9615e9
|
|
| BLAKE2b-256 |
f8c32ee9f40d5d2973864061867af4f71c14423fc27b9fe0194f7b74a5edb2ab
|
File details
Details for the file django_opensearch_toolkit-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_opensearch_toolkit-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
076922a255b76a91be273b2a9058b6a9552b4a84c28b62bf6665d920cce045cc
|
|
| MD5 |
d8ac0f359a02a78e2448f65e197d15d5
|
|
| BLAKE2b-256 |
a2be7723d5a66daf54b5434bcbd74b9ec84a6705a057b0634e7a48e0695dbe10
|