API

The mixin module provides the main interface to this module, and it is expected you will subclass it to provide search functionality in your code.

class printen.mixins.DjangoElasticsearchIndexMixin

A class that inherits from ElasticsearchIndexMixin, overriding the relevant methods with Django related defaults.

classmethod get_document_id(obj)

Provides a sane default for Django models. Override to modify behavior.

Parameters:obj (A subclass of django.db.models.Model.) – The object to index.
Returns:obj.pk, an integer.
Raises:MissingObjectError
classmethod get_queryset()

Provides a sane default for Django models. Override to modify behavior.

Returns:A Django QuerySet..
classmethod get_type_name()

Provides a sane default for Django models. Override to modify behavior.

Returns:String with a dot separated app_label and model_name.
queryset_iterator(queryset, chunksize=1000)

Provides a sane default for Django models. Override to modify behavior.

Parameters:queryset – A Django QuerySet to iterate over.
Returns:An instance of our model object.
class printen.mixins.ElasticsearchIndexMixin

This class can be used with a standalone class or directly on your model. There are several methods that you are required to subclass in order to use this.

classmethod bulk_index(index_name='', queryset=None)

Initiate a bulk index of all items returned by the queryset.

Parameters:
  • index_name – The index to update. By default this is the value returned by get_index_name()
  • queryset – The queryset to iterate over.
classmethod delete_handler(sender, instance, **kwargs)

A target for wiring to a delete signal for your model.

Parameters:
  • sender – The class which initated the delete.
  • instance – The instance being delete.
classmethod get_bulk_index_batch_size()

Get the number of items to limit bulk indexing to at a time (i.e. per call to the Elasticsearch server).

Defaults to 100. Set ELASTICSEARCH_BULK_INDEX_BATCH_SIZE to change this value, or override this method.

Returns:An integer.
classmethod get_document(obj)

Returns the ‘document’ to be indexed, which is a dictionary representation of the object passed in, including the relevant fields you wish to be in the search engine.

You must override this method.

Parameters:obj (A python object which you are indexing.) – The object to generate a document for.
Returns:A dictionary with key/value pairs of fields and values.
classmethod get_document_id(obj)

Returns the document id, which usually corresponds to the objects primary key in the database.

You must override this method.

Parameters:obj (A python object which you are indexing.) – The object to derive an id from.
Returns:An integer representing the document’s id.
classmethod get_es()

Returns the configured reference to the Elasticsearch instance.

Returns:The elasticsearch instance
Raises:Misconfigured
classmethod get_es_connection_settings()

Returns the connection parameters for Elasticsearch

Returns:A dictionary containing key/value pairs of connection parameters
Raises:AttributeError
classmethod get_index_name()

Allows indexing code to know the name of the index to use. This defaults to index but can be overridden by setting the ELASTICSEARCH_INDEX_NAME in your configuration, or overriding this method on a per-model basis.

Returns:A string containing the index name.
classmethod get_queryset()

Get a queryset for the model.

You must implement this yourself.

Returns:An object with an interface that your queryset_iterator method knows how to iterate over.
classmethod get_request_params(obj)

Overridable per-model request parameters for Elasticsearch.

Parameters:obj (A python object which you are indexing.) – The object involved in the request.
Returns:Request parameters dictionary passed to Elasticsearch’s index operation.
classmethod get_type_mapping()

Overridable per-model type mapping for Elasticsearch.

Returns:Type mapping for this type used when creating the index.
classmethod get_type_name()

This provides Elasticsearch with a name with which to do type mappings for your model. Defaults to cls.__class__.__name__.

Returns:A string naming the type of thing being indexed.
classmethod index_add(obj, index_name=None)

Adds a single object to the index.

Parameters:
  • obj (A python object which you are indexing.) – The object to index.
  • index_name – The index to update. By default this is the value returned by get_index_name()
Returns:

A Boolean indicating if the object was added.

classmethod index_add_or_delete(obj, index_name=None)

Adds or removes an object depending on the return value of should_index().

Parameters:
  • obj (A python object which you are indexing.) – The object to index.
  • index_name – The index to update. By default this is the value returned by get_index_name()
Returns:

A Boolean indicating if the object was added/removed.

classmethod index_delete(obj, index_name=None)

Removes a single object to the index.

Parameters:
  • obj (A python object which you are indexing.) – The object to index.
  • index_name – The index to update. By default this is the value returned by get_index_name()
Returns:

A Boolean indicating if the object was removed.

classmethod queryset_iterator(queryset)

Iterate (using a generator) over a given queryset.

You must implement this yourself.

Parameters:queryset (A type which provides an interface this method knows how to iterate over.) – The queryset to iterate over.
Yields:Instances of the object to index.
classmethod save_handler(sender, instance)

A target for wiring to a save signal for your model.

Parameters:
  • sender – The class which initated the save.
  • instance – The instance being saved.
classmethod should_index(obj)

Determines if an obj should be indexed.

Defaults to True

Parameters:obj (A python object which you are indexing.) – The object to generate a document for.
Returns:A Boolean.
class printen.mixins.PeeweeElasticsearchIndexMixin

A class that inherits from ElasticsearchIndexMixin, overriding the relevant methods with Peewee related defaults.

classmethod get_document_id(obj)

Provides a sane default for Peewee models. Override to modify behavior.

Parameters:obj (A subclass of peewee.Model) – The object to index.
Returns:getattr(obj, obj._meta.primary_key.name) which is an integer document id
Raises:MissingObjectError
classmethod get_queryset()

Provides a sane default for Peewee models. Override to modify behavior.

Returns:cls.model.select() - a Peewee queryset
classmethod get_type_name()

Provides a sane default for Peewee models. Override to modify behavior.

Returns:cls.model._meta.name
classmethod queryset_iterator(queryset)

Provides a sane default for Peewee models. Override to modify behavior.

Executes the given queryset and iterates over the results, yielding them one at a time to the caller.

Parameters:queryset (A Peewee queryset.) – A Peewee queryset to iterate over.
Returns:cls.model.select() - a Peewee queryset

The utils module provides various utility functions, mostly useful to the mixin class but occasionally useful elsewhere.

printen.utils.find_search_types(module, base=<class 'printen.mixins.ElasticsearchIndexMixin'>)

Returns a list of automatically detected search type classes. Useful for populating ELASTICSEARCH_TYPE_CLASSES.

Parameters:
  • module (A Python module.) – The module to search.
  • base (A Python class.) – A class that your search type classes inherit from.
Returns:

A list of type classes that inherit from base.

printen.utils.get_alias_names(aliases=None)

Return unique names from aliases

Parameters:aliases – A list alias/index tuples to get names of.
Returns:A unique list of alias names
printen.utils.get_indices(indices=None)

Returns indices and their search type classes

Parameters:indices (list.) – Filter returned indices by this list.
Returns:A dictionary containing indices as keys and a list of search classes as values.
Raises:Misconfigured
printen.utils.get_indices_from_aliases(search_aliases=None)

Returns indices associated with aliases

Parameters:search_aliases (list.) – Search aliases to get indices for.
Returns:A list of indices