Skip to content

Commit

Permalink
Merge #371
Browse files Browse the repository at this point in the history
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 <amelie@meilisearch.com>
Co-authored-by: Amélie <alallema@users.noreply.github.com>
  • Loading branch information
3 people authored May 25, 2022
2 parents 04244fa + d7bbdb5 commit 9073c17
Show file tree
Hide file tree
Showing 9 changed files with 395 additions and 63 deletions.
54 changes: 54 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,24 @@ update_displayed_attributes_1: |-
});
reset_displayed_attributes_1: |-
client.index("movies").resetDisplayedAttributesSettings();
get_typo_tolerance_1:
client.index("books").getTypoToleranceSettings();
update_typo_tolerance_1: |-
TypoTolerance typoTolerance = new TypoTolerance();
HashMap<String, Integer> minWordSizeTypos =
new HashMap<String, Integer>() {
{
put("oneTypo", 4);
put("twoTypos", 10);
}
};
typoTolerance.setMinWordSizeForTypos(minWordSizeTypos);
typoTolerance.setDisableOnAttributes(new String[] {"title"});
client.index("books").updateTypoToleranceSettings(typoTolerance);
reset_typo_tolerance_1: |-
client.index("books").resetTypoToleranceSettings();
get_index_stats_1: |-
client.index("movies").getStats();
get_indexes_stats_1: |-
Expand Down Expand Up @@ -334,6 +352,42 @@ settings_guide_sortable_1: |-
"author",
});
client.index("books").updateSettings(settings);
settings_guide_typo_tolerance_1: |-
TypoTolerance typoTolerance = new TypoTolerance();
HashMap<String, Integer> minWordSizeTypos =
new HashMap<String, Integer>() {
{
put("twoTypos", 12);
}
};
typoTolerance.setMinWordSizeForTypos(minWordSizeTypos);
typoTolerance.setDisableOnAttributes(new String[] {"title"});
client.index("movies").updateTypoToleranceSettings(typoTolerance);
typo_tolerance_guide_1: |-
TypoTolerance typoTolerance = new TypoTolerance();
typoTolerance.setEnabled(false);
client.index("movies").updateTypoToleranceSettings(typoTolerance);
typo_tolerance_guide_2: |-
TypoTolerance typoTolerance = new TypoTolerance();
typoTolerance.setDisableOnAttributes(new String[] {"title"});
client.index("movies").updateTypoToleranceSettings(typoTolerance);
typo_tolerance_guide_3: |-
TypoTolerance typoTolerance = new TypoTolerance();
typoTolerance.setDisableOnWords(new String[] {"shrek"});
client.index("movies").updateTypoToleranceSettings(typoTolerance);
typo_tolerance_guide_4: |-
TypoTolerance typoTolerance = new TypoTolerance();
HashMap<String, Integer> minWordSizeTypos =
new HashMap<String, Integer>() {
{
put("oneTypo", 4);
put("twoTypos", 10);
}
};
typoTolerance.setMinWordSizeForTypos(minWordSizeTypos);
client.index("movies").updateTypoToleranceSettings(typoTolerance);
documents_guide_add_movie_1: |-
client.index("movies").addDocuments("[{"
+ "\"movie_id\": 123sq178,"
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/meilisearch/sdk/Details.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public Details() {}
protected String[] stopWords;
protected Map<String, String[]> synonyms;
protected String distinctAttribute;
protected TypoTolerance typoTolerance;
}
34 changes: 34 additions & 0 deletions src/main/java/com/meilisearch/sdk/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,40 @@ public Task resetDistinctAttributeSettings() throws Exception {
return this.settingsHandler.resetDistinctAttributeSettings(this.uid);
}

/**
* Get the typo tolerance field of an index.
* https://docs.meilisearch.com/reference/api/typo_tolerance.html#get-typo-tolerance
*
* @return TypoTolerance instance from Index
* @throws Exception if an error occurs
*/
public TypoTolerance getTypoToleranceSettings() throws Exception {
return this.settingsHandler.getTypoToleranceSettings(this.uid);
}

/**
* Update the typo tolerance field of an index.
* https://docs.meilisearch.com/reference/api/typo_tolerance.html#update-typo-tolerance
*
* @param typoTolerance A TypoTolerance instance
* @return Task instance
* @throws Exception if an error occurs
*/
public Task updateTypoToleranceSettings(TypoTolerance typoTolerance) throws Exception {
return this.settingsHandler.updateTypoToleranceSettings(this.uid, typoTolerance);
}

/**
* Reset the typo tolerance field of an index to its default value.
* https://docs.meilisearch.com/reference/api/typo_tolerance.html#reset-typo-tolerance
*
* @return Task instance
* @throws Exception if an error occurs
*/
public Task resetTypoToleranceSettings() throws Exception {
return this.settingsHandler.resetTypoToleranceSettings(this.uid);
}

