Skip to content

Commit a4e50d2

Browse files
Merge #776
776: Added Federation Parameter to multisearch method #768 r=curquiza a=Nsreddy18 # Pull Request ## Related issue Fixes #768 ## What does this PR do? - Adds a new parameter federation to the multiSearch method. - Introduces a new search parameter federationOptions to the search query. - Adds a getter method to the MultiSearchRequest class to retrieve the query list for creating the payload. - Adds a test to validate the new functionality. ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: nsreddy18 <[email protected]>
2 parents a096edc + ce937bc commit a4e50d2

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

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/IndexSearchRequest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class IndexSearchRequest {
3434
protected Boolean showRankingScore;
3535
protected Boolean showRankingScoreDetails;
3636
protected Double rankingScoreThreshold;
37+
private FederationOptions federationOptions;
3738

3839
/**
3940
* Constructor for MultiSearchRequest for building search queries with the default values:
@@ -87,6 +88,11 @@ public String toString() {
8788
.put("sort", this.sort)
8889
.put("page", this.page)
8990
.put("hitsPerPage", this.hitsPerPage)
91+
.put(
92+
"federationOptions",
93+
this.federationOptions != null
94+
? this.federationOptions.toString()
95+
: null)
9096
.putOpt("attributesToCrop", this.attributesToCrop)
9197
.putOpt("attributesToHighlight", this.attributesToHighlight)
9298
.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)