Skip to content

Commit e17aa89

Browse files
meili-bors[bot]Barabasbalazscurquiza
authored
Merge #788
788: Language settings & search parameter support for Java client r=curquiza a=Barabasbalazs # Pull Request ## Related issue Fixes #770 ## What does this PR do? - Implements support for the _localizedAttributes_ setting, _locales_ search parameter and updates the tests accordingly. ## 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: Balazs Barabas <[email protected]> Co-authored-by: Clémentine <[email protected]>
2 parents 032deec + c72ffd6 commit e17aa89

File tree

10 files changed

+339
-4
lines changed

10 files changed

+339
-4
lines changed

.code-samples.meilisearch.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,3 +837,18 @@ distinct_attribute_guide_filterable_1: |-
837837
distinct_attribute_guide_distinct_parameter_1: |-
838838
SearchRequest searchRequest = SearchRequest.builder().q("white shirt").distinct("sku").build();
839839
client.index("products").search(searchRequest);
840+
search_parameter_reference_locales_1: |-
841+
SearchRequest searchRequest = SearchRequest.builder().q("QUERY TEXT IN JAPANESE").locales(new String[]{"jpn"}).build();
842+
client.index("INDEX_NAME").search(searchRequest);
843+
get_localized_attribute_settings_1: |-
844+
client.index("INDEX_NAME").getLocalizedAttributesSettings();
845+
update_localized_attribute_settings_1: |-
846+
LocalizedAttribute attribute = new LocalizedAttribute();
847+
attribute.setAttributePatterns(new String[] {"jpn"});
848+
attribute.setLocales(new String[] {"*_ja"});
849+
850+
client.index("INDEX_NAME").updateLocalizedAttributesSettings(
851+
new LocalizedAttributes[] {attribute}
852+
);
853+
reset_localized_attribute_settings_1: |-
854+
client.index("INDEX_NAME").resetLocalizedAttributesSettings();

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,46 @@ public TaskInfo resetDisplayedAttributesSettings() throws MeilisearchException {
697697
return this.settingsHandler.resetDisplayedAttributesSettings(this.uid);
698698
}
699699

