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
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def write_documents(
wait=self.wait_result_from_api,
)

progress_bar.update(self.write_batch_size)
progress_bar.update(len(document_batch))
return len(document_objects)

async def write_documents_async(
Expand Down Expand Up @@ -522,7 +522,7 @@ async def write_documents_async(
wait=self.wait_result_from_api,
)

progress_bar.update(self.write_batch_size)
progress_bar.update(len(document_batch))
return len(document_objects)

def delete_documents(self, document_ids: list[str]) -> None:
Expand Down Expand Up @@ -1664,7 +1664,7 @@ def _query_by_sparse(
with_vectors=return_embedding,
score_threshold=score_threshold,
).groups
return self._process_group_results(groups)
return self._process_group_results(groups, scale_score=scale_score)
else:
points = self._client.query_points(
collection_name=self.index,
Expand Down Expand Up @@ -1726,7 +1726,7 @@ def _query_by_embedding(
with_vectors=return_embedding,
score_threshold=score_threshold,
).groups
return self._process_group_results(groups)
return self._process_group_results(groups, scale_score=scale_score)

else:
points = self._client.query_points(
Expand Down Expand Up @@ -1924,7 +1924,7 @@ async def _query_by_sparse_async(
score_threshold=score_threshold,
)
groups = response.groups
return self._process_group_results(groups)
return self._process_group_results(groups, scale_score=scale_score)
else:
query_response = await self._async_client.query_points(
collection_name=self.index,
Expand Down Expand Up @@ -1988,7 +1988,7 @@ async def _query_by_embedding_async(
score_threshold=score_threshold,
)
groups = response.groups
return self._process_group_results(groups)
return self._process_group_results(groups, scale_score=scale_score)
else:
query_response = await self._async_client.query_points(
collection_name=self.index,
Expand Down Expand Up @@ -2579,19 +2579,17 @@ def _process_query_point_results(

return documents

def _process_group_results(self, groups: list[rest.PointGroup]) -> list[Document]:
def _process_group_results(self, groups: list[rest.PointGroup], scale_score: bool = False) -> list[Document]:
"""
Processes grouped query results from Qdrant.

"""
if not groups:
return []

return [
convert_qdrant_point_to_haystack_document(point, use_sparse_embeddings=self.use_sparse_embeddings)
for group in groups
for point in group.hits
]
return self._process_query_point_results(
[point for group in groups for point in group.hits], scale_score=scale_score
)

def _validate_collection_compatibility(
self,
Expand Down
30 changes: 30 additions & 0 deletions integrations/qdrant/tests/test_document_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,36 @@ def test_close_is_exception_safe(self):

assert document_store._client is None

@pytest.mark.parametrize(
"method_name, query_kwargs",
[
("_query_by_embedding", {"query_embedding": [0.1] * 768}),
("_query_by_sparse", {"query_sparse_embedding": SparseEmbedding(indices=[0], values=[0.1])}),
],
)
@pytest.mark.parametrize("scale_score, expected_score", [(True, 0.75), (False, 0.5)])
def test_query_with_group_by_scale_score(self, method_name, query_kwargs, scale_score, expected_score):
document_store = QdrantDocumentStore(location=":memory:", similarity="cosine", use_sparse_embeddings=True)
mock_client = MagicMock()
point = rest.ScoredPoint(id=1, version=0, score=0.5, payload={"id": "1", "content": "doc"})
mock_client.query_points_groups.return_value.groups = [rest.PointGroup(id="a", hits=[point])]
document_store._client = mock_client

result = getattr(document_store, method_name)(**query_kwargs, group_by="meta.group", scale_score=scale_score)

assert [doc.score for doc in result] == [expected_score]

def test_write_documents_progress_bar_counts_documents(self):
document_store = QdrantDocumentStore(location=":memory:", write_batch_size=2)
document_store._client = MagicMock()
with patch("haystack_integrations.document_stores.qdrant.document_store.tqdm") as mock_tqdm:
document_store.write_documents(
[Document(content=str(i)) for i in range(3)], policy=DuplicatePolicy.OVERWRITE
)

progress_bar = mock_tqdm.return_value.__enter__.return_value
assert [c.args[0] for c in progress_bar.update.call_args_list] == [2, 1]


@pytest.mark.integration
class TestQdrantDocumentStore(
Expand Down
32 changes: 32 additions & 0 deletions integrations/qdrant/tests/test_document_store_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,38 @@ async def test_close_async_is_exception_safe(self):

assert document_store._async_client is None

@pytest.mark.parametrize(
"method_name, query_kwargs",
[
("_query_by_embedding_async", {"query_embedding": [0.1] * 768}),
("_query_by_sparse_async", {"query_sparse_embedding": SparseEmbedding(indices=[0], values=[0.1])}),
],
)
@pytest.mark.parametrize("scale_score, expected_score", [(True, 0.75), (False, 0.5)])
async def test_query_with_group_by_scale_score_async(self, method_name, query_kwargs, scale_score, expected_score):
document_store = QdrantDocumentStore(location=":memory:", similarity="cosine", use_sparse_embeddings=True)
mock_client = AsyncMock()
point = rest.ScoredPoint(id=1, version=0, score=0.5, payload={"id": "1", "content": "doc"})
mock_client.query_points_groups.return_value = rest.GroupsResult(groups=[rest.PointGroup(id="a", hits=[point])])
document_store._async_client = mock_client

result = await getattr(document_store, method_name)(
**query_kwargs, group_by="meta.group", scale_score=scale_score
)

assert [doc.score for doc in result] == [expected_score]

async def test_write_documents_async_progress_bar_counts_documents(self):
document_store = QdrantDocumentStore(location=":memory:", write_batch_size=2)
document_store._async_client = AsyncMock()
with patch("haystack_integrations.document_stores.qdrant.document_store.tqdm") as mock_tqdm:
await document_store.write_documents_async(
[Document(content=str(i)) for i in range(3)], policy=DuplicatePolicy.OVERWRITE
)

progress_bar = mock_tqdm.return_value.__enter__.return_value
assert [c.args[0] for c in progress_bar.update.call_args_list] == [2, 1]


@pytest.mark.integration
@pytest.mark.asyncio
Expand Down
Loading