Skip to main content

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 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_opensearch_toolkit-0.1.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_opensearch_toolkit-0.1.0-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file django_opensearch_toolkit-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django_opensearch_toolkit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3a67fb2602db577404ae64518b23b5d99ff04b2744d3950d152926683aedbcf9
MD5 37d782de5fd90ef053598a93ec9615e9
BLAKE2b-256 f8c32ee9f40d5d2973864061867af4f71c14423fc27b9fe0194f7b74a5edb2ab

See more details on using hashes here.

File details

Details for the file django_opensearch_toolkit-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_opensearch_toolkit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 076922a255b76a91be273b2a9058b6a9552b4a84c28b62bf6665d920cce045cc
MD5 d8ac0f359a02a78e2448f65e197d15d5
BLAKE2b-256 a2be7723d5a66daf54b5434bcbd74b9ec84a6705a057b0634e7a48e0695dbe10

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page