Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add "exists" function into Document class #1447

Merged
merged 1 commit into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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