@@ -708,4 +708,65 @@ def test_get_field_min_max(self, document_store: OpenSearchDocumentStore):
708708 # Test with float values
709709 min_max_score = document_store .get_field_min_max ("meta.rating" )
710710 assert min_max_score ["min" ] == pytest .approx (5.2 )
711- assert min_max_score ["max" ] == pytest .approx (20.3 )
711+ assert min_max_score ["max" ] == pytest .approx (20.3 )
712+
713+ def test_get_field_unique_values (self , document_store : OpenSearchDocumentStore ):
714+ # Test with string values
715+ docs = [
716+ Document (content = "Python programming" , meta = {"category" : "A" , "language" : "Python" }),
717+ Document (content = "Java programming" , meta = {"category" : "B" , "language" : "Java" }),
718+ Document (content = "Python scripting" , meta = {"category" : "A" , "language" : "Python" }),
719+ Document (content = "JavaScript development" , meta = {"category" : "C" , "language" : "JavaScript" }),
720+ Document (content = "Python data science" , meta = {"category" : "A" , "language" : "Python" }),
721+ Document (content = "Java backend" , meta = {"category" : "B" , "language" : "Java" }),
722+ ]
723+ document_store .write_documents (docs )
724+
725+ # Test getting all unique values without search term
726+ unique_values , total_count = document_store .get_field_unique_values ("meta.category" , None , 0 , 10 )
727+ assert set (unique_values ) == {"A" , "B" , "C" }
728+ assert total_count == 3
729+
730+ # Test with "meta." prefix
731+ unique_languages , lang_count = document_store .get_field_unique_values ("meta.language" , None , 0 , 10 )
732+ assert set (unique_languages ) == {"Python" , "Java" , "JavaScript" }
733+ assert lang_count == 3
734+
735+ # Test pagination - first page
736+ unique_values_page1 , total_count = document_store .get_field_unique_values ("meta.category" , None , 0 , 2 )
737+ assert len (unique_values_page1 ) == 2
738+ assert total_count == 3
739+ assert all (val in ["A" , "B" , "C" ] for val in unique_values_page1 )
740+
741+ # Test pagination - second page
742+ unique_values_page2 , total_count = document_store .get_field_unique_values ("meta.category" , None , 2 , 2 )
743+ assert len (unique_values_page2 ) == 1
744+ assert total_count == 3
745+ assert unique_values_page2 [0 ] in ["A" , "B" , "C" ]
746+
747+ # Test with search term - filter by content matching "Python"
748+ unique_values_filtered , total_count = document_store .get_field_unique_values ("meta.category" , "Python" , 0 , 10 )
749+ assert set (unique_values_filtered ) == {"A" } # Only category A has documents with "Python" in content
750+ assert total_count == 1
751+
752+ # Test with search term - filter by content matching "Java"
753+ unique_values_java , total_count = document_store .get_field_unique_values ("meta.category" , "Java" , 0 , 10 )
754+ assert set (unique_values_java ) == {"B" } # Only category B has documents with "Java" in content
755+ assert total_count == 1
756+
757+ # Test with integer values
758+ int_docs = [
759+ Document (content = "Doc 1" , meta = {"priority" : 1 }),
760+ Document (content = "Doc 2" , meta = {"priority" : 2 }),
761+ Document (content = "Doc 3" , meta = {"priority" : 1 }),
762+ Document (content = "Doc 4" , meta = {"priority" : 3 }),
763+ ]
764+ document_store .write_documents (int_docs )
765+ unique_priorities , priority_count = document_store .get_field_unique_values ("meta.priority" , None , 0 , 10 )
766+ assert set (unique_priorities ) == {"1" , "2" , "3" }
767+ assert priority_count == 3
768+
769+ # Test with search term on integer field
770+ unique_priorities_filtered , priority_count = document_store .get_field_unique_values ("meta.priority" , "Doc 1" , 0 , 10 )
771+ assert set (unique_priorities_filtered ) == {"1" }
772+ assert priority_count == 1
0 commit comments