Skip to content
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
12 changes: 10 additions & 2 deletions src/typesense/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
import sys

from typesense.api_call import ApiCall
from typesense.types.document import DirtyValuesParameters, DocumentSchema
from typesense.types.document import (
DeleteSingleDocumentParameters,
DirtyValuesParameters,
DocumentSchema,
)

if sys.version_info >= (3, 11):
import typing
Expand Down Expand Up @@ -101,7 +105,10 @@ def update(
)
return typing.cast(TDoc, response)

def delete(self) -> TDoc:
def delete(
self,
delete_parameters: typing.Union[DeleteSingleDocumentParameters, None] = None,
) -> TDoc:
"""
Delete this specific document.

Expand All @@ -111,6 +118,7 @@ def delete(self) -> TDoc:
response: TDoc = self.api_call.delete(
self._endpoint_path,
entity_type=typing.Dict[str, str],
params=delete_parameters,
)
return response

Expand Down
11 changes: 11 additions & 0 deletions src/typesense/types/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,17 @@ class SearchResponse(typing.Generic[TDoc], typing.TypedDict):
conversation: typing.NotRequired[Conversation]


class DeleteSingleDocumentParameters(typing.TypedDict):
"""
Parameters for deleting a single document.

Attributes:
ignore_not_found (bool): Ignore not found documents.
"""

ignore_not_found: typing.NotRequired[bool]


class DeleteQueryParameters(typing.TypedDict):
"""
Parameters for deleting documents.
Expand Down
27 changes: 27 additions & 0 deletions tests/document_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import pytest
import requests_mock

from tests.fixtures.document_fixtures import Companies
Expand All @@ -13,6 +14,7 @@
from typesense.api_call import ApiCall
from typesense.document import Document
from typesense.documents import Documents
from typesense.exceptions import ObjectNotFound


def test_init(fake_api_call: ApiCall) -> None:
Expand Down Expand Up @@ -133,3 +135,28 @@ def test_actual_delete(
"company_name": "Company",
"num_employees": 10,
}


def test_actual_delete_non_existent(
actual_documents: Documents,
delete_all: None,
create_collection: None,
create_document: None,
) -> None:
"""Test that the Document object can delete an document from Typesense Server."""
with pytest.raises(ObjectNotFound):
actual_documents["1"].delete()


def test_actual_delete_non_existent_ignore_not_found(
actual_documents: Documents,
delete_all: None,
create_collection: None,
create_document: None,
) -> None:
"""Test that the Document object can delete an document from Typesense Server."""
response = actual_documents["1"].delete(
delete_parameters={"ignore_not_found": True},
)

assert response == {"id": "1"}