Skip to main content

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 migrate for 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.
  • Write cleaner unit tests with helpful test runners and mocks

Quick Start

  1. Install the package:
pip install django-opensearch-toolkit
  1. Add this app to your Django project's list of installed apps:
# settings.py

INSTALLED_APPS = [
    ...
    "django_opensearch_toolkit",
    ...
]
  1. 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,
    },
}
  1. 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",
}
  1. 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__.py for an example.

    • NOTE: Currently, we only support a dependency chain, instead of a more generic dependency graph, like Django does for its migrations.
  2. 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

Metadata

Release files for django-opensearch-toolkit 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-opensearch-toolkit 0.1.0
File Size Uploaded
django_opensearch_toolkit-0.1.0.tar.gz 13.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-opensearch-toolkit 0.1.0
File Interpreter ABI Platform
django_opensearch_toolkit-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 29.8 kB

Release files / django_opensearch_toolkit-0.1.0.tar.gz

Download URL django_opensearch_toolkit-0.1.0.tar.gz
Size 13.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3a67fb2602db577404ae64518b23b5d99ff04b2744d3950d152926683aedbcf9
BLAKE2b-256 checksum
How to use checksums
f8c32ee9f40d5d2973864061867af4f71c14423fc27b9fe0194f7b74a5edb2ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.10

Release files / django_opensearch_toolkit-0.1.0-py3-none-any.whl

Download URL django_opensearch_toolkit-0.1.0-py3-none-any.whl
Size 15.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
076922a255b76a91be273b2a9058b6a9552b4a84c28b62bf6665d920cce045cc
BLAKE2b-256 checksum
How to use checksums
a2be7723d5a66daf54b5434bcbd74b9ec84a6705a057b0634e7a48e0695dbe10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page