diff --git a/elasticsearch_dsl/document.py b/elasticsearch_dsl/document.py index 1ba146812..d68292d94 100644 --- a/elasticsearch_dsl/document.py +++ b/elasticsearch_dsl/document.py @@ -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 diff --git a/tests/test_integration/test_document.py b/tests/test_integration/test_document.py index 03bc8270a..908de1d21 100644 --- a/tests/test_integration/test_document.py +++ b/tests/test_integration/test_document.py @@ -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"