django_elasticsearch_dsl_drf package

Subpackages

Submodules

django_elasticsearch_dsl_drf.analyzers module

Analyzers.

django_elasticsearch_dsl_drf.apps module

Apps.

class django_elasticsearch_dsl_drf.apps.Config(app_name, app_module)[source]

Bases: django.apps.config.AppConfig

Config.

label = 'django_elasticsearch_dsl_drf'
name = 'django_elasticsearch_dsl_drf'

django_elasticsearch_dsl_drf.compat module

Transitional compatibility module. Contains various field wrappers and helpers for painless (testing of) Elastic 2.x to Elastic 5.x transition. This module is not supposed to solve all transition issues for you. Better move to Elastic 5.x as soon as possible.

class django_elasticsearch_dsl_drf.compat.KeywordField(attr=None, **kwargs)[source]

Bases: django_elasticsearch_dsl.fields.DEDField, elasticsearch_dsl.field.Keyword

django_elasticsearch_dsl_drf.compat.StringField(**kwargs)

String field.

Parameters

kwargs

Returns

django_elasticsearch_dsl_drf.constants module

Constants module. Contains Elasticsearch constants, lookup constants, functional constants, suggesters, etc.

django_elasticsearch_dsl_drf.elasticsearch_helpers module

django_elasticsearch_dsl_drf.elasticsearch_helpers.delete_all_indices(with_protected=False)[source]

Delete all indices.

Args:

with_protected (bool):

Returns:

tuple: Tuple of two lists with removed and errored indices.

django_elasticsearch_dsl_drf.elasticsearch_helpers.get_all_indices(with_protected=False)[source]

Get all indices.

Args:

with_protected (bool):

Returns:

list: List of indices.

django_elasticsearch_dsl_drf.helpers module

Helpers.

django_elasticsearch_dsl_drf.helpers.get_document_for_model(model)[source]

Get document for model given.

Parameters

model (Subclass of django.db.models.Model.) – Model to get document index for.

Returns

Document index for the given model.

Return type

Subclass of django_elasticsearch_dsl.Document.

django_elasticsearch_dsl_drf.helpers.get_index_and_mapping_for_model(model)[source]

Get index and mapping for model.

Parameters

model (Subclass of django.db.models.Model.) – Django model for which to get index and mapping for.

Returns

Index and mapping values.

Return type

tuple.

django_elasticsearch_dsl_drf.helpers.more_like_this(obj, fields, max_query_terms=25, min_term_freq=2, min_doc_freq=5, max_doc_freq=0, query=None)[source]

More like this.

https://www.elastic.co/guide/en/elasticsearch/reference/current/ query-dsl-mlt-query.html

Parameters
  • obj (Instance of django.db.models.Model (sub-classed) model.) – Django model instance for which similar objects shall be found.

  • fields (list) – Fields to search in.

  • max_query_terms (int) –

  • min_term_freq (int) –

  • min_doc_freq (int) –

  • max_doc_freq (int) –

  • query (elasticsearch_dsl.query.Q) – Q query

Returns

List of objects.

Return type

elasticsearch_dsl.search.Search

Example:

>>> from django_elasticsearch_dsl_drf.helpers import more_like_this
>>> from books.models import Book
>>> book = Book.objects.first()
>>> similar_books = more_like_this(
>>>     book,
>>>     ['title', 'description', 'summary']
>>> )
django_elasticsearch_dsl_drf.helpers.sort_by_list(unsorted_dict, sorted_keys)[source]

Sort an OrderedDict by list of sorted keys.

Parameters
  • unsorted_dict (collections.OrderedDict) – Source dictionary.

  • sorted_keys (list) – Keys to sort on.

Returns

Sorted dictionary.

Return type

collections.OrderedDict

django_elasticsearch_dsl_drf.pagination module

Pagination.

class django_elasticsearch_dsl_drf.pagination.LimitOffsetPagination(*args, **kwargs)[source]

Bases: rest_framework.pagination.LimitOffsetPagination, django_elasticsearch_dsl_drf.pagination.GetCountMixin

A limit/offset pagination.

Example:

get_facets(facets=None)[source]

Get facets.

Parameters

facets

Returns

get_paginated_response(data)[source]

Get paginated response.

Parameters

data

Returns

get_paginated_response_context(data)[source]

Get paginated response data.

