Skip to content

Commit 9ff37ec

Browse files
authored
Merge branch 'main' into dbg_multi
2 parents f82f1f8 + a4e50d2 commit 9ff37ec

File tree

8 files changed

+133
-9
lines changed

8 files changed

+133
-9
lines changed

build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ dependencies {
5656
api 'com.squareup.okhttp3:okhttp:[4.10.0,5.0.0)'
5757

5858
// Use JUnit test framework
59-
testImplementation(platform('org.junit:junit-bom:5.11.0'))
60-
testImplementation('org.junit.jupiter:junit-jupiter:5.11.0')
59+
testImplementation(platform('org.junit:junit-bom:5.11.1'))
60+
testImplementation('org.junit.jupiter:junit-jupiter:5.11.1')
6161
// https://mvnrepository.com/artifact/org.mockito/mockito-core
6262
testImplementation 'org.mockito:mockito-core:4.9.0'
6363
testImplementation 'org.hamcrest:hamcrest:3.0'
64-
testImplementation 'com.squareup.okio:okio:3.9.0'
64+
testImplementation 'com.squareup.okio:okio:3.9.1'
6565
testImplementation 'com.squareup.okhttp3:okhttp:4.12.0'
66-
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.17.2'
66+
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.18.0'
6767

6868
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
69-
compileOnly group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.17.2'
69+
compileOnly group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.18.0'
7070

7171
// Lombok
7272
compileOnly 'org.projectlombok:lombok:1.18.34'

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.meilisearch.sdk.model.TasksQuery;
2323
import com.meilisearch.sdk.model.TasksResults;
2424
import java.util.Date;
25+
import java.util.HashMap;
2526
import java.util.Map;
2627
import java.util.TimeZone;
2728
import java.util.UUID;
@@ -428,6 +429,18 @@ public void deleteKey(String key) throws MeilisearchException {
428429
this.keysHandler.deleteKey(key);
429430
}
430431

432+
/*
433+
* Method overloading the multi search method to add federation parameter
434+
*/
435+
public MultiSearchResult multiSearch(
436+
MultiSearchRequest search, MultiSearchFederation federation)
437+
throws MeilisearchException {
438+
Map<String, Object> payload = new HashMap<>();
439+
payload.put("queries", search.getQueries());
440+
payload.put("federation", federation);
441+
return this.config.httpClient.post("/multi-search", payload, MultiSearchResult.class);
442+
}
443+
431444
public Results<MultiSearchResult> multiSearch(MultiSearchRequest search)
432445
throws MeilisearchException {
433446
return this.config.httpClient.post(
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.meilisearch.sdk;
2+
3+
import org.json.JSONObject;
4+
5+
public class FederationOptions {
6+
7+
private Double weight;
8+
9+
public FederationOptions setWeight(Double weight) {
10+
this.weight = weight;
11+
return this;
12+
}
13+
14+
/**
15+
* Method that returns the JSON String of the FederationOptions
16+
*
17+
* @return JSON String of the FederationOptions
18+
*/
19+
@Override
20+
public String toString() {
21+
JSONObject jsonObject = new JSONObject().put("weight", this.weight);
22+
return jsonObject.toString();
23+
}
24+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@ <S, T> T put(String api, S body, Class<T> targetClass) throws MeilisearchExcepti
133133
*/
134134
<S, T> T patch(String api, S body, Class<T> targetClass) throws MeilisearchException {
135135
HttpRequest requestConfig = request.create(HttpMethod.PATCH, api, this.headers, body);
136-
HttpResponse<T> httpRequest = this.client.patch(requestConfig);
137-
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);
136+
HttpResponse<T> httpResponse = this.client.patch(requestConfig);
138137

139138
if (httpResponse.getStatusCode() >= 400) {
140139
throw new MeilisearchApiException(
141140
jsonHandler.decode(httpResponse.getContent(), APIError.class));
142141
}
143-
return httpResponse.getContent();
142+
143+
return response.create(httpResponse, targetClass).getContent();
144144
}
145145

146146
/**

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class IndexSearchRequest {
3535
protected Boolean showRankingScoreDetails;
3636
protected Double rankingScoreThreshold;
3737
private String[] attributesToSearchOn;
38-
38+
private FederationOptions federationOptions;
3939

4040
/**
4141
* Constructor for MultiSearchRequest for building search queries with the default values:
@@ -89,6 +89,11 @@ public String toString() {
8989
.put("sort", this.sort)
9090
.put("page", this.page)
9191
.put("hitsPerPage", this.hitsPerPage)
92+
.put(
93+
"federationOptions",
94+
this.federationOptions != null
95+
? this.federationOptions.toString()
96+
: null)
9297
.putOpt("attributesToCrop", this.attributesToCrop)
9398
.putOpt("attributesToHighlight", this.attributesToHighlight)
9499
.putOpt("filter", this.filter)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.meilisearch.sdk;
2+
3+
import org.json.JSONObject;
4+
5+
public class MultiSearchFederation {
6+
7+
private Integer limit;
8+
private Integer offset;
9+
10+
public MultiSearchFederation setLimit(Integer limit) {
11+
this.limit = limit;
12+
return this;
13+
}
14+
15+
public MultiSearchFederation setOffset(Integer offset) {
16+
this.offset = offset;
17+
return this;
18+
}
19+
20+
public Integer getLimit() {
21+
return this.limit;
22+
}
23+
24+
public Integer getOffset() {
25+
return this.offset;
26+
}
27+
28+
/**
29+
* Method that returns the JSON String of the MultiSearchFederation
30+
*
31+
* @return JSON String of the MultiSearchFederation
32+
*/
33+
@Override
34+
public String toString() {
35+
JSONObject jsonObject =
36+
new JSONObject().put("limit", this.limit).put("offset", this.offset);
37+
return jsonObject.toString();
38+
}
39+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ public MultiSearchRequest() {
1010
this.queries = new ArrayList();
1111
}
1212

13+
/*
14+
* Method to get Queries as a list
15+
*/
16+
public ArrayList<IndexSearchRequest> getQueries() {
17+
return this.queries;
18+
}
19+
1320
/**
1421
* Method to add new Query
1522
*

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,42 @@ public void testMultiSearch() throws Exception {
795795
}
796796
}
797797

798+
/*
799+
* Test the federation parameter in multi search method
800+
*/
801+
@Test
802+
public void testFederation() throws Exception {
803+
HashSet<String> indexUids = new HashSet();
804+
indexUids.add("MultiSearch1");
805+
indexUids.add("MultiSearch2");
806+
for (String indexUid : indexUids) {
807+
Index index = client.index(indexUid);
808+
809+
TestData<Movie> testData = this.getTestData(MOVIES_INDEX, Movie.class);
810+
TaskInfo task = index.addDocuments(testData.getRaw());
811+
812+
index.waitForTask(task.getTaskUid());
813+
}
814+
MultiSearchRequest search = new MultiSearchRequest();
815+
search.addQuery(new IndexSearchRequest("MultiSearch1").setQuery("batman"));
816+
search.addQuery(
817+
new IndexSearchRequest("MultiSearch2")
818+
.setQuery("batman")
819+
.setFederationOptions(new FederationOptions().setWeight(0.9)));
820+
821+
MultiSearchFederation federation = new MultiSearchFederation();
822+
federation.setLimit(2);
823+
MultiSearchResult results = client.multiSearch(search, federation);
824+
825+
assertThat(results.getEstimatedTotalHits(), is(2));
826+
assertThat(results.getLimit(), is(2));
827+
ArrayList<HashMap<String, Object>> hits = results.getHits();
828+
assertThat(hits, hasSize(2));
829+
for (HashMap<String, Object> record : hits) {
830+
assertThat(record.containsKey("_federation"), is(true));
831+
}
832+
}
833+
798834
/** Test multisearch with ranking score threshold */
799835
@Test
800836
public void testMultiSearchWithRankingScoreThreshold() throws Exception {

0 commit comments

Comments
 (0)