Skip to main content

A filtering system for Opensearch documents similar to django-filter

Project description

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

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_dsl_filtering-0.1.2.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file django_opensearch_dsl_filtering-0.1.2.tar.gz.

File metadata

File hashes

Hashes for django_opensearch_dsl_filtering-0.1.2.tar.gz
Algorithm Hash digest
SHA256 6d81103f3027b28f67147ddbef6dc776761f30e7851b15a7f58764e59f89fdc7
MD5 72755f78a18c3e425fec8fbee0791ff1
BLAKE2b-256 6b70dfa599a93c75c984aef48c1207ee8bab092df8cedd0b322f7da76005851b

See more details on using hashes here.

File details

Details for the file django_opensearch_dsl_filtering-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_opensearch_dsl_filtering-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 16b5b5a86ebbf544673590ed1ec7c20f01094cad874ba205e2c74cb7fb29addc
MD5 ed15171608a32954317d817511341255
BLAKE2b-256 c5233517fc63ee02e8ce23b8bbc7901d8dd25f7d8606a7f3205045b7e242434c

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