Skip to content

Commit

Permalink
Merge branch 'main' into dbg_multi
Browse files Browse the repository at this point in the history
  • Loading branch information
curquiza authored Oct 3, 2024
2 parents f82f1f8 + a4e50d2 commit 9ff37ec
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 9 deletions.
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ dependencies {
api 'com.squareup.okhttp3:okhttp:[4.10.0,5.0.0)'

// Use JUnit test framework
testImplementation(platform('org.junit:junit-bom:5.11.0'))
testImplementation('org.junit.jupiter:junit-jupiter:5.11.0')
testImplementation(platform('org.junit:junit-bom:5.11.1'))
testImplementation('org.junit.jupiter:junit-jupiter:5.11.1')
// https://mvnrepository.com/artifact/org.mockito/mockito-core
testImplementation 'org.mockito:mockito-core:4.9.0'
testImplementation 'org.hamcrest:hamcrest:3.0'
testImplementation 'com.squareup.okio:okio:3.9.0'
testImplementation 'com.squareup.okio:okio:3.9.1'
testImplementation 'com.squareup.okhttp3:okhttp:4.12.0'
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.17.2'
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.18.0'

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

// Lombok
compileOnly 'org.projectlombok:lombok:1.18.34'
Expand Down
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: 3 additions & 3 deletions src/main/java/com/meilisearch/sdk/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ <S, T> T put(String api, S body, Class<T> targetClass) throws MeilisearchExcepti
*/
<S, T> T patch(String api, S body, Class<T> targetClass) throws MeilisearchException {
HttpRequest requestConfig = request.create(HttpMethod.PATCH, api, this.headers, body);
HttpResponse<T> httpRequest = this.client.patch(requestConfig);
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);
HttpResponse<T> httpResponse = this.client.patch(requestConfig);

if (httpResponse.getStatusCode() >= 400) {
throw new MeilisearchApiException(
jsonHandler.decode(httpResponse.getContent(), APIError.class));
}
return httpResponse.getContent();

return response.create(httpResponse, targetClass).getContent();
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/meilisearch/sdk/IndexSearchRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class IndexSearchRequest {
protected Boolean showRankingScoreDetails;
protected Double rankingScoreThreshold;
private String[] attributesToSearchOn;

private FederationOptions federationOptions;

/**
* Constructor for MultiSearchRequest for building search queries with the default values:
Expand Down Expand Up @@ -89,6 +89,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 9ff37ec

Please sign in to comment.