77import org .typesense .model .Field ;
88import org .typesense .model .MultiSearchCollectionParameters ;
99import org .typesense .model .MultiSearchResult ;
10+ import org .typesense .model .SearchResult ;
1011import org .typesense .model .MultiSearchSearchesParameter ;
1112
1213import java .util .ArrayList ;
1314import java .util .HashMap ;
15+ import java .util .HashSet ;
1416import java .util .List ;
1517import java .util .Map ;
18+ import java .util .Set ;
1619
1720import static org .junit .jupiter .api .Assertions .assertEquals ;
21+ import static org .junit .jupiter .api .Assertions .assertTrue ;
1822
1923class MultiSearchTest {
2024
@@ -35,12 +39,23 @@ void setUp() throws Exception {
3539 CollectionSchema collectionSchema = new CollectionSchema ();
3640 collectionSchema .name ("embeddings" ).fields (fields );
3741 client .collections ().create (collectionSchema );
42+ // create another collection for union search
43+ collectionSchema .name ("embeddings-2" ).fields (fields );
44+ client .collections ().create (collectionSchema );
45+
46+ float [] vecVals = { 0.12f , 0.45f , 0.87f , 0.18f };
47+ Map <String , Object > doc1 = new HashMap <>();
48+ doc1 .put ("title" , "Romeo and Juliet" );
49+ doc1 .put ("vec" , vecVals );
50+ doc1 .put ("source" , "embeddings_1" );
51+ client .collections ("embeddings" ).documents ().create (doc1 );
3852
39- float [] vecVals = {0.12f , 0.45f , 0.87f , 0.18f };
40- Map <String , Object > doc = new HashMap <>();
41- doc .put ("title" , "Romeo and Juliet" );
42- doc .put ("vec" , vecVals );
43- client .collections ("embeddings" ).documents ().create (doc );
53+ // Document for the second collection
54+ Map <String , Object > doc2 = new HashMap <>();
55+ doc2 .put ("title" , "Romeo and Juliet from collection 2" );
56+ doc2 .put ("vec" , new float [] { 0.12f , 0.45f , 0.87f , 0.18f });
57+ doc2 .put ("source" , "embeddings_2" );
58+ client .collections ("embeddings-2" ).documents ().create (doc2 );
4459 }
4560
4661 @ AfterEach
@@ -55,10 +70,36 @@ void testSearch() throws Exception {
5570 search1 .setQ ("*" );
5671 search1 .setVectorQuery ("vec:([0.96826,0.94,0.39557,0.306488], k:10)" );
5772
58- MultiSearchSearchesParameter multiSearchParameters = new MultiSearchSearchesParameter ().addSearchesItem (search1 );
73+ MultiSearchSearchesParameter multiSearchParameters = new MultiSearchSearchesParameter ()
74+ .addSearchesItem (search1 );
5975 MultiSearchResult response = this .client .multiSearch .perform (multiSearchParameters , null );
6076 assertEquals (1 , response .getResults ().size ());
6177 assertEquals (1 , response .getResults ().get (0 ).getHits ().size ());
6278 assertEquals ("0" , response .getResults ().get (0 ).getHits ().get (0 ).getDocument ().get ("id" ));
6379 }
80+
81+ @ Test
82+ void testUnionSearch () throws Exception {
83+ MultiSearchCollectionParameters search1 = new MultiSearchCollectionParameters ();
84+ search1 .setCollection ("embeddings" );
85+ search1 .setQ ("*" );
86+
87+ MultiSearchCollectionParameters search2 = new MultiSearchCollectionParameters ();
88+ search2 .setCollection ("embeddings-2" );
89+ search2 .setQ ("*" );
90+
91+ MultiSearchSearchesParameter multiSearchParameters = new MultiSearchSearchesParameter ()
92+ .addSearchesItem (search1 ).addSearchesItem (search2 );
93+ SearchResult response = this .client .multiSearch .performUnion (multiSearchParameters , null );
94+
95+ assertEquals (2 , response .getHits ().size ());
96+ assertEquals (2 , response .getUnionRequestParams ().size ());
97+
98+ Set <String > sources = new HashSet <>();
99+ sources .add ((String ) response .getHits ().get (0 ).getDocument ().get ("source" ));
100+ sources .add ((String ) response .getHits ().get (1 ).getDocument ().get ("source" ));
101+
102+ assertTrue (sources .contains ("embeddings_1" ), "Results should contain a document from embeddings_1" );
103+ assertTrue (sources .contains ("embeddings_2" ), "Results should contain a document from embeddings_2" );
104+ }
64105}
0 commit comments