Parameters

data

Returns

paginate_queryset(queryset, request, view=None)[source]
class django_elasticsearch_dsl_drf.pagination.Page(object_list, number, paginator, facets)[source]

Bases: django.core.paginator.Page, django_elasticsearch_dsl_drf.pagination.GetCountMixin

Page for Elasticsearch.

class django_elasticsearch_dsl_drf.pagination.PageNumberPagination(*args, **kwargs)[source]

Bases: rest_framework.pagination.PageNumberPagination, django_elasticsearch_dsl_drf.pagination.GetCountMixin

Page number pagination.

A simple page number based style that supports page numbers as query parameters.

Example:

django_paginator_class

alias of django_elasticsearch_dsl_drf.pagination.Paginator

get_facets(page=None)[source]

Get facets.

Parameters

page

Returns

get_paginated_response(data)[source]

Get paginated response.

Parameters

data

Returns

get_paginated_response_context(data)[source]

Get paginated response data.

Parameters

data

Returns

paginate_queryset(queryset, request, view=None)[source]

Paginate a queryset.

Paginate a queryset if required, either returning a page object, or None if pagination is not configured for this view.

Parameters
  • queryset

  • request

  • view

Returns

class django_elasticsearch_dsl_drf.pagination.Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True)[source]

Bases: django.core.paginator.Paginator

Paginator for Elasticsearch.

page(number)[source]

Returns a Page object for the given 1-based page number.

Parameters

number

Returns

class django_elasticsearch_dsl_drf.pagination.QueryFriendlyPageNumberPagination(*args, **kwargs)[source]

Bases: django_elasticsearch_dsl_drf.pagination.PageNumberPagination

Page number pagination.

A simple page number based style that supports page numbers as query parameters.

Example:

django_paginator_class

alias of django_elasticsearch_dsl_drf.pagination.QueryFriendlyPaginator

orphans_query_param = 'orphans'
page_size_query_param = 'page_size'
paginate_queryset(queryset, request, view=None)[source]

Paginate a queryset.

Paginate a queryset if required, either returning a page object, or None if pagination is not configured for this view.

Parameters
  • queryset

  • request

  • view

Returns

class django_elasticsearch_dsl_drf.pagination.QueryFriendlyPaginator(object_list, per_page, orphans=0, allow_empty_first_page=True)[source]

Bases: django_elasticsearch_dsl_drf.pagination.Paginator, django_elasticsearch_dsl_drf.pagination.GetCountMixin

Paginator for Elasticsearch.

page(number)[source]

Returns a Page object for the given 1-based page number.

Parameters

number

Returns

django_elasticsearch_dsl_drf.pip_helpers module

Pip helpers module.

django_elasticsearch_dsl_drf.pip_helpers.check_if_installed(package, installed_packages=None)[source]

Check if package is installed.

Parameters
  • package (str) –

  • installed_packages (iterable) –

Returns

Return type

bool

django_elasticsearch_dsl_drf.pip_helpers.get_installed_packages(with_versions=False)[source]

Get installed packages.

Parameters

with_versions (bool) – If set to True, returned with versions.

Returns

Return type

list

django_elasticsearch_dsl_drf.serializers module

Serializers.

class django_elasticsearch_dsl_drf.serializers.DocumentSerializer(*args, **kwargs)[source]

Bases: rest_framework.serializers.Serializer

A dynamic DocumentSerializer class.

create(validated_data)[source]

Create.

Do nothing.

Parameters

validated_data

Returns

get_fields()[source]

Get the required fields for serializing the result.

update(instance, validated_data)[source]

Update.

Do nothing.

Parameters
  • instance

  • validated_data

Returns

class django_elasticsearch_dsl_drf.serializers.DocumentSerializerMeta(name, bases, attrs)[source]

Bases: rest_framework.serializers.SerializerMetaclass

Metaclass for the DocumentSerializer.

Ensures that all declared subclasses implemented a Meta.

class django_elasticsearch_dsl_drf.serializers.Meta(name, bases, attrs)[source]

Bases: type

Template for the DocumentSerializerMeta.Meta class.

exclude = ()
field_aliases = {}
field_options = {}
fields = ()
ignore_fields = ()
index_aliases = {}
index_classes = ()
search_fields = ()
serializers = ()

django_elasticsearch_dsl_drf.utils module

