Skip to main content

Django Opensearch DSL Filtering

A filtering system for Opensearch documents similar to django-filter, but designed to work with Opensearch queries instead of Django ORM.

Installation

pip install django-opensearch-dsl-filtering

Features

  • Filter Opensearch documents using a familiar Django-like API
  • Generate Django forms for your filters automatically
  • Support for pagination and sorting
  • Various filter types: CharFilter, NumericFilter, RangeFilter, DateFilter, BooleanFilter

Quick Start

Define a Document FilterSet

from django_opensearch_dsl import Document
from django_opensearch_dsl_filtering import CharFilter, DateFilter, DocumentFilterSet, NumericFilter

# Assuming you have a Document class defined
class BookDocument(Document):
    # Your document definition here
    class Index:
        name = "books"

    class Django:
        model = Book
        fields = ["id"]

# Create a FilterSet for your document
class BookDocumentFilterSet(DocumentFilterSet):
    document = BookDocument

    # Define filters
    title = CharFilter(field_name="title", lookup_expr="match", label="Title")
    author = CharFilter(field_name="author", lookup_expr="match", label="Author")
    publication_date = DateFilter(field_name="publication_date", label="Publication Date")
    price = NumericFilter(field_name="price", label="Price")
    price_min = NumericFilter(field_name="price", lookup_expr="gte", label="Min Price")
    price_max = NumericFilter(field_name="price", lookup_expr="lte", label="Max Price")

    # Define sorting options
    SORT_CHOICES = [
        ("", "Default"),
        ("title", "Title (A-Z)"),
        ("-title", "Title (Z-A)"),
        ("price", "Price (Low to High)"),
        ("-price", "Price (High to Low)"),
    ]

Use the FilterSet in a View

from django.shortcuts import render

def book_search(request):
    # Create a filter set with the request data
    filter_set = BookDocumentFilterSet(data=request.GET)

    # Get the search results
    search = filter_set.search()
    results = search.execute()

    # Get the form for rendering in the template
    form = filter_set.get_form()

    return render(
        request,
        "books/search.html",
        {
            "form": form,
            "results": results,
        },
    )

Use the Form in a Template

<form method="get">
    {{ form.as_p }}
    <button type="submit">Search</button>
</form>

<div class="results">
    {% for result in results %}
        <div class="result">
            <h2>{{ result.title }}</h2>
            <p>Author: {{ result.author }}</p>
            <p>Price: ${{ result.price }}</p>
        </div>
    {% endfor %}
</div>

Available Filters

  • CharFilter: For text fields
  • NumericFilter: For numeric fields
  • RangeFilter: For numeric fields with a range
  • DateFilter: For date fields
  • BooleanFilter: For boolean fields

Customizing Filters

Each filter can be customized with the following parameters:

  • field_name: The name of the field to filter on
  • lookup_expr: The lookup expression to use (e.g., "match", "term", "wildcard", "gt", "lt", etc.)
  • label: The label to use for the form field

License

MIT

Metadata

Release files for django-opensearch-dsl-filtering 0.1.6

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-dsl-filtering 0.1.6
File Size Uploaded Provenance Actions
django_opensearch_dsl_filtering-0.1.6.tar.gz 7.8 kB Without provenance Details for django_opensearch_dsl_filtering-0.1.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-opensearch-dsl-filtering 0.1.6
File Interpreter ABI Platform Actions
django_opensearch_dsl_filtering-0.1.6-py3-none-any.whl Python 3 none any Details for django_opensearch_dsl_filtering-0.1.6-py3-none-any.whl

Total release size: 15.1 kB

Release files / django_opensearch_dsl_filtering-0.1.6.tar.gz

Download URL django_opensearch_dsl_filtering-0.1.6.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
1c909c055c07040048e09ac2f545fd1f845068f4fe2898183a114309cc216cea
BLAKE2b-256 checksum
How to use checksums
b7ad5bb3076de15a284cf2ba72285ce49a7a82f8b5985f5ea190773d28b751e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Release files / django_opensearch_dsl_filtering-0.1.6-py3-none-any.whl

Download URL django_opensearch_dsl_filtering-0.1.6-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
20a293a2da5a16c70cdd36f23b58107fec3e55699855b2470e3bac44c0741a33
BLAKE2b-256 checksum
How to use checksums
c3537814b70571b515de5e14566b22dfd30178387934d9b2b40887a6d8206641
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Security for django-opensearch-dsl-filtering 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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