diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 09cd49a3..28daf671 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -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 minWordSizeTypos = + new HashMap() { + { + 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: |- @@ -334,6 +352,42 @@ settings_guide_sortable_1: |- "author", }); client.index("books").updateSettings(settings); +settings_guide_typo_tolerance_1: |- + TypoTolerance typoTolerance = new TypoTolerance(); + 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(); + 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," diff --git a/src/main/java/com/meilisearch/sdk/Details.java b/src/main/java/com/meilisearch/sdk/Details.java index 841789df..2b855225 100644 --- a/src/main/java/com/meilisearch/sdk/Details.java +++ b/src/main/java/com/meilisearch/sdk/Details.java @@ -22,4 +22,5 @@ public Details() {} protected String[] stopWords; protected Map synonyms; protected String distinctAttribute; + protected TypoTolerance typoTolerance; } diff --git a/src/main/java/com/meilisearch/sdk/Index.java b/src/main/java/com/meilisearch/sdk/Index.java index 7df4f036..c9d9aeba 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/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 * diff --git a/src/main/java/com/meilisearch/sdk/Result.java b/src/main/java/com/meilisearch/sdk/Result.java index f48cf70f..5f44d727 100644 --- a/src/main/java/com/meilisearch/sdk/Result.java +++ b/src/main/java/com/meilisearch/sdk/Result.java @@ -7,7 +7,7 @@ public class Result { @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 @@ -16,6 +16,6 @@ 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 f32bdd2d..1caca136 100644 --- a/src/main/java/com/meilisearch/sdk/Settings.java +++ b/src/main/java/com/meilisearch/sdk/Settings.java @@ -21,6 +21,7 @@ public class Settings { private String[] searchableAttributes; private String[] displayedAttributes; private String[] sortableAttributes; + private TypoTolerance typoTolerance; /** Empty SettingsRequest constructor */ public Settings() {} @@ -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(); } } diff --git a/src/main/java/com/meilisearch/sdk/SettingsHandler.java b/src/main/java/com/meilisearch/sdk/SettingsHandler.java index 8a831f58..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); } @@ -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); + } } 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..e1415f8a --- /dev/null +++ b/src/main/java/com/meilisearch/sdk/TypoTolerance.java @@ -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 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; + } +} 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 2ce06fa5..03314dcb 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 lombok.Getter; import lombok.Setter; @@ -13,14 +14,15 @@ @Getter @Setter 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; + @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() {} diff --git a/src/test/java/com/meilisearch/integration/SettingsTest.java b/src/test/java/com/meilisearch/integration/SettingsTest.java index 831842f8..328da508 100644 --- a/src/test/java/com/meilisearch/integration/SettingsTest.java +++ b/src/test/java/com/meilisearch/integration/SettingsTest.java @@ -5,12 +5,15 @@ 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; 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 +42,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 +105,26 @@ 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("updateSettingsTypoTolerance"); + Settings settings = index.getSettings(); + + TypoTolerance typoTolerance = new TypoTolerance(); + 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); + assertTrue(newSettings.getTypoTolerance().isEnabled()); + } + @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,156 @@ 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, 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")); + } + + @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()[0], + updatedTypoTolerance.getDisableOnWords()[0]); + assertEquals( + newTypoTolerance.getDisableOnAttributes()[0], + updatedTypoTolerance.getDisableOnAttributes()[0]); + assertTrue(updatedTypoTolerance.isEnabled()); + assertTrue( + updatedTypoTolerance.getMinWordSizeForTypos().containsKey("oneTypo") + && updatedTypoTolerance.getMinWordSizeForTypos().get("oneTypo") == 7); + assertTrue( + updatedTypoTolerance.getMinWordSizeForTypos().containsKey("twoTypos") + && updatedTypoTolerance.getMinWordSizeForTypos().get("twoTypos") == 10); + } - assertNotEquals(newRankingRule.length, resetRankingRule.length); - assertEquals(initialRankingRule.length, resetRankingRule.length); - assertArrayEquals(initialRankingRule, resetRankingRule); + @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 { + 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(); + + assertEquals( + newTypoTolerance.getDisableOnWords().length, + updatedTypoTolerance.getDisableOnWords().length); + assertEquals( + newTypoTolerance.getDisableOnAttributes().length, + updatedTypoTolerance.getDisableOnAttributes().length); + assertEquals(newTypoTolerance.isEnabled(), updatedTypoTolerance.isEnabled()); + + assertEquals( + initialTypoTolerance.getDisableOnWords().length, + typoToleranceAfterReset.getDisableOnWords().length); + assertEquals( + initialTypoTolerance.getDisableOnAttributes().length, + typoToleranceAfterReset.getDisableOnAttributes().length); + assertEquals(initialTypoTolerance.isEnabled(), typoToleranceAfterReset.isEnabled()); + 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 +697,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 {