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 fieldsNumericFilter: For numeric fieldsRangeFilter: For numeric fields with a rangeDateFilter: For date fieldsBooleanFilter: For boolean fields
Customizing Filters
Each filter can be customized with the following parameters:
field_name: The name of the field to filter onlookup_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
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_dsl_filtering-0.1.3.tar.gz.
File metadata
- Download URL: django_opensearch_dsl_filtering-0.1.3.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06febd5e808c4727faba0c6deb2dec01ed4a1734f1cd52a7f38d1f7bfbec87e6
|
|
| MD5 |
feeba60a1079aee53945ed3bf4a9a179
|
|
| BLAKE2b-256 |
bacfa004fd2754d40f913e01f4c084412c79c0e43453d459bba98bf2fe3919f5
|
File details
Details for the file django_opensearch_dsl_filtering-0.1.3-py3-none-any.whl.
File metadata
- Download URL: django_opensearch_dsl_filtering-0.1.3-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
93496b019dffa5513010a6c5f2b93aebc74edeb255192937e781c31f1601b79a
|
|
| MD5 |
ce7f35c8c01f1f54206cfe94e48f914c
|
|
| BLAKE2b-256 |
e542cbcec3009a3bd67906003257843e6638868085d4d4a578c3a6ac04223114
|