API Documentation

Below please find the documentation for the public classes and functions of elasticsearch_dsl.

Document

class elasticsearch_dsl.DocType(meta=None, **kwargs)

Model-like class for persisting documents in elasticsearch.

delete(using=None, index=None, **kwargs)

Delete the instance in elasticsearch.

Parameters:
  • index – elasticsearch index to use, if the DocType is associated with an index this can be omitted.
  • using – connection alias to use, defaults to 'default'

Any additional keyword arguments will be passed to Elasticsearch.delete unchanged.

classmethod from_es(hit)

Helper method to construct an instance from a dictionary returned by elasticsearch.

classmethod get(id, using=None, index=None, **kwargs)

Retrieve a single document from elasticsearch using it’s id.

Parameters:
  • idid of the document to be retireved
  • index – elasticsearch index to use, if the DocType is associated with an index this can be omitted.
  • using – connection alias to use, defaults to 'default'

Any additional keyword arguments will be passed to Elasticsearch.get unchanged.

classmethod init(index=None, using=None)

Create the index and populate the mappings in elasticsearch.

classmethod mget(docs, using=None, index=None, raise_on_error=True, missing='none', **kwargs)

Retrieve multiple document by their ids. Returns a list of instances in the same order as requested.

Parameters:
  • docs – list of ids of the documents to be retireved or a list of document specifications as per https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html
  • index – elasticsearch index to use, if the DocType is associated with an index this can be omitted.
  • using – connection alias to use, defaults to 'default'
  • missing – what to do when one of the documents requested is not found. Valid options are 'none' (use None), 'raise' (raise NotFoundError) or 'skip' (ignore the missing document).

Any additional keyword arguments will be passed to Elasticsearch.mget unchanged.

save(using=None, index=None, validate=True, **kwargs)

Save the document into elasticsearch. If the document doesn’t exist it is created, it is overwritten otherwise. Returns True if this operations resulted in new document being created.

Parameters:
  • index – elasticsearch index to use, if the DocType is associated with an index this can be omitted.
  • using – connection alias to use, defaults to 'default'
  • validate – set to False to skip validating the document

Any additional keyword arguments will be passed to Elasticsearch.index unchanged.

classmethod search(using=None, index=None)

Create an Search instance that will search over this DocType.

to_dict(include_meta=False)

Serialize the instance into a dictionary so that it can be saved in elasticsearch.

Parameters:include_meta – if set to True will include all the metadata (_index, _type, _id etc). Otherwise just the document’s data is serialized. This is useful when passing multiple instances into elasticsearch.helpers.bulk.
update(using=None, index=None, **fields)

Partial update of the document, specify fields you wish to update and both the instance and the document in elasticsearch will be updated:

doc = MyDocument(title='Document Title!')
doc.save()
doc.update(title='New Document Title!')
Parameters:
  • index – elasticsearch index to use, if the DocType is associated with an index this can be omitted.
  • using – connection alias to use, defaults to 'default'

Any additional keyword arguments will be passed to Elasticsearch.update unchanged.

Index

class elasticsearch_dsl.Index(name, using='default')
Parameters:
  • name – name of the index
  • using – connection alias to use, defaults to 'default'
aliases(**kwargs)

Add aliases to the index definition:

i = Index('blog-v2')
i.aliases(blog={}, published={'filter': Q('term', published=True)})
analyzer(analyzer)

Explicitly add an analyzer to an index. Note that all custom analyzers defined in mappings will also be created. This is useful for search analyzers.

Example:

from elasticsearch_dsl import analyzer, tokenizer

my_analyzer = analyzer('my_analyzer',
    tokenizer=tokenizer('trigram', 'nGram', min_gram=3, max_gram=3),
    filter=['lowercase']
)

i = Index('blog')
i.analyzer(my_analyzer)
clone(name, using=None)

Create a copy of the instance with another name or connection alias. Useful for creating multiple indices with shared configuration:

i = Index('base-index')
i.settings(number_of_shards=1)
i.create()

i2 = i.clone('other-index')
i2.create()
Parameters:
  • name – name of the index
  • using – connection alias to use, defaults to 'default'
close(**kwargs)

Closes the index in elasticsearch.

Any additional keyword arguments will be passed to Elasticsearch.indices.close unchanged.

create(**kwargs)

Creates the index in elasticsearch.

Any additional keyword arguments will be passed to Elasticsearch.indices.create unchanged.

delete(**kwargs)

Deletes the index in elasticsearch.

Any additional keyword arguments will be passed to Elasticsearch.indices.delete unchanged.

doc_type(doc_type)

Associate a DocType subclass with an index. This means that, when this index is created, it will contain the mappings fo the DocType. If the DocType class doesn’t have a default index yet, name of the Index instance will be used. Can be used as a decorator:

i = Index('blog')

@i.doc_type
class Post(DocType):
    title = Text()

# create the index, including Post mappings
i.create()

# .search() will now return a Search object that will return
# properly deserialized Post instances
s = i.search()
exists(**kwargs)

Returns True if the index already exists in elasticsearch.

Any additional keyword arguments will be passed to Elasticsearch.indices.exists unchanged.

flush(**kwargs)

Preforms a flush operation on the index.

Any additional keyword arguments will be passed to Elasticsearch.indices.flush unchanged.

open(**kwargs)

Opens the index in elasticsearch.

Any additional keyword arguments will be passed to Elasticsearch.indices.open unchanged.

refresh(**kwargs)

Preforms a refresh operation on the index.

Any additional keyword arguments will be passed to Elasticsearch.indices.refresh unchanged.

search()

Rteurn a Search object searching over this index and its DocTypes.

settings(**kwargs)

Add settings to the index:

i = Index('i')
i.settings(number_of_shards=1, number_of_replicas=0)

Multiple calls to settings will merge the keys, later overriding the earlier.