Skip to content

Commit cefaf25

Browse files
committed
Add retrieveVectors
1 parent 40fa243 commit cefaf25

4 files changed

Lines changed: 62 additions & 6 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ public class IndexSearchRequest {
3838
private FederationOptions federationOptions;
3939
protected String[] locales;
4040
protected String distinct;
41+
protected Boolean retrieveVectors;
4142

4243
/**
4344
* Constructor for MultiSearchRequest for building search queries with the default values:
4445
* offset: 0, limit: 20, attributesToRetrieve: ["*"], attributesToCrop: null, cropLength: 200,
4546
* attributesToHighlight: null, filter: null, showMatchesPosition: false, facets: null, sort:
4647
* null, showRankingScore: false, showRankingScoreDetails: false, rankingScoreThreshold: null
47-
* distinct: null
48+
* distinct: null, retrieveVectors: false
4849
*
4950
* @param indexUid uid of the requested index String
5051
*/
@@ -106,7 +107,8 @@ public String toString() {
106107
.putOpt("rankingScoreThreshold", this.rankingScoreThreshold)
107108
.putOpt("attributesToSearchOn", this.attributesToSearchOn)
108109
.putOpt("locales", this.locales)
109-
.putOpt("distinct", this.distinct);
110+
.putOpt("distinct", this.distinct)
111+
.putOpt("retrieveVectors", this.retrieveVectors);
110112

111113
return jsonObject.toString();
112114
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ public class SearchRequest {
4545
protected String distinct;
4646
protected Hybrid hybrid;
4747
protected Double[] vector;
48+
protected Boolean retrieveVectors;
4849
/**
4950
* Constructor for SearchRequest for building search queries with the default values: offset: 0,
5051
* limit: 20, attributesToRetrieve: ["*"], attributesToCrop: null, cropLength: 200,
5152
* attributesToHighlight: null, filter: null, showMatchesPosition: false, facets: null, sort:
52-
* null, showRankingScore: false, showRankingScoreDetails: false, rankingScoreThreshold: null
53+
* null, showRankingScore: false, showRankingScoreDetails: false, rankingScoreThreshold: null,
54+
* retrieveVectors: false
5355
*
5456
* @param q Query String
5557
*/
@@ -107,11 +109,11 @@ public String toString() {
107109
.putOpt("rankingScoreThreshold", this.rankingScoreThreshold)
108110
.putOpt("locales", this.locales)
109111
.putOpt("distinct", this.distinct)
110-
.putOpt("vector", this.vector);
112+
.putOpt("vector", this.vector)
113+
.putOpt("retrieveVectors", this.retrieveVectors);
111114

112-
// Add hybrid parameter if it exists
113115
if (this.hybrid != null) {
114-
jsonObject.put("hybrid", this.hybrid.toJSONObject());
116+
jsonObject.put("hybrid", new JSONObject(this.hybrid.toString()));
115117
}
116118

117119
return jsonObject.toString();

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,48 @@ public void testVectorSearch() throws Exception {
11491149
assertThat(searchResult.getHits().get(0).get("title"), is("Escape Room"));
11501150
}
11511151

1152+
/** Test vector search with retrieveVectors option */
1153+
@Test
1154+
public void testVectorSearchWithRetrieveVectors() throws Exception {
1155+
String indexUid = "testVectorSearchWithRetrieveVectors";
1156+
Index index = client.index(indexUid);
1157+
HashMap<String, Embedder> embedders = new HashMap<>();
1158+
embedders.put(
1159+
"manual", new Embedder().setSource(EmbedderSource.USER_PROVIDED).setDimensions(3));
1160+
1161+
Settings settings = new Settings();
1162+
settings.setEmbedders(embedders);
1163+
1164+
index.updateSettings(settings);
1165+
1166+
TestData<Movie> testData = this.getTestData(VECTOR_MOVIES, Movie.class);
1167+
TaskInfo task = index.addDocuments(testData.getRaw());
1168+
1169+
index.waitForTask(task.getTaskUid());
1170+
1171+
SearchRequest searchRequest =
1172+
SearchRequest.builder()
1173+
.vector(new Double[] {0.1, 0.6, 0.8})
1174+
.hybrid(Hybrid.builder().semanticRatio(0.5).embedder("manual").build())
1175+
.retrieveVectors(true)
1176+
.build();
1177+
1178+
SearchResult searchResult = (SearchResult) index.search(searchRequest);
1179+
1180+
assertThat(searchResult.getHits(), hasSize(5));
1181+
// The most similar document should be "Escape Room" since its vector [0.1, 0.6, 0.8]
1182+
assertThat(searchResult.getHits().get(0).get("id"), is("522681"));
1183+
assertThat(searchResult.getHits().get(0).get("title"), is("Escape Room"));
1184+
1185+
// Verify that vectors are returned in the response
1186+
Map<String, Object> escapeRoomHit = searchResult.getHits().get(0);
1187+
assertThat(escapeRoomHit.containsKey("_vectors"), is(true));
1188+
1189+
@SuppressWarnings("unchecked")
1190+
Map<String, Object> vectors = (Map<String, Object>) escapeRoomHit.get("_vectors");
1191+
assertThat(vectors.containsKey("manual"), is(true));
1192+
}
1193+
11521194
/** Test Search with locales */
11531195
@Test
11541196
public void testSearchWithLocales() throws Exception {

src/test/java/com/meilisearch/sdk/SearchRequestTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.meilisearch.sdk.model.Hybrid;
99
import com.meilisearch.sdk.model.MatchingStrategy;
1010
import org.junit.jupiter.api.Test;
11+
import org.json.JSONObject;
1112

1213
class SearchRequestTest {
1314

@@ -425,4 +426,13 @@ void toStringWithHybridOnlyEmbedder() {
425426
String expected = "{\"q\":\"This is a Test\",\"hybrid\":{\"embedder\":\"default\"}}";
426427
assertThat(classToTest.toString(), is(equalTo(expected)));
427428
}
429+
430+
@Test
431+
void toStringWithRetrieveVectors() {
432+
SearchRequest searchRequest = new SearchRequest("test")
433+
.setRetrieveVectors(true);
434+
String result = searchRequest.toString();
435+
JSONObject json = new JSONObject(result);
436+
assertThat(json.getBoolean("retrieveVectors"), is(true));
437+
}
428438
}

0 commit comments

Comments
 (0)