Skip to content

Commit

Permalink
Merge #776
Browse files Browse the repository at this point in the history
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 <nanavalasujeet@gmail.com>
  • Loading branch information
meili-bors[bot] and Nsreddy18 authored Oct 3, 2024
2 parents a096edc + ce937bc commit a4e50d2
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/meilisearch/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.meilisearch.sdk.model.TasksQuery;
import com.meilisearch.sdk.model.TasksResults;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;
Expand Down Expand Up @@ -428,6 +429,18 @@ public void deleteKey(String key) throws MeilisearchException {
this.keysHandler.deleteKey(key);
}

/*
* Method overloading the multi search method to add federation parameter
*/
public MultiSearchResult multiSearch(
MultiSearchRequest search, MultiSearchFederation federation)
throws MeilisearchException {
Map<String, Object> payload = new HashMap<>();
payload.put("queries", search.getQueries());
payload.put("federation", federation);
return this.config.httpClient.post("/multi-search", payload, MultiSearchResult.class);
}

public Results<MultiSearchResult> multiSearch(MultiSearchRequest search)
throws MeilisearchException {
return this.config.httpClient.post(
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/meilisearch/sdk/FederationOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.meilisearch.sdk;

import org.json.JSONObject;

public class FederationOptions {

private Double weight;

public FederationOptions setWeight(Double weight) {
this.weight = weight;
return this;
}

/**
* Method that returns the JSON String of the FederationOptions
*
* @return JSON String of the FederationOptions
*/
@Override
public String toString() {
JSONObject jsonObject = new JSONObject().put("weight", this.weight);
return jsonObject.toString();
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/meilisearch/sdk/IndexSearchRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class IndexSearchRequest {
protected Boolean showRankingScore;
protected Boolean showRankingScoreDetails;
protected Double rankingScoreThreshold;
private FederationOptions federationOptions;

/**
* Constructor for MultiSearchRequest for building search queries with the default values:
Expand Down Expand Up @@ -87,6 +88,11 @@ public String toString() {
.put("sort", this.sort)
.put("page", this.page)
.put("hitsPerPage", this.hitsPerPage)
.put(
"federationOptions",
this.federationOptions != null
? this.federationOptions.toString()
: null)
.putOpt("attributesToCrop", this.attributesToCrop)
.putOpt("attributesToHighlight", this.attributesToHighlight)
.putOpt("filter", this.filter)
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/meilisearch/sdk/MultiSearchFederation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.meilisearch.sdk;

import org.json.JSONObject;

public class MultiSearchFederation {

private Integer limit;
private Integer offset;

public MultiSearchFederation setLimit(Integer limit) {
this.limit = limit;
return this;
}

public MultiSearchFederation setOffset(Integer offset) {
this.offset = offset;
return this;
}

public Integer getLimit() {
return this.limit;
}

public Integer getOffset() {
return this.offset;
}

/**
* Method that returns the JSON String of the MultiSearchFederation
*
* @return JSON String of the MultiSearchFederation
*/
@Override
public String toString() {
JSONObject jsonObject =
new JSONObject().put("limit", this.limit).put("offset", this.offset);
return jsonObject.toString();
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/meilisearch/sdk/MultiSearchRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ public MultiSearchRequest() {
this.queries = new ArrayList();
}

/*
* Method to get Queries as a list
*/
public ArrayList<IndexSearchRequest> getQueries() {
return this.queries;
}

/**
* Method to add new Query
*
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/com/meilisearch/integration/SearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,42 @@ public void testMultiSearch() throws Exception {
}
}

/*
* Test the federation parameter in multi search method
*/
@Test
public void testFederation() throws Exception {
HashSet<String> indexUids = new HashSet();
indexUids.add("MultiSearch1");
indexUids.add("MultiSearch2");
for (String indexUid : indexUids) {
Index index = client.index(indexUid);

TestData<Movie> testData = this.getTestData(MOVIES_INDEX, Movie.class);
TaskInfo task = index.addDocuments(testData.getRaw());

index.waitForTask(task.getTaskUid());
}
MultiSearchRequest search = new MultiSearchRequest();
search.addQuery(new IndexSearchRequest("MultiSearch1").setQuery("batman"));
search.addQuery(
new IndexSearchRequest("MultiSearch2")
.setQuery("batman")
.setFederationOptions(new FederationOptions().setWeight(0.9)));

MultiSearchFederation federation = new MultiSearchFederation();
federation.setLimit(2);
MultiSearchResult results = client.multiSearch(search, federation);

assertThat(results.getEstimatedTotalHits(), is(2));
assertThat(results.getLimit(), is(2));
ArrayList<HashMap<String, Object>> hits = results.getHits();
assertThat(hits, hasSize(2));
for (HashMap<String, Object> record : hits) {
assertThat(record.containsKey("_federation"), is(true));
}
}

/** Test multisearch with ranking score threshold */
@Test
public void testMultiSearchWithRankingScoreThreshold() throws Exception {
Expand Down

0 comments on commit a4e50d2

Please sign in to comment.