Skip to content
Open
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
27 changes: 23 additions & 4 deletions src/typesense/types/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,23 +915,42 @@ class DeleteSingleDocumentParameters(typing.TypedDict):
ignore_not_found: typing.NotRequired[bool]


class DeleteQueryParameters(typing.TypedDict):
class TruncateDeleteParameters(typing.TypedDict):
"""
Parameters for deleting documents.
Parameters for truncating a collection (deleting all documents, keeping schema).

Attributes:
truncate (bool): Truncate the collection, keeping just the schema.
"""

truncate: bool


class FilterDeleteParameters(typing.TypedDict):
"""
Parameters for deleting documents by filter.

Attributes:
truncate (str): Truncate the collection, keeping just the schema.
filter_by (str): Filter to apply to documents.
batch_size (int): Batch size for deleting documents.
ignore_not_found (bool): Ignore not found documents.
"""

truncate: typing.NotRequired[bool]
filter_by: str
batch_size: typing.NotRequired[int]
ignore_not_found: typing.NotRequired[bool]


DeleteQueryParameters = typing.Union[TruncateDeleteParameters, FilterDeleteParameters]
"""
Discriminated union of parameters for deleting documents.

Either:
- TruncateDeleteParameters: Use truncate to delete all documents, keeping the schema.
- FilterDeleteParameters: Use filter_by (and optionally batch_size, ignore_not_found) to delete specific documents.
"""


class DeleteResponse(typing.TypedDict):
"""
Response from deleting documents.
Expand Down
11 changes: 11 additions & 0 deletions tests/documents_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ def test_delete(
assert response == {"num_deleted": 1}


def test_truncate(
actual_documents: Documents[Companies],
delete_all: None,
create_collection: None,
create_document: None,
) -> None:
"""Test that the Documents object can delete a document from Typesense server."""
response = actual_documents.delete({"truncate": True})
assert response == {"num_deleted": 1}


def test_delete_ignore_missing(
actual_documents: Documents[Companies],
delete_all: None,
Expand Down