Repository navigation
fix(vector_store): return stored metadata in MilvusCollection.search() (#1330) #1343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ZohaibHassan16
merged 5 commits into
semantica-agi:main
from
mhaye9545:fix/issue-1330-milvus-search-metadata
Sep 19, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a64bc80
fix(vector_store): return stored metadata in MilvusCollection.search(…
mhaye9545 f4ff73d
Merge branch 'main' into fix/issue-1330-milvus-search-metadata
ZohaibHassan16 808f42b
Merge branch 'main' into fix/issue-1330-milvus-search-metadata
ZohaibHassan16 382de79
test(vector_store): stub entity.get on Milvus mock hit in deepdive test
ZohaibHassan16 4887877
Merge branch 'main' into fix/issue-1330-milvus-search-metadata
ZohaibHassan16 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
tests/vector_store/test_milvus_store_search_metadata.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| """Tests for MilvusCollection.search() metadata fix (#1330). | ||
|
|
||
| The schema produced by create_collection() includes a JSON `metadata` field. | ||
| Before the fix, search() hardcoded ``"metadata": {}`` regardless of what was | ||
| stored. After the fix it passes ``output_fields=["metadata"]`` to the Milvus | ||
| SDK and reads the actual value from ``hit.entity.get("metadata")``. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any, Dict, List, Optional | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Helpers — minimal stubs so we never need a real Milvus server | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def _make_hit(id_: str, distance: float, metadata: Optional[Dict[str, Any]]) -> MagicMock: | ||
| """Return a mock Milvus search hit with a populated entity.""" | ||
| hit = MagicMock() | ||
| hit.id = id_ | ||
| hit.distance = distance | ||
| hit.entity.get = MagicMock(side_effect=lambda key, default=None: metadata if key == "metadata" else default) | ||
| return hit | ||
|
|
||
|
|
||
| def _make_collection(hits_per_query: List[List[MagicMock]]) -> MagicMock: | ||
| """Return a mock Milvus Collection whose search() yields *hits_per_query*.""" | ||
| collection = MagicMock() | ||
| collection.search.return_value = hits_per_query | ||
| return collection | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Tests | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("milvus_available", [True]) | ||
| def test_search_returns_stored_metadata(milvus_available: bool) -> None: | ||
| """search() must return the metadata stored in the collection, not {}.""" | ||
| stored_meta = {"category": "science", "score": 0.95} | ||
| hit = _make_hit("vec-1", 0.1, stored_meta) | ||
| collection = _make_collection([[hit]]) | ||
|
|
||
| with patch("semantica.vector_store.milvus_store.MILVUS_AVAILABLE", milvus_available): | ||
| from semantica.vector_store.milvus_store import MilvusCollection | ||
|
|
||
| mc = MilvusCollection.__new__(MilvusCollection) | ||
| mc.collection = collection | ||
|
|
||
| results = mc.search( | ||
| vectors=[np.zeros(4)], | ||
| anns_field="vector", | ||
| param={"metric_type": "L2", "params": {"nprobe": 10}}, | ||
| limit=5, | ||
| ) | ||
|
|
||
| assert isinstance(results, list) | ||
| assert len(results) == 1 | ||
| assert results[0]["id"] == "vec-1" | ||
| assert results[0]["metadata"] == stored_meta, ( | ||
| "search() should return the stored metadata, not a hardcoded empty dict" | ||
| ) | ||
|
|
||
|
|
||
| def test_search_requests_metadata_output_field() -> None: | ||
| """The underlying collection.search() call must include 'metadata' in output_fields.""" | ||
| hit = _make_hit("vec-2", 0.2, {"source": "test"}) | ||
| collection = _make_collection([[hit]]) | ||
|
|
||
| with patch("semantica.vector_store.milvus_store.MILVUS_AVAILABLE", True): | ||
| from semantica.vector_store.milvus_store import MilvusCollection | ||
|
|
||
| mc = MilvusCollection.__new__(MilvusCollection) | ||
| mc.collection = collection | ||
|
|
||
| mc.search( | ||
| vectors=[np.zeros(4)], | ||
| anns_field="vector", | ||
| param={"metric_type": "L2", "params": {"nprobe": 10}}, | ||
| limit=3, | ||
| ) | ||
|
|
||
| call_kwargs = collection.search.call_args | ||
| output_fields = call_kwargs.kwargs.get("output_fields") or call_kwargs[1].get("output_fields") | ||
| assert output_fields is not None and "metadata" in output_fields, ( | ||
| "search() must pass output_fields=['metadata'] to the Milvus SDK" | ||
| ) | ||
|
|
||
|
|
||
| def test_search_falls_back_to_empty_dict_when_metadata_is_none() -> None: | ||
| """If the entity has no metadata (e.g. legacy record), return {} not None.""" | ||
| hit = _make_hit("vec-3", 0.5, None) | ||
| collection = _make_collection([[hit]]) | ||
|
|
||
| with patch("semantica.vector_store.milvus_store.MILVUS_AVAILABLE", True): | ||
| from semantica.vector_store.milvus_store import MilvusCollection | ||
|
|
||
| mc = MilvusCollection.__new__(MilvusCollection) | ||
| mc.collection = collection | ||
|
|
||
| results = mc.search( | ||
| vectors=[np.zeros(4)], | ||
| anns_field="vector", | ||
| param={"metric_type": "L2", "params": {"nprobe": 10}}, | ||
| limit=1, | ||
| ) | ||
|
|
||
| assert results[0]["metadata"] == {}, "None metadata from entity should fall back to {}" | ||
|
|
||
|
|
||
| def test_search_score_calculation() -> None: | ||
| """Verify score = 1/(1 + max(0, distance)) and distance is preserved.""" | ||
| distance = 0.4 | ||
| hit = _make_hit("vec-4", distance, {}) | ||
| collection = _make_collection([[hit]]) | ||
|
|
||
| with patch("semantica.vector_store.milvus_store.MILVUS_AVAILABLE", True): | ||
| from semantica.vector_store.milvus_store import MilvusCollection | ||
|
|
||
| mc = MilvusCollection.__new__(MilvusCollection) | ||
| mc.collection = collection | ||
|
|
||
| results = mc.search( | ||
| vectors=[np.zeros(4)], | ||
| anns_field="vector", | ||
| param={"metric_type": "L2", "params": {"nprobe": 10}}, | ||
| limit=1, | ||
| ) | ||
|
|
||
| expected_score = 1.0 / (1.0 + distance) | ||
| assert abs(results[0]["score"] - expected_score) < 1e-9 | ||
| assert results[0]["distance"] == distance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.