Skip to content

Commit 00ec643

Browse files
committed
add function into Document class
1 parent 1408aa9 commit 00ec643

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

elasticsearch_dsl/document.py

+16
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ def get(cls, id, using=None, index=None, **kwargs):
205205
return None
206206
return cls.from_es(doc)
207207

208+
@classmethod
209+
def exists(cls, id, using=None, index=None, **kwargs):
210+
"""
211+
check if exists a single document from elasticsearch using its ``id``.
212+
213+
:arg id: ``id`` of the document to check if exists
214+
:arg index: elasticsearch index to use, if the ``Document`` is
215+
associated with an index this can be omitted.
216+
:arg using: connection alias to use, defaults to ``'default'``
217+
218+
Any additional keyword arguments will be passed to
219+
``Elasticsearch.exists`` unchanged.
220+
"""
221+
es = cls._get_connection(using)
222+
return es.exists(index=cls._default_index(index), id=id, **kwargs)
223+
208224
@classmethod
209225
def mget(
210226
cls, docs, using=None, index=None, raise_on_error=True, missing="none", **kwargs

test_elasticsearch_dsl/test_integration/test_document.py

+8
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,14 @@ def test_get(data_client):
260260
assert datetime(2014, 3, 3) == elasticsearch_repo.created_at
261261

262262

263+
def test_exists_return_true(data_client):
264+
assert Repository.exists("elasticsearch-dsl-py")
265+
266+
267+
def test_exists_false(data_client):
268+
assert not Repository.exists("elasticsearch-dsl-php")
269+
270+
263271
def test_get_with_tz_date(data_client):
264272
first_commit = Commit.get(
265273
id="3ca6e1e73a071a705b4babd2f581c91a2a3e5037", routing="elasticsearch-dsl-py"

0 commit comments

Comments
 (0)