Skip to content

Commit

Permalink
Rename FacetForm to FacetedSearchForm
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyjurjens committed Jun 4, 2024
1 parent 1a2ec99 commit 63a47e4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ Read the docs from [django-elasticsearch-dsl](https://django-elasticsearch-dsl.r
In order to create a faceted search user interface, you'll need a few things:

- A `DynamicFacetedSearch` subclass with your own settings
- A `FacetForm` with `FacetField` and/or `FilterField`
- A `ESFacetedSearchView` subclass with your created `FacetForm` and `DynamicFacetedSearch` subclasses.
- A `FacetedSearchForm` with `FacetField` and/or `FilterField`
- A `ESFacetedSearchView` subclass with your created `FacetedSearchForm` and `DynamicFacetedSearch` subclasses.

#### DynamicFacetedSearch

Expand All @@ -58,13 +58,13 @@ class CatalogueFacetedSearch(DynamicFacetedSearch):

As you can see, the implementation is quite straight forward, you define the doc_type(s) and in this case I also added a default filter, which is a feature from the `DynamicFacetedSearch`.

#### FacetForm
#### FacetedSearchForm

The FacetForm is a very simple subclass of Django's default Form class. It has a method `get_es_facets` that returns all `FacetField` on the form, which is then later used within the `ESFacetedSearchView`
The FacetedSearchForm is a very simple subclass of Django's default Form class. It has a method `get_es_facets` that returns all `FacetField` on the form, which is then later used within the `ESFacetedSearchView` to populate the available choices for each `FacetField`

#### FacetField, TermsFacetField, RangeFacetField & FilterField

In order to allow users to apply filters, you have to add 'special' fields to your `FacetForm`.
In order to allow users to apply filters, you have to add 'special' fields to your `FacetedSearchForm`.

There are two types of fields:

Expand Down Expand Up @@ -102,13 +102,13 @@ And the `RangeFacetField` also has a `ranges` argument which is **required**. Th
An example form with facet fields would look something like this:

```python
from django_es_kit.forms import FacetForm
from django_es_kit.forms import FacetedSearchForm
from django_es_kit.fields import TermsFacetField, RangeFacetField, RangeOption

def num_stock_formatter(request, key, doc_count):
return f"{key} pieces ({doc_count})"

class CatalogueForm(FacetForm):
class CatalogueForm(FacetedSearchForm):
size = TermsFacetField(es_field="attributes.size", field_type=str)
num_available = RangeFacetField(
es_field="num_available",
Expand Down
4 changes: 4 additions & 0 deletions src/django_es_kit/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,7 @@ def get_es_filter_query(self, cleaned_data) -> Q:
raise NotImplementedError(
"You need to implement the method get_es_filter_query in your subclass."
)


class SortingField(forms.ChoiceField):
pass
6 changes: 3 additions & 3 deletions src/django_es_kit/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
from .fields import FacetField


class FacetForm(forms.Form):
class FacetedSearchForm(forms.Form):
"""
A form for handling Elasticsearch facets.
A form for handling Elasticsearch operations.
This form is used to collect and manage facet fields, making it easier to interact with Elasticsearch.
"""

def __init__(self, *args, **kwargs):
"""
Initialize the FacetForm.
Initialize the FacetedSearchForm.
Args:
*args: Variable length argument list.
Expand Down
6 changes: 3 additions & 3 deletions src/django_es_kit/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.test import RequestFactory, TestCase

from ..views import ESFacetedSearchView
from ..forms import FacetForm
from ..forms import FacetedSearchForm
from ..fields import TermsFacetField
from ..faceted_search import DynamicFacetedSearch

Expand Down Expand Up @@ -60,7 +60,7 @@ def is_role_formatter(request, key, doc_count):
return f"No ({doc_count})"


class UsersFacetForm(FacetForm):
class UsersFacetedSearchForm(FacetedSearchForm):
username = TermsFacetField(es_field="username.keyword", field_type=str)
first_name = TermsFacetField(es_field="first_name.keyword", field_type=str)
last_name = TermsFacetField(es_field="last_name.keyword", field_type=str)
Expand All @@ -82,7 +82,7 @@ class UsersFacetetedSearch(DynamicFacetedSearch):

class UsersFacetedSearchView(ESFacetedSearchView):
faceted_search_class = UsersFacetetedSearch
form_class = UsersFacetForm
form_class = UsersFacetedSearchForm


@pytest.mark.usefixtures("elasticsearch_container")
Expand Down
16 changes: 8 additions & 8 deletions src/django_es_kit/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.views.generic.base import ContextMixin

from .faceted_search import DynamicFacetedSearch
from .forms import FacetForm
from .forms import FacetedSearchForm
from .fields import FacetField, FilterField

logger = logging.getLogger(__name__)
Expand All @@ -19,7 +19,7 @@ class ESFacetedSearchView(ContextMixin, View):
Attributes:
faceted_search_class (type): The class used for faceted search, must be a subclass of `DynamicFacetedSearch`.
form_class (type): The form class used for filtering, must be a subclass of `FacetForm`.
form_class (type): The form class used for filtering, must be a subclass of `FacetedSearchForm`.
"""

faceted_search_class = None
Expand All @@ -36,7 +36,7 @@ def __init__(self, *args, **kwargs):
Raises:
NotImplementedError: If `faceted_search_class` or `form_class` is not defined.
ValueError: If `faceted_search_class` is not a subclass of `DynamicFacetedSearch` or
if `form_class` is not a subclass of `FacetForm`.
if `form_class` is not a subclass of `FacetedSearchForm`.
"""
if not self.get_faceted_search_class():
raise NotImplementedError("The class must have a faceted_search_class")
Expand All @@ -49,8 +49,8 @@ def __init__(self, *args, **kwargs):
if not self.get_form_class():
raise NotImplementedError("The class must have a form_class")

if not issubclass(self.get_form_class(), FacetForm):
raise ValueError("The form_class must be a subclass of FacetForm")
if not issubclass(self.get_form_class(), FacetedSearchForm):
raise ValueError("The form_class must be a subclass of FacetedSearchForm")

self._faceted_search = None
self._form = None
Expand Down Expand Up @@ -123,7 +123,7 @@ def get_form(self):
Get the form instance.
Returns:
FacetForm: The form instance.
FacetedSearchForm: The form instance.
"""
if self._form:
return self._form
Expand Down Expand Up @@ -181,7 +181,7 @@ def apply_filters(self, form, faceted_search):
Apply filters from the form to the faceted search.
Args:
form (FacetForm): The form instance.
form (FacetedSearchForm): The form instance.
faceted_search (DynamicFacetedSearch): The faceted search instance.
"""
for key, value in form.cleaned_data.items():
Expand Down Expand Up @@ -210,7 +210,7 @@ def reflect_es_response_to_form_fields(self, es_response, form):
Args:
es_response (FacetedResponse): The Elasticsearch response.
form (FacetForm): The form instance.
form (FacetedSearchForm): The form instance.
"""
if not hasattr(es_response, "facets"):
return
Expand Down

0 comments on commit 63a47e4

Please sign in to comment.