From 22d6045a697b552b93869e12c3de94d92e7af321 Mon Sep 17 00:00:00 2001 From: goodnight Date: Wed, 28 Oct 2026 17:54:06 +0100 Subject: [PATCH 1/2] fix: qdrant - apply scale_score to grouped results and fix write progress bar --- .../document_stores/qdrant/document_store.py | 22 ++++++++-------- .../qdrant/tests/test_document_store.py | 25 +++++++++++++++++++ .../qdrant/tests/test_document_store_async.py | 11 ++++++++ 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/integrations/qdrant/src/haystack_integrations/document_stores/qdrant/document_store.py b/integrations/qdrant/src/haystack_integrations/document_stores/qdrant/document_store.py index b13275169c..27fc5cbd2d 100644 --- a/integrations/qdrant/src/haystack_integrations/document_stores/qdrant/document_store.py +++ b/integrations/qdrant/src/haystack_integrations/document_stores/qdrant/document_store.py @@ -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( @@ -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: @@ -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, @@ -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( @@ -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, @@ -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, @@ -2579,7 +2579,7 @@ 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. @@ -2587,11 +2587,9 @@ def _process_group_results(self, groups: list[rest.PointGroup]) -> list[Document 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, diff --git a/integrations/qdrant/tests/test_document_store.py b/integrations/qdrant/tests/test_document_store.py index 88516f46b2..d9cf37f34c 100644 --- a/integrations/qdrant/tests/test_document_store.py +++ b/integrations/qdrant/tests/test_document_store.py @@ -452,6 +452,31 @@ def test_close_is_exception_safe(self): assert document_store._client is None + @pytest.mark.parametrize("scale_score, expected_score", [(True, 0.75), (False, 0.5)]) + def test_query_by_embedding_with_group_by_scale_score(self, scale_score, expected_score): + document_store = QdrantDocumentStore(location=":memory:", similarity="cosine") + 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 = document_store._query_by_embedding( + query_embedding=[0.1] * 768, 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( diff --git a/integrations/qdrant/tests/test_document_store_async.py b/integrations/qdrant/tests/test_document_store_async.py index 703bc9c17a..d912bb864b 100644 --- a/integrations/qdrant/tests/test_document_store_async.py +++ b/integrations/qdrant/tests/test_document_store_async.py @@ -207,6 +207,17 @@ async def test_close_async_is_exception_safe(self): assert document_store._async_client is None + 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 From 0ed57807f881ee4d7de04a0d99b1936bc664eaed Mon Sep 17 00:00:00 2001 From: "David S. Batista" Date: Mon, 9 Nov 2026 16:54:43 +0100 Subject: [PATCH 2/2] test: qdrant - cover scale_score with group_by for sparse and async queries --- .../qdrant/tests/test_document_store.py | 15 ++++++++----- .../qdrant/tests/test_document_store_async.py | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/integrations/qdrant/tests/test_document_store.py b/integrations/qdrant/tests/test_document_store.py index d9cf37f34c..fc222d3a2d 100644 --- a/integrations/qdrant/tests/test_document_store.py +++ b/integrations/qdrant/tests/test_document_store.py @@ -452,17 +452,22 @@ 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_by_embedding_with_group_by_scale_score(self, scale_score, expected_score): - document_store = QdrantDocumentStore(location=":memory:", similarity="cosine") + 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 = document_store._query_by_embedding( - query_embedding=[0.1] * 768, group_by="meta.group", scale_score=scale_score - ) + 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] diff --git a/integrations/qdrant/tests/test_document_store_async.py b/integrations/qdrant/tests/test_document_store_async.py index d912bb864b..642b1a3797 100644 --- a/integrations/qdrant/tests/test_document_store_async.py +++ b/integrations/qdrant/tests/test_document_store_async.py @@ -207,6 +207,27 @@ 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()