Skip to content

Commit a3b8457

Browse files
committed
Add vector search test
1 parent ebaa11d commit a3b8457

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/main/java/com/meilisearch/sdk/SearchRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class SearchRequest {
4444
protected String[] locales;
4545
protected String distinct;
4646
protected Hybrid hybrid;
47-
47+
protected Double[] vector;
4848
/**
4949
* Constructor for SearchRequest for building search queries with the default values: offset: 0,
5050
* limit: 20, attributesToRetrieve: ["*"], attributesToCrop: null, cropLength: 200,
@@ -106,7 +106,8 @@ public String toString() {
106106
.putOpt("showRankingScoreDetails", this.showRankingScoreDetails)
107107
.putOpt("rankingScoreThreshold", this.rankingScoreThreshold)
108108
.putOpt("locales", this.locales)
109-
.putOpt("distinct", this.distinct);
109+
.putOpt("distinct", this.distinct)
110+
.putOpt("vector", this.vector);
110111

111112
// Add hybrid parameter if it exists
112113
if (this.hybrid != null) {

src/test/java/com/meilisearch/integration/SearchTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,36 @@ public void testSimilarDocuments() throws Exception {
11161116
assertThat(hits.get(3).get("title"), is("Shazam!"));
11171117
}
11181118

1119+
/** Test vector search */
1120+
@Test
1121+
public void testVectorSearch() throws Exception {
1122+
String indexUid = "testVectorSearch";
1123+
Index index = client.index(indexUid);
1124+
HashMap<String, Embedder> embedders = new HashMap<>();
1125+
embedders.put(
1126+
"manual", new Embedder().setSource(EmbedderSource.USER_PROVIDED).setDimensions(3));
1127+
1128+
Settings settings = new Settings();
1129+
settings.setEmbedders(embedders);
1130+
1131+
index.updateSettings(settings);
1132+
1133+
TestData<Movie> testData = this.getTestData(VECTOR_MOVIES, Movie.class);
1134+
TaskInfo task = index.addDocuments(testData.getRaw());
1135+
1136+
index.waitForTask(task.getTaskUid());
1137+
1138+
SearchRequest searchRequest =
1139+
SearchRequest.builder().vector(new Double[] {0.1, 0.6, 0.8}).build();
1140+
1141+
SearchResult searchResult = (SearchResult) index.search(searchRequest);
1142+
1143+
assertThat(searchResult.getHits(), hasSize(5));
1144+
// The most similar document should be "Escape Room" since its vector [0.1, 0.6, 0.8]
1145+
assertThat(searchResult.getHits().get(0).get("id"), is("522681"));
1146+
assertThat(searchResult.getHits().get(0).get("title"), is("Escape Room"));
1147+
}
1148+
11191149
/** Test Search with locales */
11201150
@Test
11211151
public void testSearchWithLocales() throws Exception {

0 commit comments

Comments
 (0)