Skip to content

Commit

Permalink
deepset-ai#1047 Remove count_documents from delete_documents
Browse files Browse the repository at this point in the history
Removed the expensive check to see if the collection is non-empty by performing a full count.

This is to fix issue deepset-ai#1047
  • Loading branch information
brendancicchi committed Sep 4, 2024
1 parent 129ba54 commit 4c94dc7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,24 @@ def find_documents(self, find_query):
else:
logger.warning(f"No documents found: {response_dict}")

def find_one_document(self, find_query):
"""
Find one document in the Astra index.
:param find_query: a dictionary with the query options
:returns: the document found in the index
"""
response_dict = self._astra_db_collection.find_one(
filter=find_query.get("filter"),
options=find_query.get("options"),
projection={"*": 1},
)

if "data" in response_dict and "document" in response_dict["data"]:
return response_dict["data"]["document"]
else:
logger.warning(f"No document found: {response_dict}")

def get_documents(self, ids: List[str], batch_size: int = 20) -> QueryResponse:
"""
Get documents from the Astra index by their ids.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ def delete_documents(self, document_ids: Optional[List[str]] = None, delete_all:
:param delete_all: if `True`, delete all documents.
:raises MissingDocumentError: if no document was deleted but document IDs were provided.
"""
deletion_counter = 0
if self.index.count_documents() > 0:
if self.index.find_one_document({"filter": {}}) is not None:
deletion_counter = 0
if document_ids is not None:
for batch in _batches(document_ids, MAX_BATCH_SIZE):
deletion_counter += self.index.delete(ids=batch)
Expand Down

0 comments on commit 4c94dc7

Please sign in to comment.