Skip to main content

Create Django forms that perform ElasticSearch queries and filters.

Installing

Install using pip:

pip install django-elasticfilter

Using

The filter forms are standard Django forms, with some small extensions. They inherit from elasticfilter.filterform.FilterForm. Fields to search and filter on are declared as fields on the form, which should inherit from elasticfilter.fields.BaseField:

from elasticfilter.filterform import FilterForm
from elasticfilter.fields import Query, Filter

class MyFilterForm(FilterForm):

    class FilterFormMeta:
        s = S().get_es(**ES_SETTINGS)\
            .indexes('my-index')\
            .doctypes('my-doctype')

    match = Query(required=False, fields=['_all', '_partial'])
    type = Filter(field='type')

Custom queries and filters

The provided Query and Filter classes are very basic. Writing custom Query and Filter classes is quite simple. For example, to filter results based upon the ContentType of the model:

from django import forms
from django.db.models import get_models
from elasticfilter.filterform import FilterForm
from elasticfilter.fields import Filter

class ContentTypeFilter(Filter, forms.ChoiceField):
    required = False

    def __init__(self, models=None, **kwargs):
        if models is None:
            models = get_models()

        self.choice_map = dict((model._meta.model_name, model)
                               for model in models)

        choices = [(key, model.get_verbose_name())
                   for key, model in self.choice_map.items()]
        kwargs.setdefault('choices', choices)
        super(ContentTypeFilter, self).__init__(**kwargs)

    def search(self, s, name, data):
        value = data.get(name, None)
        if not value:
            return s

        field = self.field or name
        content_type = ContentType.objects.get_for_model(self.choice_map[value])
        return s.filter(F(**{field: content_type.pk}))

class MyFilterForm(FilterForm):
    content_type = ContentTypeFilter()

Release files for django-elasticfilter 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-elasticfilter 0.1.0
File Size Uploaded
django-elasticfilter-0.1.0.tar.gz 3.2 kB Details

Release files / django-elasticfilter-0.1.0.tar.gz

Download URL django-elasticfilter-0.1.0.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5580ade79cf24bc88bd3d4dd65a84faf387fa0d9243349e9c936c5aebc351050
BLAKE2b-256 checksum
How to use checksums
2fff15321bbf5d6a64f4586fb653813b00abd861a1cffc4306a11d33f7dc3b8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file

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