Utils.

class django_elasticsearch_dsl_drf.utils.DictionaryProxy(mapping, meta=None)[source]

Bases: object

Dictionary proxy.

to_dict()[source]

To dict.

Returns

class django_elasticsearch_dsl_drf.utils.EmptySearch(*args, **kwargs)[source]

Bases: object

Empty Search.

execute(*args, **kwargs)[source]
highlight(*args, **kwargs)[source]
property hits
sort(*args, **kwargs)[source]
to_dict(*args, **kwargs)[source]

django_elasticsearch_dsl_drf.versions module

Contains information about the current Elasticsearch version in use, including (LTE and GTE).

django_elasticsearch_dsl_drf.versions.get_elasticsearch_version(default=(2, 0, 0))[source]

Get Elasticsearch version.

Parameters

default (tuple) – Default value. Mainly added for building the docs when Elasticsearch is not running.

Returns

Return type

list

django_elasticsearch_dsl_drf.viewsets module

Base ViewSets.

class django_elasticsearch_dsl_drf.viewsets.BaseDocumentViewSet(*args, **kwargs)[source]

Bases: rest_framework.viewsets.ReadOnlyModelViewSet

Base document ViewSet.

dictionary_proxy

alias of django_elasticsearch_dsl_drf.utils.DictionaryProxy

document = None
document_uid_field = 'id'
get_object()[source]

Get object.

get_queryset()[source]

Get queryset.

ignore = []
pagination_class

alias of django_elasticsearch_dsl_drf.pagination.PageNumberPagination

class django_elasticsearch_dsl_drf.viewsets.DocumentViewSet(*args, **kwargs)[source]

Bases: django_elasticsearch_dsl_drf.viewsets.BaseDocumentViewSet, django_elasticsearch_dsl_drf.viewsets.SuggestMixin, django_elasticsearch_dsl_drf.viewsets.FunctionalSuggestMixin

DocumentViewSet with suggest and functional-suggest mix-ins.

class django_elasticsearch_dsl_drf.viewsets.FunctionalSuggestMixin[source]

Bases: object

Functional suggest mixin.

functional_suggest(request)[source]

Functional suggest functionality.

Parameters

request

Returns

class django_elasticsearch_dsl_drf.viewsets.MoreLikeThisMixin[source]

Bases: object

More-like-this mixin.

more_like_this(request, pk=None, id=None)[source]

More-like-this functionality detail view.

Parameters

request

Returns

class django_elasticsearch_dsl_drf.viewsets.SuggestMixin[source]

Bases: object

Suggest mixin.

suggest(request)[source]

Suggest functionality.

django_elasticsearch_dsl_drf.wrappers module

class django_elasticsearch_dsl_drf.wrappers.Wrapper[source]

Bases: object

Wrapper.

Example: >>> from django_elasticsearch_dsl_drf.wrappers import dict_to_obj >>> >>> mapping = { >>> ‘country’: { >>> ‘name’: ‘Netherlands’, >>> ‘province’: { >>> ‘name’: ‘North Holland’, >>> ‘city’: { >>> ‘name’: ‘Amsterdam’, >>> } >>> } >>> } >>> } >>> >>> wrapper = dict_to_obj(mapping) >>> wrapper.country.name >>> “Netherlands” >>> wrapper.country.province.name >>> “North Holland” >>> wrapper.country.province.city.name >>> “Amsterdam” >>> wrapper.as_dict >>> { >>> ‘country’: { >>> ‘name’: ‘Netherlands’, >>> ‘province’: { >>> ‘name’: ‘North Holland’, >>> ‘city’: { >>> ‘name’: ‘Amsterdam’, >>> } >>> } >>> } >>> } >>> str(wrapper) >>> “Netherlands”

property as_dict

As dict.

Returns

Return type

dict

property as_json

As JSON.

Returns

Return type

str

django_elasticsearch_dsl_drf.wrappers.dict_to_obj(mapping)[source]

dict to obj mapping.

Parameters

mapping (dict) –

Returns

Return type

Wrapper

django_elasticsearch_dsl_drf.wrappers.obj_to_dict(obj)[source]

Wrapper to dict.

Parameters

obj (`obj`:Wrapper:) –

Returns

Return type

dict

Module contents

Integrate Elasticsearch DSL with Django REST framework.