@@ -499,4 +499,66 @@ async def test_get_field_min_max(self, document_store: OpenSearchDocumentStore):
499499 assert min_max_score ["min" ] == pytest .approx (5.2 )
500500 assert min_max_score ["max" ] == pytest .approx (20.3 )
501501
502+ @pytest .mark .asyncio
503+ async def test_get_field_unique_values (self , document_store : OpenSearchDocumentStore ):
504+ # Test with string values
505+ docs = [
506+ Document (content = "Python programming" , meta = {"category" : "A" , "language" : "Python" }),
507+ Document (content = "Java programming" , meta = {"category" : "B" , "language" : "Java" }),
508+ Document (content = "Python scripting" , meta = {"category" : "A" , "language" : "Python" }),
509+ Document (content = "JavaScript development" , meta = {"category" : "C" , "language" : "JavaScript" }),
510+ Document (content = "Python data science" , meta = {"category" : "A" , "language" : "Python" }),
511+ Document (content = "Java backend" , meta = {"category" : "B" , "language" : "Java" }),
512+ ]
513+ await document_store .write_documents_async (docs )
514+
515+ # Test getting all unique values without search term
516+ unique_values , total_count = await document_store .get_field_unique_values_async ("meta.category" , None , 0 , 10 )
517+ assert set (unique_values ) == {"A" , "B" , "C" }
518+ assert total_count == 3
519+
520+ # Test with "meta." prefix
521+ unique_languages , lang_count = await document_store .get_field_unique_values_async ("meta.language" , None , 0 , 10 )
522+ assert set (unique_languages ) == {"Python" , "Java" , "JavaScript" }
523+ assert lang_count == 3
524+
525+ # Test pagination - first page
526+ unique_values_page1 , total_count = await document_store .get_field_unique_values_async ("meta.category" , None , 0 , 2 )
527+ assert len (unique_values_page1 ) == 2
528+ assert total_count == 3
529+ assert all (val in ["A" , "B" , "C" ] for val in unique_values_page1 )
530+
531+ # Test pagination - second page
532+ unique_values_page2 , total_count = await document_store .get_field_unique_values_async ("meta.category" , None , 2 , 2 )
533+ assert len (unique_values_page2 ) == 1
534+ assert total_count == 3
535+ assert unique_values_page2 [0 ] in ["A" , "B" , "C" ]
536+
537+ # Test with search term - filter by content matching "Python"
538+ unique_values_filtered , total_count = await document_store .get_field_unique_values_async ("meta.category" , "Python" , 0 , 10 )
539+ assert set (unique_values_filtered ) == {"A" } # Only category A has documents with "Python" in content
540+ assert total_count == 1
541+
542+ # Test with search term - filter by content matching "Java"
543+ unique_values_java , total_count = await document_store .get_field_unique_values_async ("meta.category" , "Java" , 0 , 10 )
544+ assert set (unique_values_java ) == {"B" } # Only category B has documents with "Java" in content
545+ assert total_count == 1
546+
547+ # Test with integer values
548+ int_docs = [
549+ Document (content = "Doc 1" , meta = {"priority" : 1 }),
550+ Document (content = "Doc 2" , meta = {"priority" : 2 }),
551+ Document (content = "Doc 3" , meta = {"priority" : 1 }),
552+ Document (content = "Doc 4" , meta = {"priority" : 3 }),
553+ ]
554+ await document_store .write_documents_async (int_docs )
555+ unique_priorities , priority_count = await document_store .get_field_unique_values_async ("meta.priority" , None , 0 , 10 )
556+ assert set (unique_priorities ) == {"1" , "2" , "3" }
557+ assert priority_count == 3
558+
559+ # Test with search term on integer field
560+ unique_priorities_filtered , priority_count = await document_store .get_field_unique_values_async ("meta.priority" , "Doc 1" , 0 , 10 )
561+ assert set (unique_priorities_filtered ) == {"1" }
562+ assert priority_count == 1
563+
502564
0 commit comments