@@ -196,10 +196,11 @@ def test_compute_field_min_max_skips_non_scalar_values(self):
196196 @pytest .mark .parametrize (
197197 "result" ,
198198 [
199- {"ids" : ["1" ], "documents" : None , "metadatas" : [{"cat" : "A" }]},
200- {"ids" : ["1" ], "documents" : ["hello world" ], "metadatas" : [{"cat" : "A" }]},
199+ {"ids" : ["1" ], "metadatas" : [{"cat" : "A" }]},
200+ {"ids" : ["1" ], "metadatas" : [None ]},
201+ {"ids" : ["1" ], "metadatas" : [{"other" : "A" }]},
201202 ],
202- ids = ["documents_none " , "no_matches " ],
203+ ids = ["no_match " , "metadata_none" , "field_missing " ],
203204 )
204205 def test_compute_field_unique_values_with_search_term_edge_cases (self , result ):
205206 values , total = ChromaDocumentStore ._compute_field_unique_values (result , "cat" , "absent" , 0 , 10 )
@@ -741,12 +742,57 @@ def test_get_metadata_field_unique_values_pagination(self, populated_store):
741742 assert sorted (all_values ) == ["A" , "B" , "C" ]
742743
743744 def test_get_metadata_field_unique_values_with_search_term (self , populated_store ):
744- """Test getting unique values filtered by search term"""
745- # Search for documents containing "Doc 1"
745+ """Test getting unique values filtered by search term.
746+
747+ The search term is matched against the metadata field's own value, not document
748+ content. "Doc 1" is content for one document (category "A"), but it is not a
749+ substring of any "category" value, so no values should match.
750+ """
746751 values , total = populated_store .get_metadata_field_unique_values (
747752 "category" , search_term = "Doc 1" , from_ = 0 , size = 10
748753 )
749- assert values == ["A" ] # Only Doc 1 has category A
754+ assert values == []
755+ assert total == 0
756+
757+ def test_get_metadata_field_unique_values_search_term_matches_field_value (self , populated_store ):
758+ """Search term matches when it's a case-insensitive substring of the field's value."""
759+ values , total = populated_store .get_metadata_field_unique_values ("status" , search_term = "ACT" , from_ = 0 , size = 10 )
760+ # "ACT" is a substring of both "active" and "inactive" (case-insensitive)
761+ assert sorted (values ) == ["active" , "inactive" ]
762+ assert total == 2
763+
764+ values , total = populated_store .get_metadata_field_unique_values ("status" , search_term = "ina" , from_ = 0 , size = 10 )
765+ assert values == ["inactive" ]
766+ assert total == 1
767+
768+ def test_get_metadata_field_unique_values_search_term_excludes_content_only_match (self , document_store ):
769+ """A search term present only in document content (not in the metadata field value)
770+ must not match, proving search_term no longer filters on content."""
771+ docs = [
772+ Document (content = "unique-marker-xyz" , meta = {"category" : "A" }),
773+ Document (content = "plain content" , meta = {"category" : "B" }),
774+ ]
775+ document_store .write_documents (docs )
776+
777+ values , total = document_store .get_metadata_field_unique_values (
778+ "category" , search_term = "unique-marker-xyz" , from_ = 0 , size = 10
779+ )
780+ assert values == []
781+ assert total == 0
782+
783+ def test_get_metadata_field_unique_values_search_term_matches_field_value_not_content (self , document_store ):
784+ """A search term present in the metadata field's value but absent from the content
785+ must match, proving search_term filters on the metadata field's value."""
786+ docs = [
787+ Document (content = "Nothing special here" , meta = {"category" : "special-value" }),
788+ Document (content = "Nothing special here either" , meta = {"category" : "other" }),
789+ ]
790+ document_store .write_documents (docs )
791+
792+ values , total = document_store .get_metadata_field_unique_values (
793+ "category" , search_term = "SPECIAL" , from_ = 0 , size = 10
794+ )
795+ assert values == ["special-value" ]
750796 assert total == 1
751797
752798 def test_get_metadata_field_unique_values_field_normalization (self , populated_store ):
0 commit comments