From 385813f9f731483e931b4f78aa512ec6d1c2734b Mon Sep 17 00:00:00 2001 From: alallema Date: Thu, 21 Apr 2022 16:08:46 +0200 Subject: [PATCH 01/13] Add typo tolerance settings --- .../java/com/meilisearch/sdk/Settings.java | 4 ++ .../com/meilisearch/sdk/SettingsHandler.java | 46 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/main/java/com/meilisearch/sdk/Settings.java b/src/main/java/com/meilisearch/sdk/Settings.java index 411e646f..b9052dee 100644 --- a/src/main/java/com/meilisearch/sdk/Settings.java +++ b/src/main/java/com/meilisearch/sdk/Settings.java @@ -19,6 +19,7 @@ public class Settings { @Getter @Setter private String[] searchableAttributes; @Getter @Setter private String[] displayedAttributes; @Getter @Setter private String[] sortableAttributes; + @Getter @Setter private HashMap typoTolerance; /** Empty SettingsRequest constructor */ public Settings() {} @@ -54,6 +55,9 @@ String getUpdateQuery() { if (this.getSortableAttributes() != null) { jsonObject.put("sortableAttributes", this.getSortableAttributes()); } + if (this.getTypoTolerance() != null) { + jsonObject.put("typoTolerance", this.getTypoTolerance()); + } return jsonObject.toString(); } } diff --git a/src/main/java/com/meilisearch/sdk/SettingsHandler.java b/src/main/java/com/meilisearch/sdk/SettingsHandler.java index 8a831f58..e6f7fa72 100644 --- a/src/main/java/com/meilisearch/sdk/SettingsHandler.java +++ b/src/main/java/com/meilisearch/sdk/SettingsHandler.java @@ -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 Map that contains all typo tolerance and their associated words + * @throws Exception if an error occurs + */ + public Map getTypoToleranceSettings(String uid) throws Exception { + return this.gson.>fromJson( + meilisearchHttpRequest.get("/indexes/" + uid + "/settings/typo-tolerance"), + Map.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 Map that contains the new typo tolerance settings + * @return Task instance + * @throws Exception if an error occurs + */ + public Task updateTypoToleranceSettings(String uid, Map 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); + } } From 151c2e06b0f0ab7f55447e15f2994197512feaeb Mon Sep 17 00:00:00 2001 From: alallema Date: Mon, 2 May 2022 16:15:27 +0200 Subject: [PATCH 02/13] Add typo tolerance method to settings --- .../java/com/meilisearch/sdk/Details.java | 9 +++ src/main/java/com/meilisearch/sdk/Result.java | 4 +- .../java/com/meilisearch/sdk/Settings.java | 4 +- .../com/meilisearch/sdk/SettingsHandler.java | 6 +- .../com/meilisearch/sdk/TypoTolerance.java | 60 +++++++++++++++++++ .../meilisearch/sdk/api/index/Settings.java | 10 ++++ 6 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/meilisearch/sdk/TypoTolerance.java diff --git a/src/main/java/com/meilisearch/sdk/Details.java b/src/main/java/com/meilisearch/sdk/Details.java index 6af44ddd..2e5875f6 100644 --- a/src/main/java/com/meilisearch/sdk/Details.java +++ b/src/main/java/com/meilisearch/sdk/Details.java @@ -18,6 +18,7 @@ public Details() {} protected String[] stopWords; protected Map synonyms; protected String distinctAttribute; + protected TypoTolerance typoTolerance; public int getReceivedDocuments() { return receivedDocuments; @@ -114,4 +115,12 @@ public String getDistinctAttribute() { public void setDistinctAttribute(String distinctAttribute) { this.distinctAttribute = distinctAttribute; } + + public TypoTolerance getTypoTolerance() { + return typoTolerance; + } + + public void setTypoTolerance(TypoTolerance typoTolerance) { + this.typoTolerance = typoTolerance; + } } diff --git a/src/main/java/com/meilisearch/sdk/Result.java b/src/main/java/com/meilisearch/sdk/Result.java index a04bedcf..9e45094d 100644 --- a/src/main/java/com/meilisearch/sdk/Result.java +++ b/src/main/java/com/meilisearch/sdk/Result.java @@ -6,7 +6,7 @@ public class Result { 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 @@ -15,7 +15,7 @@ public class Result { */ @Override public String toString() { - return gsonUpdate.toJson(this); + return gsonResult.toJson(this); } /** diff --git a/src/main/java/com/meilisearch/sdk/Settings.java b/src/main/java/com/meilisearch/sdk/Settings.java index b9052dee..6b6dbb9e 100644 --- a/src/main/java/com/meilisearch/sdk/Settings.java +++ b/src/main/java/com/meilisearch/sdk/Settings.java @@ -19,7 +19,7 @@ public class Settings { @Getter @Setter private String[] searchableAttributes; @Getter @Setter private String[] displayedAttributes; @Getter @Setter private String[] sortableAttributes; - @Getter @Setter private HashMap typoTolerance; + @Getter @Setter private TypoTolerance typoTolerance; /** Empty SettingsRequest constructor */ public Settings() {} @@ -56,7 +56,7 @@ String getUpdateQuery() { jsonObject.put("sortableAttributes", this.getSortableAttributes()); } if (this.getTypoTolerance() != null) { - jsonObject.put("typoTolerance", this.getTypoTolerance()); + jsonObject.put("typoTolerance", this.getTypoTolerance().toJson()); } return jsonObject.toString(); } diff --git a/src/main/java/com/meilisearch/sdk/SettingsHandler.java b/src/main/java/com/meilisearch/sdk/SettingsHandler.java index e6f7fa72..58747874 100644 --- a/src/main/java/com/meilisearch/sdk/SettingsHandler.java +++ b/src/main/java/com/meilisearch/sdk/SettingsHandler.java @@ -399,8 +399,8 @@ public Task resetDistinctAttributeSettings(String uid) throws Exception { * @return a Map that contains all typo tolerance and their associated words * @throws Exception if an error occurs */ - public Map getTypoToleranceSettings(String uid) throws Exception { - return this.gson.>fromJson( + public TypoTolerance getTypoToleranceSettings(String uid) throws Exception { + return this.gson.fromJson( meilisearchHttpRequest.get("/indexes/" + uid + "/settings/typo-tolerance"), Map.class); } @@ -414,7 +414,7 @@ public Map getTypoToleranceSettings(String uid) throws Excepti * @return Task instance * @throws Exception if an error occurs */ - public Task updateTypoToleranceSettings(String uid, Map typoTolerance) + public Task updateTypoToleranceSettings(String uid, TypoTolerance typoTolerance) throws Exception { String typoToleranceAsJson = gson.toJson(typoTolerance); return this.gson.fromJson( diff --git a/src/main/java/com/meilisearch/sdk/TypoTolerance.java b/src/main/java/com/meilisearch/sdk/TypoTolerance.java new file mode 100644 index 00000000..aa19866c --- /dev/null +++ b/src/main/java/com/meilisearch/sdk/TypoTolerance.java @@ -0,0 +1,60 @@ +package com.meilisearch.sdk; + +import java.util.HashMap; +import org.json.JSONObject; + +public class TypoTolerance { + + public TypoTolerance() {} + + protected boolean enabled = false; + protected HashMap minWordSizeForTypos; + protected String[] disableOnWords; + protected 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; + } + + public boolean getEnabled() { + return this.enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public String[] getDisableOnWords() { + return this.disableOnWords; + } + + public void setDisableOnWords(String[] disableOnWords) { + this.disableOnWords = disableOnWords; + } + + public String[] getDisableOnAttributes() { + return this.disableOnAttributes; + } + + public void setDisableOnAttributes(String[] disableOnAttributes) { + this.disableOnAttributes = disableOnAttributes; + } + + public HashMap getMinWordSizeForTypos() { + return this.minWordSizeForTypos; + } + + public void setMinWordSizeForTypos(HashMap minWordSizeForTypos) { + this.minWordSizeForTypos = minWordSizeForTypos; + } +} diff --git a/src/main/java/com/meilisearch/sdk/api/index/Settings.java b/src/main/java/com/meilisearch/sdk/api/index/Settings.java index 667e2182..db5a2d86 100644 --- a/src/main/java/com/meilisearch/sdk/api/index/Settings.java +++ b/src/main/java/com/meilisearch/sdk/api/index/Settings.java @@ -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 java.util.Map; @@ -18,6 +19,7 @@ public class Settings { private String[] searchableAttributes; private String[] displayedAttributes; private String[] sortableAttributes; + private TypoTolerance typoTolerance; /** Empty SettingsRequest constructor */ public Settings() {} @@ -85,4 +87,12 @@ public String[] getSortableAttributes() { public void setSortableAttributes(String[] sortableAttributes) { this.sortableAttributes = sortableAttributes; } + + public TypoTolerance getTypoTolerance() { + return typoTolerance; + } + + public void setTypoTolerance(TypoTolerance typoTolerance) { + this.typoTolerance = typoTolerance; + } } From eb74848c93627dabdb7218f7b3c0e7c071e3ac8c Mon Sep 17 00:00:00 2001 From: alallema Date: Tue, 3 May 2022 14:12:51 +0200 Subject: [PATCH 03/13] Add tests for typo tolerance setting --- src/main/java/com/meilisearch/sdk/Index.java | 34 +++ .../com/meilisearch/sdk/SettingsHandler.java | 6 +- .../meilisearch/integration/SettingsTest.java | 245 ++++++++++++++---- 3 files changed, 230 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/meilisearch/sdk/Index.java b/src/main/java/com/meilisearch/sdk/Index.java index 7df4f036..032478be 100644 --- a/src/main/java/com/meilisearch/sdk/Index.java +++ b/src/main/java/com/meilisearch/sdk/Index.java @@ -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/distinct_attribute.html#get-distinct-attribute + * + * @return typo tolerance field of a given uid as TypoTolerance instance + * @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/distinct_attribute.html#update-distinct-attribute + * + * @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/distinct_attribute.html#reset-distinct-attribute + * + * @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 * diff --git a/src/main/java/com/meilisearch/sdk/SettingsHandler.java b/src/main/java/com/meilisearch/sdk/SettingsHandler.java index 58747874..a208d97e 100644 --- a/src/main/java/com/meilisearch/sdk/SettingsHandler.java +++ b/src/main/java/com/meilisearch/sdk/SettingsHandler.java @@ -396,13 +396,13 @@ public Task resetDistinctAttributeSettings(String uid) throws Exception { * https://docs.meilisearch.com/reference/api/typo-tolerance.html#get-typo-tolerance * * @param uid Index identifier - * @return a Map that contains all typo tolerance and their associated words + * @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"), - Map.class); + TypoTolerance.class); } /** @@ -410,7 +410,7 @@ public TypoTolerance getTypoToleranceSettings(String uid) throws Exception { * https://docs.meilisearch.com/reference/api/typo-tolerance.html#update-typo-tolerance * * @param uid Index identifier - * @param typoTolerance a Map that contains the new typo tolerance settings + * @param typoTolerance a TypoTolerance instance that contains the new typo tolerance settings * @return Task instance * @throws Exception if an error occurs */ diff --git a/src/test/java/com/meilisearch/integration/SettingsTest.java b/src/test/java/com/meilisearch/integration/SettingsTest.java index 831842f8..971179e7 100644 --- a/src/test/java/com/meilisearch/integration/SettingsTest.java +++ b/src/test/java/com/meilisearch/integration/SettingsTest.java @@ -5,12 +5,14 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import com.meilisearch.integration.classes.AbstractIT; import com.meilisearch.integration.classes.TestData; import com.meilisearch.sdk.Index; import com.meilisearch.sdk.Settings; import com.meilisearch.sdk.Task; +import com.meilisearch.sdk.TypoTolerance; import com.meilisearch.sdk.json.GsonJsonHandler; import com.meilisearch.sdk.utils.Movie; import java.util.Arrays; @@ -39,6 +41,7 @@ static void cleanMeiliSearch() { cleanup(); } + /** Tests of the setting methods */ @Test @DisplayName("Test get settings from an index by uid") public void testGetSettings() throws Exception { @@ -101,6 +104,27 @@ public void testUpdateSettingsSort() throws Exception { assertEquals(2, newSettings.getSortableAttributes().length); } + @Test + @DisplayName("Test update settings changing the typo tolerance") + public void testUpdateSettingsTypoTolerance() throws Exception { + Index index = createIndex("updateSettingsSynonyms"); + Settings settings = index.getSettings(); + + TypoTolerance typoTolerance = new TypoTolerance(); + typoTolerance.setEnabled(true); + typoTolerance.setDisableOnWords(new String[] {"and"}); + typoTolerance.setDisableOnAttributes(new String[] {"title"}); + settings.setTypoTolerance(typoTolerance); + + index.waitForTask(index.updateSettings(settings).getUid()); + + Settings newSettings = index.getSettings(); + + assertEquals(1, newSettings.getTypoTolerance().getDisableOnWords().length); + assertEquals(1, newSettings.getTypoTolerance().getDisableOnAttributes().length); + assertEquals(true, newSettings.getTypoTolerance().getEnabled()); + } + @Test @DisplayName("Test reset settings") public void testResetSettings() throws Exception { @@ -122,6 +146,7 @@ public void testResetSettings() throws Exception { assertEquals(initialSettings.getSynonyms().size(), settingsAfterReset.getSynonyms().size()); } + /** Tests of the ranking rules setting methods */ @Test @DisplayName("Test get ranking rules settings by uid") public void testGetRankingRuleSettings() throws Exception { @@ -176,18 +201,19 @@ public void testResetRankingRuleSettings() throws Exception { index.waitForTask(index.updateRankingRuleSettings(newRankingRules).getUid()); String[] updatedRankingRuleSettings = index.getRankingRuleSettings(); + index.waitForTask(index.resetRankingRuleSettings().getUid()); + String[] rankingRulesAfterReset = index.getRankingRuleSettings(); + assertEquals(newRankingRules.length, updatedRankingRuleSettings.length); assertArrayEquals(newRankingRules, updatedRankingRuleSettings); assertNotEquals(initialRuleSettings.length, updatedRankingRuleSettings.length); - index.waitForTask(index.resetRankingRuleSettings().getUid()); - String[] rankingRulesAfterReset = index.getRankingRuleSettings(); - assertNotEquals(updatedRankingRuleSettings.length, rankingRulesAfterReset.length); assertEquals(initialRuleSettings.length, rankingRulesAfterReset.length); assertArrayEquals(initialRuleSettings, rankingRulesAfterReset); } + /** Tests of the synonyms setting methods */ @Test @DisplayName("Test get synonyms settings by uid") public void testGetSynonymsSettings() throws Exception { @@ -210,12 +236,12 @@ public void testUpdateSynonymsSettings() throws Exception { newSynonymsSettings.put("wow", new String[] {"world of warcraft"}); index.waitForTask(index.updateSynonymsSettings(newSynonymsSettings).getUid()); - Map updatedRankingRuleSettings = index.getSynonymsSettings(); + Map updatedSynonymsSettings = index.getSynonymsSettings(); - assertEquals(newSynonymsSettings.size(), updatedRankingRuleSettings.size()); - assertEquals(newSynonymsSettings.keySet(), updatedRankingRuleSettings.keySet()); - assertNotEquals(synonymsSettings.size(), updatedRankingRuleSettings.size()); - assertNotEquals(synonymsSettings.keySet(), updatedRankingRuleSettings.keySet()); + assertEquals(newSynonymsSettings.size(), updatedSynonymsSettings.size()); + assertEquals(newSynonymsSettings.keySet(), updatedSynonymsSettings.keySet()); + assertNotEquals(synonymsSettings.size(), updatedSynonymsSettings.size()); + assertNotEquals(synonymsSettings.keySet(), updatedSynonymsSettings.keySet()); } @Test @@ -229,21 +255,22 @@ public void testResetSynonymsSettings() throws Exception { newSynonymsSettings.put("wow", new String[] {"world of warcraft"}); index.waitForTask(index.updateSynonymsSettings(newSynonymsSettings).getUid()); - Map updatedRankingRuleSettings = index.getSynonymsSettings(); - - assertEquals(newSynonymsSettings.size(), updatedRankingRuleSettings.size()); - assertEquals(newSynonymsSettings.keySet(), updatedRankingRuleSettings.keySet()); - assertNotEquals(synonymsSettings.size(), updatedRankingRuleSettings.size()); - assertNotEquals(synonymsSettings.keySet(), updatedRankingRuleSettings.keySet()); + Map updatedSynonymsSettings = index.getSynonymsSettings(); index.waitForTask(index.resetSynonymsSettings().getUid()); Map synonymsSettingsAfterReset = index.getSynonymsSettings(); - assertNotEquals(updatedRankingRuleSettings.size(), synonymsSettingsAfterReset.size()); + assertEquals(newSynonymsSettings.size(), updatedSynonymsSettings.size()); + assertEquals(newSynonymsSettings.keySet(), updatedSynonymsSettings.keySet()); + assertNotEquals(synonymsSettings.size(), updatedSynonymsSettings.size()); + assertNotEquals(synonymsSettings.keySet(), updatedSynonymsSettings.keySet()); + + assertNotEquals(updatedSynonymsSettings.size(), synonymsSettingsAfterReset.size()); assertEquals(synonymsSettings.size(), synonymsSettingsAfterReset.size()); assertEquals(synonymsSettings.keySet(), synonymsSettingsAfterReset.keySet()); } + /** Tests of the stop words setting methods */ @Test @DisplayName("Test get stop-words settings by uid") public void testGetStopWordsSettings() throws Exception { @@ -280,18 +307,19 @@ public void testResetStopWordsSettings() throws Exception { index.waitForTask(index.updateStopWordsSettings(newStopWords).getUid()); String[] updatedStopWordsSettings = index.getStopWordsSettings(); + index.waitForTask(index.resetStopWordsSettings().getUid()); + String[] stopWordsAfterReset = index.getStopWordsSettings(); + assertEquals(newStopWords.length, updatedStopWordsSettings.length); assertArrayEquals(newStopWords, updatedStopWordsSettings); assertNotEquals(initialStopWords.length, updatedStopWordsSettings.length); - index.waitForTask(index.resetStopWordsSettings().getUid()); - String[] stopWordsAfterReset = index.getStopWordsSettings(); - assertNotEquals(updatedStopWordsSettings.length, stopWordsAfterReset.length); assertEquals(initialStopWords.length, stopWordsAfterReset.length); assertArrayEquals(initialStopWords, stopWordsAfterReset); } + /** Tests of the searchable attributes setting methods */ @Test @DisplayName("Test get searchable attributes settings by uid") public void testGetSearchableAttributesSettings() throws Exception { @@ -332,18 +360,19 @@ public void testResetSearchableAttributesSettings() throws Exception { index.updateSearchableAttributesSettings(newSearchableAttributes).getUid()); String[] updatedSearchableAttributes = index.getSearchableAttributesSettings(); + index.waitForTask(index.resetSearchableAttributesSettings().getUid()); + String[] searchableAttributesAfterReset = index.getSearchableAttributesSettings(); + assertEquals(newSearchableAttributes.length, updatedSearchableAttributes.length); assertArrayEquals(newSearchableAttributes, updatedSearchableAttributes); assertNotEquals(initialSearchableAttributes.length, updatedSearchableAttributes.length); - index.waitForTask(index.resetSearchableAttributesSettings().getUid()); - String[] searchableAttributesAfterReset = index.getSearchableAttributesSettings(); - assertNotEquals(updatedSearchableAttributes.length, searchableAttributesAfterReset.length); assertEquals(initialSearchableAttributes.length, searchableAttributesAfterReset.length); assertArrayEquals(initialSearchableAttributes, searchableAttributesAfterReset); } + /** Tests of the display attributes setting methods */ @Test @DisplayName("Test get display attributes settings by uid") public void testGetDisplayedAttributesSettings() throws Exception { @@ -382,16 +411,17 @@ public void testResetDisplayedAttributesSettings() throws Exception { index.waitForTask(index.updateDisplayedAttributesSettings(newDisplayedAttributes).getUid()); String[] updatedDisplayedAttributes = index.getDisplayedAttributesSettings(); + index.waitForTask(index.resetDisplayedAttributesSettings().getUid()); + String[] displayedAttributesAfterReset = index.getDisplayedAttributesSettings(); + assertEquals(newDisplayedAttributes.length, updatedDisplayedAttributes.length); assertArrayEquals(newDisplayedAttributes, updatedDisplayedAttributes); assertNotEquals(initialDisplayedAttributes.length, updatedDisplayedAttributes.length); - index.waitForTask(index.resetDisplayedAttributesSettings().getUid()); - String[] displayedAttributesAfterReset = index.getDisplayedAttributesSettings(); - assertNotEquals(updatedDisplayedAttributes.length, displayedAttributesAfterReset.length); } + /** Tests of the filterable attributes setting methods */ @Test @DisplayName("Test get filterable attributes settings by uid") public void testGetFilterableAttributesSettings() throws Exception { @@ -436,19 +466,20 @@ public void testResetFilterableAttributesSettings() throws Exception { index.updateFilterableAttributesSettings(newFilterableAttributes).getUid()); String[] updatedFilterableAttributes = index.getFilterableAttributesSettings(); + index.waitForTask(index.resetFilterableAttributesSettings().getUid()); + String[] filterableAttributesAfterReset = index.getFilterableAttributesSettings(); + assertEquals(newFilterableAttributes.length, updatedFilterableAttributes.length); assertThat( Arrays.asList(newFilterableAttributes), containsInAnyOrder(updatedFilterableAttributes)); assertNotEquals(initialFilterableAttributes.length, updatedFilterableAttributes.length); - index.waitForTask(index.resetFilterableAttributesSettings().getUid()); - String[] filterableAttributesAfterReset = index.getFilterableAttributesSettings(); - assertNotEquals(updatedFilterableAttributes.length, filterableAttributesAfterReset.length); assertNotEquals(initialFilterableAttributes.length, updatedFilterableAttributes.length); } + /** Tests of the distinct attributes setting methods */ @Test @DisplayName("Test get distinct attribute settings by uid") public void testGetDistinctAttributeSettings() throws Exception { @@ -476,50 +507,133 @@ public void testUpdateDistinctAttributeSettings() throws Exception { @Test @DisplayName("Test reset distinct attribute settings") public void testResetDistinctAttributeSettings() throws Exception { - Index index = createIndex("testUpdateDistinctAttributeSettings"); + Index index = createIndex("testResetDistinctAttributeSettings"); String initialDistinctAttribute = index.getDistinctAttributeSettings(); String newDistinctAttribute = "title"; index.waitForTask(index.updateDistinctAttributeSettings(newDistinctAttribute).getUid()); String updatedDistinctAttribute = index.getDistinctAttributeSettings(); - assertEquals(newDistinctAttribute, updatedDistinctAttribute); - assertNotEquals(initialDistinctAttribute, updatedDistinctAttribute); - index.waitForTask(index.resetDistinctAttributeSettings().getUid()); String distinctAttributeAfterReset = index.getDistinctAttributeSettings(); + assertEquals(newDistinctAttribute, updatedDistinctAttribute); + assertNotEquals(initialDistinctAttribute, updatedDistinctAttribute); + assertNotEquals(updatedDistinctAttribute, distinctAttributeAfterReset); assertNotEquals(initialDistinctAttribute, updatedDistinctAttribute); } + /** Tests of the typo tolerance setting methods */ @Test - @DisplayName("Test reset ranking rules when null value is passed") - public void testUpdateRankingRuleSettingsUsingNull() throws Exception { - Index index = createIndex("testUpdateRankingRuleSettingsUsingNull"); - String[] initialRankingRule = index.getRankingRuleSettings(); - String[] newRankingRules = { - "typo", - "words", - "sort", - "proximity", - "attribute", - "exactness", - "release_date:desc", - "rank:desc" - }; + @DisplayName("Test get typo tolerance settings by uid") + public void testGetTypoTolerance() throws Exception { + Index index = createIndex("testGetTypoTolerance"); + Settings initialSettings = index.getSettings(); + TypoTolerance initialTypoTolerance = index.getTypoToleranceSettings(); - index.waitForTask(index.updateRankingRuleSettings(newRankingRules).getUid()); - String[] newRankingRule = index.getRankingRuleSettings(); + assertEquals( + initialSettings.getTypoTolerance().getDisableOnWords().length, + initialTypoTolerance.getDisableOnWords().length); + assertEquals( + initialSettings.getTypoTolerance().getDisableOnAttributes().length, + initialTypoTolerance.getDisableOnAttributes().length); + assertEquals( + initialSettings.getTypoTolerance().getEnabled(), initialTypoTolerance.getEnabled()); + assertTrue( + initialTypoTolerance.getMinWordSizeForTypos().containsKey("oneTypo") + && initialTypoTolerance.getMinWordSizeForTypos().get("oneTypo") != null); + assertTrue( + initialTypoTolerance.getMinWordSizeForTypos().containsKey("twoTypos") + && initialTypoTolerance.getMinWordSizeForTypos().get("twoTypos") != null); + } + + @Test + @DisplayName("Test update typo tolerance settings") + public void testUpdateTypoTolerance() throws Exception { + Index index = createIndex("testUpdateTypoTolerance"); + TypoTolerance initialTypoTolerance = index.getTypoToleranceSettings(); + TypoTolerance newTypoTolerance = new TypoTolerance(); + newTypoTolerance.setEnabled(true); + newTypoTolerance.setDisableOnWords(new String[] {"and"}); + newTypoTolerance.setDisableOnAttributes(new String[] {"title"}); + + HashMap minWordSizeTypos = + new HashMap() { + { + put("oneTypo", 7); + put("twoTypos", 10); + } + }; + newTypoTolerance.setMinWordSizeForTypos(minWordSizeTypos); + index.waitForTask(index.updateTypoToleranceSettings(newTypoTolerance).getUid()); + TypoTolerance updatedTypoTolerance = index.getTypoToleranceSettings(); - index.waitForTask(index.updateRankingRuleSettings(null).getUid()); - String[] resetRankingRule = index.getRankingRuleSettings(); + assertEquals( + newTypoTolerance.getDisableOnWords().length, + updatedTypoTolerance.getDisableOnWords().length); + assertEquals( + newTypoTolerance.getDisableOnAttributes().length, + updatedTypoTolerance.getDisableOnAttributes().length); + assertEquals(newTypoTolerance.getEnabled(), updatedTypoTolerance.getEnabled()); + assertTrue( + updatedTypoTolerance.getMinWordSizeForTypos().containsKey("oneTypo") + && updatedTypoTolerance.getMinWordSizeForTypos().get("oneTypo") == 7); + assertTrue( + updatedTypoTolerance.getMinWordSizeForTypos().containsKey("twoTypos") + && updatedTypoTolerance.getMinWordSizeForTypos().get("twoTypos") == 10); + } + + @Test + @DisplayName("Test reset typo tolerance settings") + public void testResetTypoTolerance() throws Exception { + Index index = createIndex("testResetTypoTolerance"); + + TypoTolerance initialTypoTolerance = index.getTypoToleranceSettings(); + TypoTolerance newTypoTolerance = new TypoTolerance(); + newTypoTolerance.setEnabled(true); + newTypoTolerance.setDisableOnWords(new String[] {"and"}); + newTypoTolerance.setDisableOnAttributes(new String[] {"title"}); + HashMap minWordSizeTypos = + new HashMap() { + { + put("oneTypo", 7); + put("twoTypos", 10); + } + }; + newTypoTolerance.setMinWordSizeForTypos(minWordSizeTypos); + + index.waitForTask(index.updateTypoToleranceSettings(newTypoTolerance).getUid()); + TypoTolerance updatedTypoTolerance = index.getTypoToleranceSettings(); + + index.waitForTask(index.resetTypoToleranceSettings().getUid()); + TypoTolerance typoToleranceAfterReset = index.getTypoToleranceSettings(); - assertNotEquals(newRankingRule.length, resetRankingRule.length); - assertEquals(initialRankingRule.length, resetRankingRule.length); - assertArrayEquals(initialRankingRule, resetRankingRule); + assertEquals( + newTypoTolerance.getDisableOnWords().length, + updatedTypoTolerance.getDisableOnWords().length); + assertEquals( + newTypoTolerance.getDisableOnAttributes().length, + updatedTypoTolerance.getDisableOnAttributes().length); + assertEquals(newTypoTolerance.getEnabled(), updatedTypoTolerance.getEnabled()); + + assertEquals( + initialTypoTolerance.getDisableOnWords().length, + typoToleranceAfterReset.getDisableOnWords().length); + assertEquals( + initialTypoTolerance.getDisableOnAttributes().length, + typoToleranceAfterReset.getDisableOnAttributes().length); + assertEquals(initialTypoTolerance.getEnabled(), typoToleranceAfterReset.getEnabled()); + assertTrue( + typoToleranceAfterReset.getMinWordSizeForTypos().containsKey("oneTypo") + && typoToleranceAfterReset.getMinWordSizeForTypos().get("oneTypo") != null); + assertTrue( + typoToleranceAfterReset.getMinWordSizeForTypos().containsKey("twoTypos") + && typoToleranceAfterReset.getMinWordSizeForTypos().get("twoTypos") + != null); } + /** Tests of all the specifics setting methods when null is passed */ @Test @DisplayName("Test update synonyms settings when null is passed") public void testUpdateSynonymsSettingsUsingNull() throws Exception { @@ -560,6 +674,33 @@ public void testUpdateStopWordsSettingsUsingNull() throws Exception { assertArrayEquals(initialStopWords, resetStopWords); } + @Test + @DisplayName("Test reset ranking rules when null value is passed") + public void testUpdateRankingRuleSettingsUsingNull() throws Exception { + Index index = createIndex("testUpdateRankingRuleSettingsUsingNull"); + String[] initialRankingRule = index.getRankingRuleSettings(); + String[] newRankingRules = { + "typo", + "words", + "sort", + "proximity", + "attribute", + "exactness", + "release_date:desc", + "rank:desc" + }; + + index.waitForTask(index.updateRankingRuleSettings(newRankingRules).getUid()); + String[] newRankingRule = index.getRankingRuleSettings(); + + index.waitForTask(index.updateRankingRuleSettings(null).getUid()); + String[] resetRankingRule = index.getRankingRuleSettings(); + + assertNotEquals(newRankingRule.length, resetRankingRule.length); + assertEquals(initialRankingRule.length, resetRankingRule.length); + assertArrayEquals(initialRankingRule, resetRankingRule); + } + @Test @DisplayName("Test update searchable attributes settings when null is passed") public void testUpdateSearchableAttributesSettingssUsingNull() throws Exception { From 7d0451a97430247474b46f2574943d829686dae9 Mon Sep 17 00:00:00 2001 From: alallema Date: Wed, 4 May 2022 16:50:31 +0200 Subject: [PATCH 04/13] Fix due to the review --- src/main/java/com/meilisearch/sdk/Index.java | 8 ++++---- src/main/java/com/meilisearch/sdk/SettingsHandler.java | 6 +++--- src/main/java/com/meilisearch/sdk/TypoTolerance.java | 3 --- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/meilisearch/sdk/Index.java b/src/main/java/com/meilisearch/sdk/Index.java index 032478be..c9d9aeba 100644 --- a/src/main/java/com/meilisearch/sdk/Index.java +++ b/src/main/java/com/meilisearch/sdk/Index.java @@ -569,9 +569,9 @@ public Task resetDistinctAttributeSettings() throws Exception { /** * Get the typo tolerance field of an index. - * https://docs.meilisearch.com/reference/api/distinct_attribute.html#get-distinct-attribute + * https://docs.meilisearch.com/reference/api/typo_tolerance.html#get-typo-tolerance * - * @return typo tolerance field of a given uid as TypoTolerance instance + * @return TypoTolerance instance from Index * @throws Exception if an error occurs */ public TypoTolerance getTypoToleranceSettings() throws Exception { @@ -580,7 +580,7 @@ public TypoTolerance getTypoToleranceSettings() throws Exception { /** * Update the typo tolerance field of an index. - * https://docs.meilisearch.com/reference/api/distinct_attribute.html#update-distinct-attribute + * https://docs.meilisearch.com/reference/api/typo_tolerance.html#update-typo-tolerance * * @param typoTolerance A TypoTolerance instance * @return Task instance @@ -592,7 +592,7 @@ public Task updateTypoToleranceSettings(TypoTolerance typoTolerance) throws Exce /** * Reset the typo tolerance field of an index to its default value. - * https://docs.meilisearch.com/reference/api/distinct_attribute.html#reset-distinct-attribute + * https://docs.meilisearch.com/reference/api/typo_tolerance.html#reset-typo-tolerance * * @return Task instance * @throws Exception if an error occurs diff --git a/src/main/java/com/meilisearch/sdk/SettingsHandler.java b/src/main/java/com/meilisearch/sdk/SettingsHandler.java index a208d97e..13d492e3 100644 --- a/src/main/java/com/meilisearch/sdk/SettingsHandler.java +++ b/src/main/java/com/meilisearch/sdk/SettingsHandler.java @@ -393,7 +393,7 @@ public Task resetDistinctAttributeSettings(String uid) throws Exception { /** * Gets the typo tolerance settings of a given index Refer - * https://docs.meilisearch.com/reference/api/typo-tolerance.html#get-typo-tolerance + * 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 @@ -407,7 +407,7 @@ public TypoTolerance getTypoToleranceSettings(String uid) throws Exception { /** * Updates the typo tolerance settings of a given index Refer - * https://docs.meilisearch.com/reference/api/typo-tolerance.html#update-typo-tolerance + * 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 @@ -425,7 +425,7 @@ public Task updateTypoToleranceSettings(String uid, TypoTolerance typoTolerance) /** * Resets the typo tolerance settings of a given index Refer - * https://docs.meilisearch.com/reference/api/typo-tolerance.html#reset-typo-tolerance + * https://docs.meilisearch.com/reference/api/typo_tolerance.html#reset-typo-tolerance * * @param uid Index identifier * @return Task instance diff --git a/src/main/java/com/meilisearch/sdk/TypoTolerance.java b/src/main/java/com/meilisearch/sdk/TypoTolerance.java index aa19866c..e21d8fb8 100644 --- a/src/main/java/com/meilisearch/sdk/TypoTolerance.java +++ b/src/main/java/com/meilisearch/sdk/TypoTolerance.java @@ -4,9 +4,6 @@ import org.json.JSONObject; public class TypoTolerance { - - public TypoTolerance() {} - protected boolean enabled = false; protected HashMap minWordSizeForTypos; protected String[] disableOnWords; From f4246122a6acf2728ee2401b0510859d33ca2d19 Mon Sep 17 00:00:00 2001 From: alallema Date: Wed, 4 May 2022 18:35:16 +0200 Subject: [PATCH 05/13] Exchange getter setter for lombok --- .../com/meilisearch/sdk/TypoTolerance.java | 42 +++---------------- .../meilisearch/integration/SettingsTest.java | 10 ++--- 2 files changed, 11 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/meilisearch/sdk/TypoTolerance.java b/src/main/java/com/meilisearch/sdk/TypoTolerance.java index e21d8fb8..f3abb838 100644 --- a/src/main/java/com/meilisearch/sdk/TypoTolerance.java +++ b/src/main/java/com/meilisearch/sdk/TypoTolerance.java @@ -1,13 +1,15 @@ package com.meilisearch.sdk; import java.util.HashMap; +import lombok.Getter; +import lombok.Setter; import org.json.JSONObject; public class TypoTolerance { - protected boolean enabled = false; - protected HashMap minWordSizeForTypos; - protected String[] disableOnWords; - protected String[] disableOnAttributes; + @Getter @Setter private boolean enabled = false; + @Getter @Setter private HashMap minWordSizeForTypos; + @Getter @Setter private String[] disableOnWords; + @Getter @Setter private String[] disableOnAttributes; /** * Method to return the JSONObject of the TypoTolerance Setting @@ -22,36 +24,4 @@ public JSONObject toJson() { jsonObject.put("disableOnAttributes", this.disableOnAttributes); return jsonObject; } - - public boolean getEnabled() { - return this.enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public String[] getDisableOnWords() { - return this.disableOnWords; - } - - public void setDisableOnWords(String[] disableOnWords) { - this.disableOnWords = disableOnWords; - } - - public String[] getDisableOnAttributes() { - return this.disableOnAttributes; - } - - public void setDisableOnAttributes(String[] disableOnAttributes) { - this.disableOnAttributes = disableOnAttributes; - } - - public HashMap getMinWordSizeForTypos() { - return this.minWordSizeForTypos; - } - - public void setMinWordSizeForTypos(HashMap minWordSizeForTypos) { - this.minWordSizeForTypos = minWordSizeForTypos; - } } diff --git a/src/test/java/com/meilisearch/integration/SettingsTest.java b/src/test/java/com/meilisearch/integration/SettingsTest.java index 971179e7..7eb80d2d 100644 --- a/src/test/java/com/meilisearch/integration/SettingsTest.java +++ b/src/test/java/com/meilisearch/integration/SettingsTest.java @@ -122,7 +122,7 @@ public void testUpdateSettingsTypoTolerance() throws Exception { assertEquals(1, newSettings.getTypoTolerance().getDisableOnWords().length); assertEquals(1, newSettings.getTypoTolerance().getDisableOnAttributes().length); - assertEquals(true, newSettings.getTypoTolerance().getEnabled()); + assertEquals(true, newSettings.getTypoTolerance().isEnabled()); } @Test @@ -539,7 +539,7 @@ public void testGetTypoTolerance() throws Exception { initialSettings.getTypoTolerance().getDisableOnAttributes().length, initialTypoTolerance.getDisableOnAttributes().length); assertEquals( - initialSettings.getTypoTolerance().getEnabled(), initialTypoTolerance.getEnabled()); + initialSettings.getTypoTolerance().isEnabled(), initialTypoTolerance.isEnabled()); assertTrue( initialTypoTolerance.getMinWordSizeForTypos().containsKey("oneTypo") && initialTypoTolerance.getMinWordSizeForTypos().get("oneTypo") != null); @@ -575,7 +575,7 @@ public void testUpdateTypoTolerance() throws Exception { assertEquals( newTypoTolerance.getDisableOnAttributes().length, updatedTypoTolerance.getDisableOnAttributes().length); - assertEquals(newTypoTolerance.getEnabled(), updatedTypoTolerance.getEnabled()); + assertEquals(newTypoTolerance.isEnabled(), updatedTypoTolerance.isEnabled()); assertTrue( updatedTypoTolerance.getMinWordSizeForTypos().containsKey("oneTypo") && updatedTypoTolerance.getMinWordSizeForTypos().get("oneTypo") == 7); @@ -615,7 +615,7 @@ public void testResetTypoTolerance() throws Exception { assertEquals( newTypoTolerance.getDisableOnAttributes().length, updatedTypoTolerance.getDisableOnAttributes().length); - assertEquals(newTypoTolerance.getEnabled(), updatedTypoTolerance.getEnabled()); + assertEquals(newTypoTolerance.isEnabled(), updatedTypoTolerance.isEnabled()); assertEquals( initialTypoTolerance.getDisableOnWords().length, @@ -623,7 +623,7 @@ public void testResetTypoTolerance() throws Exception { assertEquals( initialTypoTolerance.getDisableOnAttributes().length, typoToleranceAfterReset.getDisableOnAttributes().length); - assertEquals(initialTypoTolerance.getEnabled(), typoToleranceAfterReset.getEnabled()); + assertEquals(initialTypoTolerance.isEnabled(), typoToleranceAfterReset.isEnabled()); assertTrue( typoToleranceAfterReset.getMinWordSizeForTypos().containsKey("oneTypo") && typoToleranceAfterReset.getMinWordSizeForTypos().get("oneTypo") != null); From 3b089de1dabf1d0fd25d9b32e7b7822555308db4 Mon Sep 17 00:00:00 2001 From: alallema Date: Thu, 5 May 2022 11:07:50 +0200 Subject: [PATCH 06/13] Replace getter setter by lombok --- .../meilisearch/sdk/api/index/Settings.java | 93 +++---------------- 1 file changed, 11 insertions(+), 82 deletions(-) diff --git a/src/main/java/com/meilisearch/sdk/api/index/Settings.java b/src/main/java/com/meilisearch/sdk/api/index/Settings.java index db5a2d86..104eb93e 100644 --- a/src/main/java/com/meilisearch/sdk/api/index/Settings.java +++ b/src/main/java/com/meilisearch/sdk/api/index/Settings.java @@ -3,7 +3,8 @@ import com.meilisearch.sdk.Index; import com.meilisearch.sdk.TypoTolerance; import java.util.HashMap; -import java.util.Map; +import lombok.Getter; +import lombok.Setter; /** * Data Structure for the Settings in an {@link Index} @@ -11,88 +12,16 @@ *

Refer https://docs.meilisearch.com/references/settings.html */ public class Settings { - private HashMap synonyms; - private String[] stopWords; - private String[] rankingRules; - private String[] filterableAttributes; - private String distinctAttribute; - private String[] searchableAttributes; - private String[] displayedAttributes; - private String[] sortableAttributes; - private TypoTolerance typoTolerance; + @Getter @Setter private HashMap 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() {} - - public Map getSynonyms() { - return synonyms; - } - - public void setSynonyms(Map synonyms) { - this.synonyms = new HashMap<>(synonyms); - } - - public String[] getStopWords() { - return stopWords; - } - - public void setStopWords(String[] stopWords) { - this.stopWords = stopWords; - } - - public String[] getRankingRules() { - return rankingRules; - } - - public void setRankingRules(String[] rankingRules) { - this.rankingRules = rankingRules; - } - - public String[] getFilterableAttributes() { - return filterableAttributes; - } - - public void setFilterableAttributes(String[] filterableAttributes) { - this.filterableAttributes = filterableAttributes; - } - - public String getDistinctAttribute() { - return distinctAttribute; - } - - public void setDistinctAttribute(String distinctAttribute) { - this.distinctAttribute = distinctAttribute; - } - - public String[] getSearchableAttributes() { - return searchableAttributes; - } - - public void setSearchableAttributes(String[] searchableAttributes) { - this.searchableAttributes = searchableAttributes; - } - - public String[] getDisplayedAttributes() { - return displayedAttributes; - } - - public void setDisplayedAttributes(String[] displayedAttributes) { - this.displayedAttributes = displayedAttributes; - } - - public String[] getSortableAttributes() { - return sortableAttributes; - } - - public void setSortableAttributes(String[] sortableAttributes) { - this.sortableAttributes = sortableAttributes; - } - - public TypoTolerance getTypoTolerance() { - return typoTolerance; - } - - public void setTypoTolerance(TypoTolerance typoTolerance) { - this.typoTolerance = typoTolerance; - } } From af89442a9d9129dabdde61fc3651774ac1919248 Mon Sep 17 00:00:00 2001 From: alallema Date: Thu, 5 May 2022 16:50:38 +0200 Subject: [PATCH 07/13] Update code sample for the typo tolerance --- .code-samples.meilisearch.yaml | 50 ++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index a43f48e6..2058c2f2 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -192,6 +192,22 @@ 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(); + typoTolerance.setDisableOnAttributes(new String[] {"title"}); + HashMap minWordSizeTypos = + new HashMap() { + { + put("oneTypo", 4); + put("twoTypos", 10); + } + }; + typoTolerance.setMinWordSizeForTypos(minWordSizeTypos); + client.index("movies").updateTypoToleranceSettings(typoTolerance); +reset_typo_tolerance_1: |- + client.index("books").resetTypoToleranceSettings(); get_index_stats_1: |- client.index("movies").getStats(); get_indexes_stats_1: |- @@ -321,6 +337,40 @@ settings_guide_sortable_1: |- "author", }); client.index("books").updateSettings(settings); +settings_guide_typo_tolerance_1: |- + TypoTolerance typoTolerance = new TypoTolerance(); + typoTolerance.setDisableOnAttributes(new String[] {"title"}); + HashMap minWordSizeTypos = + new HashMap() { + { + put("twoTypos", 12); + } + }; + typoTolerance.setMinWordSizeForTypos(minWordSizeTypos); + 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 minWordSizeTypos = + new HashMap() { + { + 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," From 4f6d0413e869ed6d366a0ff7c574d24986ee02f2 Mon Sep 17 00:00:00 2001 From: alallema Date: Tue, 17 May 2022 11:55:37 +0200 Subject: [PATCH 08/13] Fix typo and cast --- src/main/java/com/meilisearch/sdk/SettingsHandler.java | 4 ++-- src/test/java/com/meilisearch/integration/SettingsTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/meilisearch/sdk/SettingsHandler.java b/src/main/java/com/meilisearch/sdk/SettingsHandler.java index 13d492e3..a0041cde 100644 --- a/src/main/java/com/meilisearch/sdk/SettingsHandler.java +++ b/src/main/java/com/meilisearch/sdk/SettingsHandler.java @@ -117,7 +117,7 @@ public Task resetRankingRulesSettings(String uid) throws Exception { * @throws Exception if an error occurs */ public Map getSynonymsSettings(String uid) throws Exception { - return this.gson.>fromJson( + return this.gson.fromJson( meilisearchHttpRequest.get("/indexes/" + uid + "/settings/synonyms"), Map.class); } @@ -400,7 +400,7 @@ public Task resetDistinctAttributeSettings(String uid) throws Exception { * @throws Exception if an error occurs */ public TypoTolerance getTypoToleranceSettings(String uid) throws Exception { - return this.gson.fromJson( + return this.gson.fromJson( meilisearchHttpRequest.get("/indexes/" + uid + "/settings/typo-tolerance"), TypoTolerance.class); } diff --git a/src/test/java/com/meilisearch/integration/SettingsTest.java b/src/test/java/com/meilisearch/integration/SettingsTest.java index 7eb80d2d..f23573f8 100644 --- a/src/test/java/com/meilisearch/integration/SettingsTest.java +++ b/src/test/java/com/meilisearch/integration/SettingsTest.java @@ -107,7 +107,7 @@ public void testUpdateSettingsSort() throws Exception { @Test @DisplayName("Test update settings changing the typo tolerance") public void testUpdateSettingsTypoTolerance() throws Exception { - Index index = createIndex("updateSettingsSynonyms"); + Index index = createIndex("updateSettingsTypoTolerance"); Settings settings = index.getSettings(); TypoTolerance typoTolerance = new TypoTolerance(); From e4a49f139fce3519e8f06e5294e62a80a7329c76 Mon Sep 17 00:00:00 2001 From: alallema Date: Wed, 18 May 2022 14:35:43 +0200 Subject: [PATCH 09/13] Modify assert for clarity --- .../com/meilisearch/integration/SettingsTest.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/meilisearch/integration/SettingsTest.java b/src/test/java/com/meilisearch/integration/SettingsTest.java index f23573f8..1a9f1edd 100644 --- a/src/test/java/com/meilisearch/integration/SettingsTest.java +++ b/src/test/java/com/meilisearch/integration/SettingsTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import com.meilisearch.integration.classes.AbstractIT; @@ -122,7 +123,7 @@ public void testUpdateSettingsTypoTolerance() throws Exception { assertEquals(1, newSettings.getTypoTolerance().getDisableOnWords().length); assertEquals(1, newSettings.getTypoTolerance().getDisableOnAttributes().length); - assertEquals(true, newSettings.getTypoTolerance().isEnabled()); + assertTrue(newSettings.getTypoTolerance().isEnabled()); } @Test @@ -540,12 +541,10 @@ public void testGetTypoTolerance() throws Exception { initialTypoTolerance.getDisableOnAttributes().length); assertEquals( initialSettings.getTypoTolerance().isEnabled(), initialTypoTolerance.isEnabled()); - assertTrue( - initialTypoTolerance.getMinWordSizeForTypos().containsKey("oneTypo") - && initialTypoTolerance.getMinWordSizeForTypos().get("oneTypo") != null); - assertTrue( - initialTypoTolerance.getMinWordSizeForTypos().containsKey("twoTypos") - && initialTypoTolerance.getMinWordSizeForTypos().get("twoTypos") != null); + assertNotNull(initialTypoTolerance.getMinWordSizeForTypos().containsKey("oneTypo")); + assertNotNull(initialTypoTolerance.getMinWordSizeForTypos().get("oneTypo")); + assertNotNull(initialTypoTolerance.getMinWordSizeForTypos().containsKey("twoTypos")); + assertNotNull(initialTypoTolerance.getMinWordSizeForTypos().get("twoTypos")); } @Test From e2dd324b9c707000ca2a37dcd6b2c9aa1ffdb403 Mon Sep 17 00:00:00 2001 From: alallema Date: Wed, 18 May 2022 15:49:42 +0200 Subject: [PATCH 10/13] Rewriting somes tests --- .../meilisearch/integration/SettingsTest.java | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/test/java/com/meilisearch/integration/SettingsTest.java b/src/test/java/com/meilisearch/integration/SettingsTest.java index 1a9f1edd..bfbf7801 100644 --- a/src/test/java/com/meilisearch/integration/SettingsTest.java +++ b/src/test/java/com/meilisearch/integration/SettingsTest.java @@ -533,18 +533,16 @@ public void testGetTypoTolerance() throws Exception { Settings initialSettings = index.getSettings(); TypoTolerance initialTypoTolerance = index.getTypoToleranceSettings(); - assertEquals( - initialSettings.getTypoTolerance().getDisableOnWords().length, - initialTypoTolerance.getDisableOnWords().length); - assertEquals( - initialSettings.getTypoTolerance().getDisableOnAttributes().length, - initialTypoTolerance.getDisableOnAttributes().length); + assertEquals(initialSettings.getTypoTolerance().getDisableOnWords().length, 0); + assertEquals(initialTypoTolerance.getDisableOnWords().length, 0); + assertEquals(initialSettings.getTypoTolerance().getDisableOnAttributes().length, 0); + assertEquals(initialTypoTolerance.getDisableOnAttributes().length, 0); assertEquals( initialSettings.getTypoTolerance().isEnabled(), initialTypoTolerance.isEnabled()); assertNotNull(initialTypoTolerance.getMinWordSizeForTypos().containsKey("oneTypo")); assertNotNull(initialTypoTolerance.getMinWordSizeForTypos().get("oneTypo")); - assertNotNull(initialTypoTolerance.getMinWordSizeForTypos().containsKey("twoTypos")); - assertNotNull(initialTypoTolerance.getMinWordSizeForTypos().get("twoTypos")); + assertNotNull(initialTypoTolerance.getMinWordSizeForTypos().containsKey("twoTypos")); + assertNotNull(initialTypoTolerance.getMinWordSizeForTypos().get("twoTypos")); } @Test @@ -569,11 +567,11 @@ public void testUpdateTypoTolerance() throws Exception { TypoTolerance updatedTypoTolerance = index.getTypoToleranceSettings(); assertEquals( - newTypoTolerance.getDisableOnWords().length, - updatedTypoTolerance.getDisableOnWords().length); + newTypoTolerance.getDisableOnWords()[0], + updatedTypoTolerance.getDisableOnWords()[0]); assertEquals( - newTypoTolerance.getDisableOnAttributes().length, - updatedTypoTolerance.getDisableOnAttributes().length); + newTypoTolerance.getDisableOnAttributes()[0], + updatedTypoTolerance.getDisableOnAttributes()[0]); assertEquals(newTypoTolerance.isEnabled(), updatedTypoTolerance.isEnabled()); assertTrue( updatedTypoTolerance.getMinWordSizeForTypos().containsKey("oneTypo") From af54d14e9afcca9624474bc60bd4520f15f854f3 Mon Sep 17 00:00:00 2001 From: alallema Date: Thu, 19 May 2022 13:36:06 +0200 Subject: [PATCH 11/13] Improve code sample --- .code-samples.meilisearch.yaml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 18f60ee7..28daf671 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -196,7 +196,6 @@ get_typo_tolerance_1: client.index("books").getTypoToleranceSettings(); update_typo_tolerance_1: |- TypoTolerance typoTolerance = new TypoTolerance(); - typoTolerance.setDisableOnAttributes(new String[] {"title"}); HashMap minWordSizeTypos = new HashMap() { { @@ -204,8 +203,11 @@ update_typo_tolerance_1: |- put("twoTypos", 10); } }; + typoTolerance.setMinWordSizeForTypos(minWordSizeTypos); - client.index("movies").updateTypoToleranceSettings(typoTolerance); + typoTolerance.setDisableOnAttributes(new String[] {"title"}); + + client.index("books").updateTypoToleranceSettings(typoTolerance); reset_typo_tolerance_1: |- client.index("books").resetTypoToleranceSettings(); get_index_stats_1: |- @@ -352,14 +354,16 @@ settings_guide_sortable_1: |- client.index("books").updateSettings(settings); settings_guide_typo_tolerance_1: |- TypoTolerance typoTolerance = new TypoTolerance(); - typoTolerance.setDisableOnAttributes(new String[] {"title"}); HashMap minWordSizeTypos = new HashMap() { { put("twoTypos", 12); } }; + typoTolerance.setMinWordSizeForTypos(minWordSizeTypos); + typoTolerance.setDisableOnAttributes(new String[] {"title"}); + client.index("movies").updateTypoToleranceSettings(typoTolerance); typo_tolerance_guide_1: |- TypoTolerance typoTolerance = new TypoTolerance(); From 705e3cfae4efb2ee22bedd771ef712ff65ec3dfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Am=C3=A9lie?= Date: Thu, 19 May 2022 17:46:10 +0200 Subject: [PATCH 12/13] Update src/test/java/com/meilisearch/integration/SettingsTest.java Co-authored-by: Bruno Casali --- src/test/java/com/meilisearch/integration/SettingsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/meilisearch/integration/SettingsTest.java b/src/test/java/com/meilisearch/integration/SettingsTest.java index bfbf7801..e0950b80 100644 --- a/src/test/java/com/meilisearch/integration/SettingsTest.java +++ b/src/test/java/com/meilisearch/integration/SettingsTest.java @@ -572,7 +572,7 @@ public void testUpdateTypoTolerance() throws Exception { assertEquals( newTypoTolerance.getDisableOnAttributes()[0], updatedTypoTolerance.getDisableOnAttributes()[0]); - assertEquals(newTypoTolerance.isEnabled(), updatedTypoTolerance.isEnabled()); + assertTrue(updatedTypoTolerance.isEnabled()); assertTrue( updatedTypoTolerance.getMinWordSizeForTypos().containsKey("oneTypo") && updatedTypoTolerance.getMinWordSizeForTypos().get("oneTypo") == 7); From d7bbdb52ffc470582ed187de690028686899fadd Mon Sep 17 00:00:00 2001 From: alallema Date: Mon, 23 May 2022 15:11:22 +0200 Subject: [PATCH 13/13] Add tests update partial typo tolerance --- .../com/meilisearch/sdk/TypoTolerance.java | 2 +- .../meilisearch/integration/SettingsTest.java | 28 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/meilisearch/sdk/TypoTolerance.java b/src/main/java/com/meilisearch/sdk/TypoTolerance.java index f3abb838..e1415f8a 100644 --- a/src/main/java/com/meilisearch/sdk/TypoTolerance.java +++ b/src/main/java/com/meilisearch/sdk/TypoTolerance.java @@ -6,7 +6,7 @@ import org.json.JSONObject; public class TypoTolerance { - @Getter @Setter private boolean enabled = false; + @Getter @Setter private boolean enabled = true; @Getter @Setter private HashMap minWordSizeForTypos; @Getter @Setter private String[] disableOnWords; @Getter @Setter private String[] disableOnAttributes; diff --git a/src/test/java/com/meilisearch/integration/SettingsTest.java b/src/test/java/com/meilisearch/integration/SettingsTest.java index e0950b80..328da508 100644 --- a/src/test/java/com/meilisearch/integration/SettingsTest.java +++ b/src/test/java/com/meilisearch/integration/SettingsTest.java @@ -112,7 +112,6 @@ public void testUpdateSettingsTypoTolerance() throws Exception { Settings settings = index.getSettings(); TypoTolerance typoTolerance = new TypoTolerance(); - typoTolerance.setEnabled(true); typoTolerance.setDisableOnWords(new String[] {"and"}); typoTolerance.setDisableOnAttributes(new String[] {"title"}); settings.setTypoTolerance(typoTolerance); @@ -581,6 +580,33 @@ public void testUpdateTypoTolerance() throws Exception { && updatedTypoTolerance.getMinWordSizeForTypos().get("twoTypos") == 10); } + @Test + @DisplayName("Test update typo tolerance settings") + public void testPartialUpdateTypoTolerance() throws Exception { + Index index = createIndex("testUpdateTypoTolerance"); + TypoTolerance initialTypoTolerance = index.getTypoToleranceSettings(); + TypoTolerance newTypoTolerance = new TypoTolerance(); + newTypoTolerance.setDisableOnWords(new String[] {"the"}); + newTypoTolerance.setDisableOnAttributes(new String[] {"title"}); + + index.waitForTask(index.updateTypoToleranceSettings(newTypoTolerance).getUid()); + TypoTolerance updatedTypoTolerance = index.getTypoToleranceSettings(); + + assertEquals( + newTypoTolerance.getDisableOnWords()[0], + updatedTypoTolerance.getDisableOnWords()[0]); + assertEquals( + newTypoTolerance.getDisableOnAttributes()[0], + updatedTypoTolerance.getDisableOnAttributes()[0]); + assertTrue(updatedTypoTolerance.isEnabled()); + assertTrue( + updatedTypoTolerance.getMinWordSizeForTypos().containsKey("oneTypo") + && updatedTypoTolerance.getMinWordSizeForTypos().get("oneTypo") == 5); + assertTrue( + updatedTypoTolerance.getMinWordSizeForTypos().containsKey("twoTypos") + && updatedTypoTolerance.getMinWordSizeForTypos().get("twoTypos") == 9); + } + @Test @DisplayName("Test reset typo tolerance settings") public void testResetTypoTolerance() throws Exception {