Skip to content

Commit 7db675e

Browse files
committed
adding async version + tests
1 parent ca3532b commit 7db675e

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

integrations/qdrant/src/haystack_integrations/document_stores/qdrant/document_store.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,57 @@ def delete_all_documents(self, recreate_index: bool = False) -> None:
574574
f"Error {e} when calling QdrantDocumentStore.delete_all_documents()",
575575
)
576576

577+
async def delete_all_documents_async(self, recreate_index: bool = False) -> None:
578+
"""
579+
Asynchronously deletes all documents from the document store.
580+
581+
:param recreate_index: Whether to recreate the index after deleting all documents.
582+
"""
583+
584+
await self._initialize_async_client()
585+
assert self._async_client is not None
586+
587+
if recreate_index:
588+
# get current collection config as json
589+
collection_info = await self._async_client.get_collection(collection_name=self.index)
590+
info_json = collection_info.model_dump()
591+
592+
# deal with the Optional use_sparse_embeddings
593+
sparse_vectors = info_json["config"]["params"]["sparse_vectors"]
594+
use_sparse_embeddings = sparse_vectors if sparse_vectors else False
595+
596+
# deal with the Optional sparse_idf
597+
hnsw_config = info_json["config"]["params"]["vectors"].get("config", {}).get("hnsw_config", None)
598+
sparse_idf = hnsw_config if use_sparse_embeddings and hnsw_config else False
599+
600+
# recreate collection
601+
await self._set_up_collection_async(
602+
collection_name=self.index,
603+
embedding_dim=info_json["config"]["params"]["vectors"]["size"],
604+
recreate_collection=True,
605+
similarity=info_json["config"]["params"]["vectors"]["distance"].lower(),
606+
use_sparse_embeddings=use_sparse_embeddings,
607+
sparse_idf=sparse_idf,
608+
on_disk=info_json["config"]["hnsw_config"]["on_disk"],
609+
payload_fields_to_index=info_json["payload_schema"],
610+
)
611+
612+
else:
613+
try:
614+
await self._async_client.delete(
615+
collection_name=self.index,
616+
points_selector=rest.FilterSelector(
617+
filter=rest.Filter(
618+
must=[],
619+
)
620+
),
621+
wait=self.wait_result_from_api,
622+
)
623+
except Exception as e:
624+
logger.warning(
625+
f"Error {e} when calling QdrantDocumentStore.delete_all_documents_async()",
626+
)
627+
577628
@classmethod
578629
def from_dict(cls, data: Dict[str, Any]) -> "QdrantDocumentStore":
579630
"""

integrations/qdrant/tests/test_document_store_async.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,43 @@ async def test_set_up_collection_with_distance_mismatch_async(self):
218218
):
219219
with pytest.raises(ValueError, match="different similarity"):
220220
await document_store._set_up_collection_async("test_collection", 768, False, "cosine", False, False)
221+
222+
@pytest.mark.asyncio
223+
async def test_delete_all_documents_async_no_index_recreation(self, document_store):
224+
await document_store._initialize_async_client()
225+
226+
# write some documents
227+
docs = [Document(id=str(i)) for i in range(5)]
228+
await document_store.write_documents_async(docs)
229+
230+
# delete all documents without recreating the index
231+
await document_store.delete_all_documents_async(recreate_index=False)
232+
assert await document_store.count_documents_async() == 0
233+
234+
# ensure the collection still exists by writing documents again
235+
await document_store.write_documents_async(docs)
236+
assert await document_store.count_documents_async() == 5
237+
238+
@pytest.mark.asyncio
239+
async def test_delete_all_documents_async_index_recreation(self, document_store):
240+
await document_store._initialize_async_client()
241+
242+
# write some documents
243+
docs = [Document(id=str(i)) for i in range(5)]
244+
await document_store.write_documents_async(docs)
245+
246+
# get the current document_store config
247+
config_before = await document_store._async_client.get_collection(document_store.index)
248+
249+
# delete all documents with recreating the index
250+
await document_store.delete_all_documents_async(recreate_index=True)
251+
assert await document_store.count_documents_async() == 0
252+
253+
# assure that with the same config
254+
config_after = await document_store._async_client.get_collection(document_store.index)
255+
256+
assert config_before.config == config_after.config
257+
258+
# ensure the collection still exists by writing documents again
259+
await document_store.write_documents_async(docs)
260+
assert await document_store.count_documents_async() == 5

0 commit comments

Comments
 (0)