700+
/**
701+
* Gets the localized attributes of the index
702+
*
703+
* @return localized attributes of a given uid as LocalizedAttribute
704+
* @throws MeilisearchException if an error occurs
705+
* @see <a
706+
* href="https://www.meilisearch.com/docs/reference/api/settings#get-localized-attributes">API
707+
* specification</a>
708+
*/
709+
public LocalizedAttribute[] getLocalizedAttributesSettings() throws MeilisearchException {
710+
return this.settingsHandler.getLocalizedAttributes(this.uid);
711+
}
712+
713+
/**
714+
* Updates the localized attributes of the index
715+
*
716+
* @param localizedAttributes An array of localized attributes that contains attributes of an
717+
* index to display
718+
* @return TaskInfo instance
719+
* @throws MeilisearchException if an error occurs
720+
* @see <a
721+
* href="https://www.meilisearch.com/docs/reference/api/settings#update-localized-attribute-settings">API
722+
* specification</a>
723+
*/
724+
public TaskInfo updateLocalizedAttributesSettings(LocalizedAttribute[] localizedAttributes)
725+
throws MeilisearchException {
726+
return this.settingsHandler.updateLocalizedAttributesSettings(
727+
this.uid, localizedAttributes);
728+
}
729+
730+
/**
731+
* Resets the localized attributes of the index
732+
*
733+
* @return TaskInfo instance
734+
* @throws MeilisearchException if an error occurs
735+
*/
736+
public TaskInfo resetLocalizedAttributesSettings() throws MeilisearchException {
737+
return this.settingsHandler.resetLocalizedAttributesSettings(this.uid);
738+
}
739+
700740
/**
701741
* Gets the filterable attributes of the index
702742
*

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class IndexSearchRequest {
3636
protected Double rankingScoreThreshold;
3737
private String[] attributesToSearchOn;
3838
private FederationOptions federationOptions;
39+
protected String[] locales;
3940
protected String distinct;
4041

4142
/**
@@ -104,6 +105,7 @@ public String toString() {
104105
.putOpt("showRankingScoreDetails", this.showRankingScoreDetails)
105106
.putOpt("rankingScoreThreshold", this.rankingScoreThreshold)
106107
.putOpt("attributesToSearchOn", this.attributesToSearchOn)
108+
.putOpt("locales", this.locales)
107109
.putOpt("distinct", this.distinct);
108110

109111
return jsonObject.toString();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class SearchRequest {
4040
protected Boolean showRankingScore;
4141
protected Boolean showRankingScoreDetails;
4242
protected Double rankingScoreThreshold;
43+
protected String[] locales;
4344
protected String distinct;
4445

4546
/**
@@ -102,6 +103,7 @@ public String toString() {
102103
.putOpt("showRankingScore", this.showRankingScore)
103104
.putOpt("showRankingScoreDetails", this.showRankingScoreDetails)
104105
.putOpt("rankingScoreThreshold", this.rankingScoreThreshold)
106+
.putOpt("locales", this.locales)
105107
.putOpt("distinct", this.distinct);
106108

107109
return jsonObject.toString();

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.meilisearch.sdk.exceptions.MeilisearchException;
44
import com.meilisearch.sdk.http.URLBuilder;
55
import com.meilisearch.sdk.model.Faceting;
6+
import com.meilisearch.sdk.model.LocalizedAttribute;
67
import com.meilisearch.sdk.model.Pagination;
78
import com.meilisearch.sdk.model.Settings;
89
import com.meilisearch.sdk.model.TaskInfo;
@@ -266,6 +267,50 @@ TaskInfo resetDisplayedAttributesSettings(String uid) throws MeilisearchExceptio
266267
settingsPath(uid).addSubroute("displayed-attributes").getURL(), TaskInfo.class);
267268
}
268269

270+
/**
271+
* Gets the localized attributes of the index
272+
*
273+
* @param uid Index identifier
274+
* @return an array of localizedattributes that contains attributes of the index to display
275+
* @throws MeilisearchException if an error occurs
276+
*/
277+
LocalizedAttribute[] getLocalizedAttributes(String uid) throws MeilisearchException {
278+
return httpClient.get(
279+
settingsPath(uid).addSubroute("localized-attributes").getURL(),
280+
LocalizedAttribute[].class);
281+
}
282+
283+
/**
284+
* Updates the localized attributes of the index.
285+
*
286+
* @param uid Index identifier
287+
* @param localizedAttributes an array of LocalizedAttributes that contain patterns and locales
288+
* settings
289+
* @return TaskInfo instance
290+
* @throws MeilisearchException if an error occurs
291+
*/
292+
TaskInfo updateLocalizedAttributesSettings(String uid, LocalizedAttribute[] localizedAttributes)
293+
throws MeilisearchException {
294+
return httpClient.put(
295+
settingsPath(uid).addSubroute("localized-attributes").getURL(),
296+
localizedAttributes == null
297+
? httpClient.jsonHandler.encode(localizedAttributes)
298+
: localizedAttributes,
299+
TaskInfo.class);
300+
}
301+
302+
/**
303+
* Resets the localized attributes of the index
304+
*
305+
* @param uid Index identifier
306+
* @return TaskInfo instance
307+
* @throws MeilisearchException if an error occurs
308+
*/
309+
TaskInfo resetLocalizedAttributesSettings(String uid) throws MeilisearchException {
310+
return httpClient.delete(
311+
settingsPath(uid).addSubroute("localized-attributes").getURL(), TaskInfo.class);
312+
}
313+
269314
/**
270315
* Gets the filterableAttributes of the index
271316
*
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.meilisearch.sdk.model;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
8+
/**
9+
* LocalizedAttribute setting data structure
10+
*
11+
* @see <a href="https://www.meilisearch.com/docs/reference/api/settings#localized-attributes">API
12+
* specification</a>
13+
*/
14+
@Getter
15+
@Setter
16+
@AllArgsConstructor
17+
@NoArgsConstructor
18+
public class LocalizedAttribute {
19+
protected String[] attributePatterns;
20+
protected String[] locales;
21+
}

