Skip to content

Commit

Permalink
added locales to indexsearchrequest
Browse files Browse the repository at this point in the history
  • Loading branch information
Barabasbalazs committed Nov 2, 2024
1 parent 26154ef commit ee89f33
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/com/meilisearch/sdk/IndexSearchRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class IndexSearchRequest {
protected Double rankingScoreThreshold;
private String[] attributesToSearchOn;
private FederationOptions federationOptions;
protected String[] locales;

/**
* Constructor for MultiSearchRequest for building search queries with the default values:
Expand Down Expand Up @@ -101,7 +102,8 @@ public String toString() {
.putOpt("showRankingScore", this.showRankingScore)
.putOpt("showRankingScoreDetails", this.showRankingScoreDetails)
.putOpt("rankingScoreThreshold", this.rankingScoreThreshold)
.putOpt("attributesToSearchOn", this.attributesToSearchOn);
.putOpt("attributesToSearchOn", this.attributesToSearchOn)
.putOpt("locales", this.locales);

return jsonObject.toString();
}
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/com/meilisearch/integration/SearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -933,4 +933,52 @@ public void testSearchWithLocales() throws Exception {

assertThat(resGson.hits, is(arrayWithSize(2)));
}

/** Test multisearch with locales */
@Test
public void testMultiSearchWithLocales() throws Exception {
HashSet<String> indexUids = new HashSet<String>();
indexUids.add("LocaleSearch1");
indexUids.add("LocaleSearch2");

for (String indexUid : indexUids) {
Index index = client.index(indexUid);

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

index.waitForTask(task.getTaskUid());

Settings settings = index.getSettings();

LocalizedAttribute localizedAttribute = new LocalizedAttribute();
localizedAttribute.setAttributePatterns(new String[] {"title", "comment"});
localizedAttribute.setLocales(new String[] {"fra", "eng"});
settings.setLocalizedAttributes(new LocalizedAttribute[] {localizedAttribute});

index.waitForTask(index.updateSettings(settings).getTaskUid());
}

MultiSearchRequest search = new MultiSearchRequest();

search.addQuery(
new IndexSearchRequest("LocaleSearch1")
.setQuery("")
.setLocales(
new String[] {"eng"}));
search.addQuery(
new IndexSearchRequest("LocaleSearch2")
.setQuery("french")
.setLocales(new String[] {"fra"}));

MultiSearchResult[] results = client.multiSearch(search).getResults();

assertThat(results.length, is(2));

for (MultiSearchResult searchResult : results) {
assertThat(indexUids.contains(searchResult.getIndexUid()), is(true));
}
assertThat(results[0].getHits().size(), is(7));
assertThat(results[1].getHits().size(), is(2));
}
}

0 comments on commit ee89f33

Please sign in to comment.