/**
* Retrieves an index tasks by its ID
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/meilisearch/sdk/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class Result<T> {
@Getter protected T[] results = null;

private static Gson gsonUpdate = new Gson();
private static Gson gsonResult = new Gson();

/**
* Method to return the JSON String of the Result
Expand All @@ -16,6 +16,6 @@ public class Result<T> {
*/
@Override
public String toString() {
return gsonUpdate.toJson(this);
return gsonResult.toJson(this);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/meilisearch/sdk/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Settings {
private String[] searchableAttributes;
private String[] displayedAttributes;
private String[] sortableAttributes;
private TypoTolerance typoTolerance;

/** Empty SettingsRequest constructor */
public Settings() {}
Expand Down Expand Up @@ -56,6 +57,9 @@ String getUpdateQuery() {
if (this.getSortableAttributes() != null) {
jsonObject.put("sortableAttributes", this.getSortableAttributes());
}
if (this.getTypoTolerance() != null) {
jsonObject.put("typoTolerance", this.getTypoTolerance().toJson());
}
return jsonObject.toString();
}
}
48 changes: 47 additions & 1 deletion src/main/java/com/meilisearch/sdk/SettingsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Task resetRankingRulesSettings(String uid) throws Exception {
* @throws Exception if an error occurs
*/
public Map<String, String[]> getSynonymsSettings(String uid) throws Exception {
return this.gson.<Map<String, String[]>>fromJson(
return this.gson.fromJson(
meilisearchHttpRequest.get("/indexes/" + uid + "/settings/synonyms"), Map.class);
}

Expand Down Expand Up @@ -390,4 +390,50 @@ public Task resetDistinctAttributeSettings(String uid) throws Exception {
meilisearchHttpRequest.delete("/indexes/" + uid + "/settings/distinct-attribute"),
Task.class);
}

/**
* Gets the typo tolerance settings of a given index Refer
* https://docs.meilisearch.com/reference/api/typo_tolerance.html#get-typo-tolerance
*
* @param uid Index identifier
* @return a TypoTolerance instance that contains all typo tolerance settings
* @throws Exception if an error occurs
*/
public TypoTolerance getTypoToleranceSettings(String uid) throws Exception {
return this.gson.fromJson(
meilisearchHttpRequest.get("/indexes/" + uid + "/settings/typo-tolerance"),
TypoTolerance.class);
}

/**
* Updates the typo tolerance settings of a given index Refer
* https://docs.meilisearch.com/reference/api/typo_tolerance.html#update-typo-tolerance
*
* @param uid Index identifier
* @param typoTolerance a TypoTolerance instance that contains the new typo tolerance settings
* @return Task instance
* @throws Exception if an error occurs
*/
public Task updateTypoToleranceSettings(String uid, TypoTolerance typoTolerance)
throws Exception {
String typoToleranceAsJson = gson.toJson(typoTolerance);
return this.gson.fromJson(
meilisearchHttpRequest.post(
"/indexes/" + uid + "/settings/typo-tolerance", typoToleranceAsJson),
Task.class);
}

/**
* Resets the typo tolerance settings of a given index Refer
* https://docs.meilisearch.com/reference/api/typo_tolerance.html#reset-typo-tolerance
*
* @param uid Index identifier
* @return Task instance
* @throws Exception if an error occurs
*/
public Task resetTypoToleranceSettings(String uid) throws Exception {
return this.gson.fromJson(
meilisearchHttpRequest.delete("/indexes/" + uid + "/settings/typo-tolerance"),
Task.class);
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/meilisearch/sdk/TypoTolerance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.meilisearch.sdk;

import java.util.HashMap;
import lombok.Getter;
import lombok.Setter;
import org.json.JSONObject;

public class TypoTolerance {
@Getter @Setter private boolean enabled = true;
@Getter @Setter private HashMap<String, Integer> minWordSizeForTypos;
@Getter @Setter private String[] disableOnWords;
@Getter @Setter private String[] disableOnAttributes;

/**
* Method to return the JSONObject of the TypoTolerance Setting
*
* @return JSONObject of the TypoTolerance Setting object
*/
public JSONObject toJson() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("enabled", this.enabled);
jsonObject.put("minWordSizeForTypos", this.minWordSizeForTypos);
jsonObject.put("disableOnWords", this.disableOnWords);
jsonObject.put("disableOnAttributes", this.disableOnAttributes);
return jsonObject;
}
}
18 changes: 10 additions & 8 deletions src/main/java/com/meilisearch/sdk/api/index/Settings.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.meilisearch.sdk.api.index;

import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.TypoTolerance;
import java.util.HashMap;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -13,14 +14,15 @@
@Getter
@Setter
public class Settings {
private HashMap<String, String[]> synonyms;
private String[] stopWords;
private String[] rankingRules;
private String[] filterableAttributes;
private String distinctAttribute;
private String[] searchableAttributes;
private String[] displayedAttributes;
private String[] sortableAttributes;
@Getter @Setter private HashMap<String, String[]> synonyms;
@Getter @Setter private String[] stopWords;
@Getter @Setter private String[] rankingRules;
@Getter @Setter private String[] filterableAttributes;
@Getter @Setter private String distinctAttribute;
@Getter @Setter private String[] searchableAttributes;
@Getter @Setter private String[] displayedAttributes;
@Getter @Setter private String[] sortableAttributes;
@Getter @Setter private TypoTolerance typoTolerance;

/** Empty SettingsRequest constructor */
public Settings() {}
Expand Down
Loading

0 comments on commit 9073c17

Please sign in to comment.