Skip to content

Commit e4dae43

Browse files
committed
add function into Document class
1 parent 58c0273 commit e4dae43

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

tests/test_integration/test_document.py

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

335335

336+
def test_exists_return_true(data_client):
337+
assert Repository.exists("elasticsearch-dsl-py")
338+
339+
340+
def test_exists_false(data_client):
341+
assert not Repository.exists("elasticsearch-dsl-php")
342+
343+
336344
def test_get_with_tz_date(data_client):
337345
first_commit = Commit.get(
338346
id="3ca6e1e73a071a705b4babd2f581c91a2a3e5037", routing="elasticsearch-dsl-py"

0 commit comments

Comments
 (0)