1+ package org .typesense .api ;
2+
3+ import junit .framework .TestCase ;
4+ import org .typesense .model .CollectionSchema ;
5+ import org .typesense .model .Field ;
6+ import org .typesense .resources .Node ;
7+
8+ import java .time .Duration ;
9+ import java .util .ArrayList ;
10+ import java .util .HashMap ;
11+ import java .util .List ;
12+
13+ public class MultiSearchTest extends TestCase {
14+
15+ private Client client ;
16+ private Helper helper ;
17+
18+ public void setUp () throws Exception {
19+ super .setUp ();
20+ helper = new Helper ();
21+ client = helper .getClient ();
22+ helper .createTestCollection ();
23+ helper .createTestDocument ();
24+
25+ ArrayList <Field > fields = new ArrayList <>();
26+ fields .add (new Field ().name (".*" ).type (Field .TypeEnum .AUTO ).optional (true ));
27+ CollectionSchema collectionSchema = new CollectionSchema ();
28+ collectionSchema .name ("brands" ).fields (fields );
29+ client .collections ().create (collectionSchema );
30+
31+ String [] authors = {"shakspeare" ,"william" };
32+ HashMap <String , Object > hmap = new HashMap <>();
33+ hmap .put ("title" ,"Romeo and juliet" );
34+ hmap .put ("authors" ,authors );
35+ hmap .put ("publication_year" ,1666 );
36+ hmap .put ("ratings_count" ,124 );
37+ hmap .put ("average_rating" ,3.2 );
38+ hmap .put ("id" ,"1" );
39+ client .collections ("brands" ).documents ().create (hmap );
40+ }
41+
42+ public void testSearch (){
43+ HashMap <String ,String > val1 = new HashMap <>();
44+ HashMap <String ,String > val2 = new HashMap <>();
45+
46+ val1 .put ("collection" ,"books" );
47+ val1 .put ("q" ,"romeo" );
48+
49+ val2 .put ("collection" ,"brands" );
50+ val2 .put ("q" ,"juliet" );
51+
52+ List <HashMap <String , String >> list = new ArrayList <>();
53+ list .add (val2 );
54+ list .add (val1 );
55+
56+ HashMap <String , List <HashMap <String ,String >>> map = new HashMap <>();
57+ map .put ("searches" ,list );
58+
59+ /*ObjectMapper objectMapper = new ObjectMapper();
60+ try {
61+ String json = objectMapper.writeValueAsString(maplist);
62+ System.out.println(json);
63+ } catch (JsonProcessingException e) {
64+ e.printStackTrace();
65+ }*/
66+ HashMap <String ,String > common_params = new HashMap <>();
67+ common_params .put ("query_by" ,"title" );
68+
69+ System .out .println (this .client .multiSearch .perform (map , common_params ));
70+ }
71+
72+ }
0 commit comments