Skip to content

Commit 9073c17

Browse files
bors[bot]alallema
andauthored
Merge #371
371: Add typo tolerance settings r=alallema a=alallema ⚠️ This PR is a `WIP` it will be merged after the v0.27.0 This PR introduces the new setting: [typoTolerance](meilisearch/specifications#117) new methods: `index.getTypoTolerance()` `index.updateTypoTolerance(params)` `index.resetTypoTolerance()` Co-authored-by: alallema <[email protected]> Co-authored-by: Amélie <[email protected]>
2 parents 04244fa + d7bbdb5 commit 9073c17

File tree

9 files changed

+395
-63
lines changed

9 files changed

+395
-63
lines changed

.code-samples.meilisearch.yaml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,24 @@ update_displayed_attributes_1: |-
192192
});
193193
reset_displayed_attributes_1: |-
194194
client.index("movies").resetDisplayedAttributesSettings();
195+
get_typo_tolerance_1:
196+
client.index("books").getTypoToleranceSettings();
197+
update_typo_tolerance_1: |-
198+
TypoTolerance typoTolerance = new TypoTolerance();
199+
HashMap<String, Integer> minWordSizeTypos =
200+
new HashMap<String, Integer>() {
201+
{
202+
put("oneTypo", 4);
203+
put("twoTypos", 10);
204+
}
205+
};
206+
207+
typoTolerance.setMinWordSizeForTypos(minWordSizeTypos);
208+
typoTolerance.setDisableOnAttributes(new String[] {"title"});
209+
210+
client.index("books").updateTypoToleranceSettings(typoTolerance);
211+
reset_typo_tolerance_1: |-
212+
client.index("books").resetTypoToleranceSettings();
195213
get_index_stats_1: |-
196214
client.index("movies").getStats();
197215
get_indexes_stats_1: |-
@@ -334,6 +352,42 @@ settings_guide_sortable_1: |-
334352
"author",
335353
});
336354
client.index("books").updateSettings(settings);
355+
settings_guide_typo_tolerance_1: |-
356+
TypoTolerance typoTolerance = new TypoTolerance();
357+
HashMap<String, Integer> minWordSizeTypos =
358+
new HashMap<String, Integer>() {
359+
{
360+
put("twoTypos", 12);
361+
}
362+
};
363+
364+
typoTolerance.setMinWordSizeForTypos(minWordSizeTypos);
365+
typoTolerance.setDisableOnAttributes(new String[] {"title"});
366+
367+
client.index("movies").updateTypoToleranceSettings(typoTolerance);
368+
typo_tolerance_guide_1: |-
369+
TypoTolerance typoTolerance = new TypoTolerance();
370+
typoTolerance.setEnabled(false);
371+
client.index("movies").updateTypoToleranceSettings(typoTolerance);
372+
typo_tolerance_guide_2: |-
373+
TypoTolerance typoTolerance = new TypoTolerance();
374+
typoTolerance.setDisableOnAttributes(new String[] {"title"});
375+
client.index("movies").updateTypoToleranceSettings(typoTolerance);
376+
typo_tolerance_guide_3: |-
377+
TypoTolerance typoTolerance = new TypoTolerance();
378+
typoTolerance.setDisableOnWords(new String[] {"shrek"});
379+
client.index("movies").updateTypoToleranceSettings(typoTolerance);
380+
typo_tolerance_guide_4: |-
381+
TypoTolerance typoTolerance = new TypoTolerance();
382+
HashMap<String, Integer> minWordSizeTypos =
383+
new HashMap<String, Integer>() {
384+
{
385+
put("oneTypo", 4);
386+
put("twoTypos", 10);
387+
}
388+
};
389+
typoTolerance.setMinWordSizeForTypos(minWordSizeTypos);
390+
client.index("movies").updateTypoToleranceSettings(typoTolerance);
337391
documents_guide_add_movie_1: |-
338392
client.index("movies").addDocuments("[{"
339393
+ "\"movie_id\": 123sq178,"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ public Details() {}
2222
protected String[] stopWords;
2323
protected Map<String, String[]> synonyms;
2424
protected String distinctAttribute;
25+
protected TypoTolerance typoTolerance;
2526
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,40 @@ public Task resetDistinctAttributeSettings() throws Exception {
567567
return this.settingsHandler.resetDistinctAttributeSettings(this.uid);
568568
}
569569

570+
/**
571+
* Get the typo tolerance field of an index.
572+
* https://docs.meilisearch.com/reference/api/typo_tolerance.html#get-typo-tolerance
573+
*
574+
* @return TypoTolerance instance from Index
575+
* @throws Exception if an error occurs
576+
*/
577+
public TypoTolerance getTypoToleranceSettings() throws Exception {
578+
return this.settingsHandler.getTypoToleranceSettings(this.uid);
579+
}
580+
581+
/**
582+
* Update the typo tolerance field of an index.
583+
* https://docs.meilisearch.com/reference/api/typo_tolerance.html#update-typo-tolerance
584+
*
585+
* @param typoTolerance A TypoTolerance instance
586+
* @return Task instance
587+
* @throws Exception if an error occurs
588+
*/
589+
public Task updateTypoToleranceSettings(TypoTolerance typoTolerance) throws Exception {
590+
return this.settingsHandler.updateTypoToleranceSettings(this.uid, typoTolerance);
591+
}
592+
593+
/**
594+
* Reset the typo tolerance field of an index to its default value.
595+
* https://docs.meilisearch.com/reference/api/typo_tolerance.html#reset-typo-tolerance
596+
*
597+
* @return Task instance
598+
* @throws Exception if an error occurs
599+
*/
600+
public Task resetTypoToleranceSettings() throws Exception {
601+
return this.settingsHandler.resetTypoToleranceSettings(this.uid);
602+
}
603+
570604
/**
571605
* Retrieves an index tasks by its ID
572606
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class Result<T> {
88
@Getter protected T[] results = null;
99

10-
private static Gson gsonUpdate = new Gson();
10+
private static Gson gsonResult = new Gson();
1111

1212
/**
1313
* Method to return the JSON String of the Result
@@ -16,6 +16,6 @@ public class Result<T> {
1616
*/
1717
@Override
1818
public String toString() {
19-
return gsonUpdate.toJson(this);
19+
return gsonResult.toJson(this);
2020
}
2121
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class Settings {
2121
private String[] searchableAttributes;
2222
private String[] displayedAttributes;
2323
private String[] sortableAttributes;
24+
private TypoTolerance typoTolerance;
2425

2526
/** Empty SettingsRequest constructor */
2627
public Settings() {}
@@ -56,6 +57,9 @@ String getUpdateQuery() {
5657
if (this.getSortableAttributes() != null) {
5758
jsonObject.put("sortableAttributes", this.getSortableAttributes());
5859
}
60+
if (this.getTypoTolerance() != null) {
61+
jsonObject.put("typoTolerance", this.getTypoTolerance().toJson());
62+
}
5963
return jsonObject.toString();
6064
}
6165
}

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public Task resetRankingRulesSettings(String uid) throws Exception {
117117
* @throws Exception if an error occurs
118118
*/
119119
public Map<String, String[]> getSynonymsSettings(String uid) throws Exception {
120-
return this.gson.<Map<String, String[]>>fromJson(
120+
return this.gson.fromJson(
121121
meilisearchHttpRequest.get("/indexes/" + uid + "/settings/synonyms"), Map.class);
122122
}
123123

@@ -390,4 +390,50 @@ public Task resetDistinctAttributeSettings(String uid) throws Exception {
390390
meilisearchHttpRequest.delete("/indexes/" + uid + "/settings/distinct-attribute"),
391391
Task.class);
392392
}
393+
394+
/**
395+
* Gets the typo tolerance settings of a given index Refer
396+
* https://docs.meilisearch.com/reference/api/typo_tolerance.html#get-typo-tolerance
397+
*
398+
* @param uid Index identifier
399+
* @return a TypoTolerance instance that contains all typo tolerance settings
400+
* @throws Exception if an error occurs
401+
*/
402+
public TypoTolerance getTypoToleranceSettings(String uid) throws Exception {
403+
return this.gson.fromJson(
404+
meilisearchHttpRequest.get("/indexes/" + uid + "/settings/typo-tolerance"),
405+
TypoTolerance.class);
406+
}
407+
408+
/**
409+
* Updates the typo tolerance settings of a given index Refer
410+
* https://docs.meilisearch.com/reference/api/typo_tolerance.html#update-typo-tolerance
411+
*
412+
* @param uid Index identifier
413+
* @param typoTolerance a TypoTolerance instance that contains the new typo tolerance settings
414+
* @return Task instance
415+
* @throws Exception if an error occurs
416+
*/
417+
public Task updateTypoToleranceSettings(String uid, TypoTolerance typoTolerance)
418+
throws Exception {
419+
String typoToleranceAsJson = gson.toJson(typoTolerance);
420+
return this.gson.fromJson(
421+
meilisearchHttpRequest.post(
422+
"/indexes/" + uid + "/settings/typo-tolerance", typoToleranceAsJson),
423+
Task.class);
424+
}
425+
426+
/**
427+
* Resets the typo tolerance settings of a given index Refer
428+
* https://docs.meilisearch.com/reference/api/typo_tolerance.html#reset-typo-tolerance
429+
*
430+
* @param uid Index identifier
431+
* @return Task instance
432+
* @throws Exception if an error occurs
433+
*/
434+
public Task resetTypoToleranceSettings(String uid) throws Exception {
435+
return this.gson.fromJson(
436+
meilisearchHttpRequest.delete("/indexes/" + uid + "/settings/typo-tolerance"),
437+
Task.class);
438+
}
393439
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.meilisearch.sdk;
2+
3+
import java.util.HashMap;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import org.json.JSONObject;
7+
8+
public class TypoTolerance {
9+
@Getter @Setter private boolean enabled = true;
10+
@Getter @Setter private HashMap<String, Integer> minWordSizeForTypos;
11+
@Getter @Setter private String[] disableOnWords;
12+
@Getter @Setter private String[] disableOnAttributes;
13+
14+
/**
15+
* Method to return the JSONObject of the TypoTolerance Setting
16+
*
17+
* @return JSONObject of the TypoTolerance Setting object
18+
*/
19+
public JSONObject toJson() {
20+
JSONObject jsonObject = new JSONObject();
21+
jsonObject.put("enabled", this.enabled);
22+
jsonObject.put("minWordSizeForTypos", this.minWordSizeForTypos);
23+
jsonObject.put("disableOnWords", this.disableOnWords);
24+
jsonObject.put("disableOnAttributes", this.disableOnAttributes);
25+
return jsonObject;
26+
}
27+
}

src/main/java/com/meilisearch/sdk/api/index/Settings.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.meilisearch.sdk.api.index;
22

33
import com.meilisearch.sdk.Index;
4+
import com.meilisearch.sdk.TypoTolerance;
45
import java.util.HashMap;
56
import lombok.Getter;
67
import lombok.Setter;
@@ -13,14 +14,15 @@
1314
@Getter
1415
@Setter
1516
public class Settings {
16-
private HashMap<String, String[]> synonyms;
17-
private String[] stopWords;
18-
private String[] rankingRules;
19-
private String[] filterableAttributes;
20-
private String distinctAttribute;
21-
private String[] searchableAttributes;
22-
private String[] displayedAttributes;
23-
private String[] sortableAttributes;
17+
@Getter @Setter private HashMap<String, String[]> synonyms;
18+
@Getter @Setter private String[] stopWords;
19+
@Getter @Setter private String[] rankingRules;
20+
@Getter @Setter private String[] filterableAttributes;
21+
@Getter @Setter private String distinctAttribute;
22+
@Getter @Setter private String[] searchableAttributes;
23+
@Getter @Setter private String[] displayedAttributes;
24+
@Getter @Setter private String[] sortableAttributes;
25+
@Getter @Setter private TypoTolerance typoTolerance;
2426

2527
/** Empty SettingsRequest constructor */
2628
public Settings() {}

0 commit comments

Comments
 (0)