src/main/java/com/meilisearch/sdk/model/Settings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class Settings {
3232
protected String[] separatorTokens;
3333
protected String[] nonSeparatorTokens;
3434
protected HashMap<String, Embedders> embedders;
35+
protected LocalizedAttribute[] localizedAttributes;
3536

3637
public Settings() {}
3738
}

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,4 +1119,80 @@ public void testSimilarDocuments() throws Exception {
11191119
assertThat(hits.get(2).get("title"), is("How to Train Your Dragon: The Hidden World"));
11201120
assertThat(hits.get(3).get("title"), is("Shazam!"));
11211121
}
1122+
1123+
/** Test Search with locales */
1124+
@Test
1125+
public void testSearchWithLocales() throws Exception {
1126+
String indexUid = "SearchLocales";
1127+
Index index = client.index(indexUid);
1128+
GsonJsonHandler jsonGson = new GsonJsonHandler();
1129+
1130+
TestData<Movie> testData = this.getTestData(NESTED_MOVIES, Movie.class);
1131+
TaskInfo task = index.addDocuments(testData.getRaw());
1132+
1133+
index.waitForTask(task.getTaskUid());
1134+
1135+
Settings settings = index.getSettings();
1136+
1137+
LocalizedAttribute localizedAttribute = new LocalizedAttribute();
1138+
localizedAttribute.setAttributePatterns(new String[] {"title", "comment"});
1139+
localizedAttribute.setLocales(new String[] {"fra", "eng"});
1140+
settings.setLocalizedAttributes(new LocalizedAttribute[] {localizedAttribute});
1141+
1142+
index.waitForTask(index.updateSettings(settings).getTaskUid());
1143+
1144+
SearchRequest searchRequest =
1145+
SearchRequest.builder().q("french").locales(new String[] {"fra", "eng"}).build();
1146+
1147+
Results resGson = jsonGson.decode(index.rawSearch(searchRequest), Results.class);
1148+
1149+
assertThat(resGson.hits, is(arrayWithSize(2)));
1150+
}
1151+
1152+
/** Test multisearch with locales */
1153+
@Test
1154+
public void testMultiSearchWithLocales() throws Exception {
1155+
HashSet<String> indexUids = new HashSet<String>();
1156+
indexUids.add("LocaleSearch1");
1157+
indexUids.add("LocaleSearch2");
1158+
1159+
for (String indexUid : indexUids) {
1160+
Index index = client.index(indexUid);
1161+
1162+
TestData<Movie> testData = this.getTestData(NESTED_MOVIES, Movie.class);
1163+
TaskInfo task = index.addDocuments(testData.getRaw());
1164+
1165+
index.waitForTask(task.getTaskUid());
1166+
1167+
Settings settings = index.getSettings();
1168+
1169+
LocalizedAttribute localizedAttribute = new LocalizedAttribute();
1170+
localizedAttribute.setAttributePatterns(new String[] {"title", "comment"});
1171+
localizedAttribute.setLocales(new String[] {"fra", "eng"});
1172+
settings.setLocalizedAttributes(new LocalizedAttribute[] {localizedAttribute});
1173+
1174+
index.waitForTask(index.updateSettings(settings).getTaskUid());
1175+
}
1176+
1177+
MultiSearchRequest search = new MultiSearchRequest();
1178+
1179+
search.addQuery(
1180+
new IndexSearchRequest("LocaleSearch1")
1181+
.setQuery("")
1182+
.setLocales(new String[] {"eng"}));
1183+
search.addQuery(
1184+
new IndexSearchRequest("LocaleSearch2")
1185+
.setQuery("french")
1186+
.setLocales(new String[] {"fra"}));
1187+
1188+
MultiSearchResult[] results = client.multiSearch(search).getResults();
1189+
1190+
assertThat(results.length, is(2));
1191+
1192+
for (MultiSearchResult searchResult : results) {
1193+
assertThat(indexUids.contains(searchResult.getIndexUid()), is(true));
1194+
}
1195+
assertThat(results[0].getHits().size(), is(7));
1196+
assertThat(results[1].getHits().size(), is(2));
1197+
}
11221198
}

0 commit comments

Comments
 (0)