Skip to content

Commit 4a80c9f

Browse files
committed
Add multisearch example that uses typed parameter.
1 parent dc6ed18 commit 4a80c9f

File tree

1 file changed

+24
-45
lines changed

1 file changed

+24
-45
lines changed
Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.typesense.api;
22

33
import junit.framework.TestCase;
4-
import org.typesense.model.CollectionSchema;
5-
import org.typesense.model.Field;
4+
import org.junit.Assert;
5+
import org.typesense.model.*;
66

77
import java.util.ArrayList;
88
import java.util.HashMap;
@@ -17,26 +17,23 @@ public class MultiSearchTest extends TestCase {
1717
public void setUp() throws Exception {
1818
super.setUp();
1919
helper = new Helper();
20-
helper.teardown();
2120
client = helper.getClient();
22-
helper.createTestCollection();
23-
helper.createTestDocument();
2421

25-
ArrayList<Field> fields = new ArrayList<>();
26-
fields.add(new Field().name(".*").type(FieldTypes.AUTO).optional(true));
22+
helper.teardown();
23+
24+
// create a collection with 2 fields: title and vec to store embeddings
25+
List<Field> fields = new ArrayList<>();
26+
fields.add(new Field().name("title").type(FieldTypes.STRING));
27+
fields.add(new Field().name("vec").type(FieldTypes.FLOAT_ARRAY).numDim(4));
2728
CollectionSchema collectionSchema = new CollectionSchema();
28-
collectionSchema.name("brands").fields(fields);
29+
collectionSchema.name("embeddings").fields(fields);
2930
client.collections().create(collectionSchema);
3031

31-
String[] authors = {"shakspeare","william"};
32-
Map<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);
32+
float[] vecVals = {0.12f, 0.45f, 0.87f, 0.18f};
33+
Map<String, Object> doc = new HashMap<>();
34+
doc.put("title", "Romeo and Juliet");
35+
doc.put("vec",vecVals);
36+
client.collections("embeddings").documents().create(doc);
4037
}
4138

4239
public void tearDown() throws Exception {
@@ -45,33 +42,15 @@ public void tearDown() throws Exception {
4542
}
4643

4744
public void testSearch() throws Exception {
48-
Map<String,String > val1 = new HashMap<>();
49-
Map<String,String > val2 = new HashMap<>();
50-
51-
val1.put("collection","books");
52-
val1.put("q","romeo");
53-
54-
val2.put("collection","brands");
55-
val2.put("q","juliet");
56-
57-
List<Map<String, String>> list = new ArrayList<>();
58-
list.add(val2);
59-
list.add(val1);
60-
61-
Map<String, List<Map<String ,String>>> map = new HashMap<>();
62-
map.put("searches",list);
63-
64-
/*ObjectMapper objectMapper = new ObjectMapper();
65-
try {
66-
String json = objectMapper.writeValueAsString(maplist);
67-
System.out.println(json);
68-
} catch (JsonProcessingException e) {
69-
e.printStackTrace();
70-
}*/
71-
Map<String,String> common_params = new HashMap<>();
72-
common_params.put("query_by","title");
73-
74-
System.out.println(this.client.multiSearch.perform(map, common_params));
45+
MultiSearchCollectionParameters search1 = new MultiSearchCollectionParameters();
46+
search1.setCollection("embeddings");
47+
search1.setQ("*");
48+
search1.setVectorQuery("vec:([0.96826,0.94,0.39557,0.306488], k:10)");
49+
50+
MultiSearchSearchesParameter multiSearchParameters = new MultiSearchSearchesParameter().addSearchesItem(search1);
51+
MultiSearchResponse response = this.client.multiSearch.perform(multiSearchParameters, null);
52+
Assert.assertEquals(1, response.getResults().size());
53+
Assert.assertEquals(1, response.getResults().get(0).getHits().size());
54+
Assert.assertEquals("0", response.getResults().get(0).getHits().get(0).getDocument().get("id"));
7555
}
76-
7756
}

0 commit comments

Comments
 (0)