Skip to content

Commit

Permalink
Add Document.exists() function
Browse files Browse the repository at this point in the history
  • Loading branch information
dem4ply committed Jul 7, 2021
1 parent 58c0273 commit c089a40
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions elasticsearch_dsl/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ def get(cls, id, using=None, index=None, **kwargs):
return None
return cls.from_es(doc)

@classmethod
def exists(cls, id, using=None, index=None, **kwargs):
"""
check if exists a single document from elasticsearch using its ``id``.
:arg id: ``id`` of the document to check if exists
:arg index: elasticsearch index to use, if the ``Document`` is
associated with an index this can be omitted.
:arg using: connection alias to use, defaults to ``'default'``
Any additional keyword arguments will be passed to
``Elasticsearch.exists`` unchanged.
"""
es = cls._get_connection(using)
return es.exists(index=cls._default_index(index), id=id, **kwargs)

@classmethod
def mget(
cls, docs, using=None, index=None, raise_on_error=True, missing="none", **kwargs
Expand Down
8 changes: 8 additions & 0 deletions tests/test_integration/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,14 @@ def test_get(data_client):
assert datetime(2014, 3, 3) == elasticsearch_repo.created_at


def test_exists_return_true(data_client):
assert Repository.exists("elasticsearch-dsl-py")


def test_exists_false(data_client):
assert not Repository.exists("elasticsearch-dsl-php")


def test_get_with_tz_date(data_client):
first_commit = Commit.get(
id="3ca6e1e73a071a705b4babd2f581c91a2a3e5037", routing="elasticsearch-dsl-py"
Expand Down

0 comments on commit c089a40

Please sign in to comment.