diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 2e65aa2d..776dc60c 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -280,6 +280,12 @@ update_faceting_settings_1: |- client.index("books").updateFacetingSettings(newFaceting); reset_faceting_settings_1: |- client.index("books").resetFacetingSettings(); +get_dictionary_1: |- + client.index("books").getDictionarySettings(); +update_dictionary_1: |- + client.index("books").updateDictionarySettings(new String[] {"J. R. R.", "W. E. B."}); +reset_dictionary_1: |- + client.index("books").resetDictionarySettings(); get_index_stats_1: |- client.index("movies").getStats(); get_indexes_stats_1: |- diff --git a/src/main/java/com/meilisearch/sdk/Index.java b/src/main/java/com/meilisearch/sdk/Index.java index c8f94d06..b282f8ea 100644 --- a/src/main/java/com/meilisearch/sdk/Index.java +++ b/src/main/java/com/meilisearch/sdk/Index.java @@ -832,6 +832,43 @@ public TaskInfo resetFacetingSettings() throws MeilisearchException { return this.settingsHandler.resetFacetingSettings(this.uid); } + /** + * Gets the dictionary settings of the index + * + * @return dictionary of a given uid as String + * @throws MeilisearchException if an error occurs + * @see API + * specification + */ + public String[] getDictionarySettings() throws MeilisearchException { + return this.settingsHandler.getDictionarySettings(this.uid); + } + + /** + * Updates the dictionary settings of the index + * + * @param dictionary An array of strings that contains the dictionary. + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + * @see API + * specification + */ + public TaskInfo updateDictionarySettings(String[] dictionary) throws MeilisearchException { + return this.settingsHandler.updateDictionarySettings(this.uid, dictionary); + } + + /** + * Resets the dictionary settings of the index + * + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + * @see API + * specification + */ + public TaskInfo resetDictionarySettings() throws MeilisearchException { + return this.settingsHandler.resetDictionarySettings(this.uid); + } + /** * Gets extended information and metrics about indexes and the Meilisearch database * diff --git a/src/main/java/com/meilisearch/sdk/SettingsHandler.java b/src/main/java/com/meilisearch/sdk/SettingsHandler.java index 0d23a6ae..7ff4825b 100644 --- a/src/main/java/com/meilisearch/sdk/SettingsHandler.java +++ b/src/main/java/com/meilisearch/sdk/SettingsHandler.java @@ -515,6 +515,44 @@ TaskInfo resetFacetingSettings(String uid) throws MeilisearchException { settingsPath(uid).addSubroute("faceting").getURL(), TaskInfo.class); } + /** + * Gets the dictionary settings of the index + * + * @param uid Index identifier + * @return an array of strings that contains the dictionary + * @throws MeilisearchException if an error occurs + */ + String[] getDictionarySettings(String uid) throws MeilisearchException { + return httpClient.get(settingsPath(uid).addSubroute("dictionary").getURL(), String[].class); + } + + /** + * Updates the dictionary settings of the index + * + * @param uid Index identifier + * @param dictionary an array of strings that contains the new dictionary settings + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + */ + TaskInfo updateDictionarySettings(String uid, String[] dictionary) throws MeilisearchException { + return httpClient.put( + settingsPath(uid).addSubroute("dictionary").getURL(), + dictionary == null ? httpClient.jsonHandler.encode(dictionary) : dictionary, + TaskInfo.class); + } + + /** + * Resets the dictionary settings of the index + * + * @param uid Index identifier + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + */ + TaskInfo resetDictionarySettings(String uid) throws MeilisearchException { + return httpClient.delete( + settingsPath(uid).addSubroute("dictionary").getURL(), TaskInfo.class); + } + /** Creates an URLBuilder for the constant route settings */ private URLBuilder settingsPath(String uid) { return new URLBuilder("/indexes").addSubroute(uid).addSubroute("/settings"); diff --git a/src/main/java/com/meilisearch/sdk/model/Settings.java b/src/main/java/com/meilisearch/sdk/model/Settings.java index 4fbd8d95..631362e5 100644 --- a/src/main/java/com/meilisearch/sdk/model/Settings.java +++ b/src/main/java/com/meilisearch/sdk/model/Settings.java @@ -26,6 +26,7 @@ public class Settings { protected TypoTolerance typoTolerance; protected Pagination pagination; protected Faceting faceting; + protected String[] dictionary; public Settings() {} } diff --git a/src/test/java/com/meilisearch/sdk/SettingsHandlerTest.java b/src/test/java/com/meilisearch/sdk/SettingsHandlerTest.java new file mode 100644 index 00000000..56b260fa --- /dev/null +++ b/src/test/java/com/meilisearch/sdk/SettingsHandlerTest.java @@ -0,0 +1,89 @@ +package com.meilisearch.sdk; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.equalTo; + +import com.meilisearch.integration.classes.AbstractIT; +import com.meilisearch.integration.classes.TestData; +import com.meilisearch.sdk.model.Settings; +import com.meilisearch.sdk.model.TaskInfo; +import com.meilisearch.sdk.utils.Movie; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class SettingsHandlerTest extends AbstractIT { + + private TestData testData; + + @BeforeEach + public void initialize() { + setUp(); + setUpJacksonClient(); + if (testData == null) testData = this.getTestData(MOVIES_INDEX, Movie.class); + cleanup(); + } + + @AfterAll + static void cleanMeilisearch() { + cleanup(); + } + + /** Tests of the dictionary setting methods */ + @Test + @DisplayName("Test get dictionary settings by uid") + public void testGetDictionarySettings() throws Exception { + Index index = createIndex("testGetDictionarySettings"); + Settings initialSettings = index.getSettings(); + String[] initialDictionary = index.getDictionarySettings(); + + assertThat(initialDictionary, is(arrayWithSize(initialSettings.getDictionary().length))); + assertThat(initialDictionary, is(equalTo(initialSettings.getDictionary()))); + } + + @Test + @DisplayName("Test update dictionary settings") + public void testUpdateDictionarySettings() throws Exception { + Index index = createIndex("testUpdateDictionarySettings"); + String[] initialDictionary = index.getDictionarySettings(); + String[] newDictionary = {"J. R. R.", "W. E. B."}; + + index.waitForTask(index.updateDictionarySettings(newDictionary).getTaskUid()); + String[] updatedDictionarySettings = index.getDictionarySettings(); + + assertThat(updatedDictionarySettings, is(arrayWithSize(newDictionary.length))); + assertThat(updatedDictionarySettings, is(equalTo(newDictionary))); + assertThat(updatedDictionarySettings, is(not(arrayWithSize(initialDictionary.length)))); + } + + @Test + @DisplayName("Test reset dictionary settings") + public void testResetDictionarySettings() throws Exception { + Index index = createIndex("testResetDictionarySettings"); + String[] initialDictionary = index.getDictionarySettings(); + String[] newDictionary = {"J. R. R.", "W. E. B."}; + + index.waitForTask(index.updateDictionarySettings(newDictionary).getTaskUid()); + String[] updatedDictionarySettings = index.getDictionarySettings(); + + index.waitForTask(index.resetDictionarySettings().getTaskUid()); + String[] dictionaryAfterReset = index.getDictionarySettings(); + + assertThat(updatedDictionarySettings, is(arrayWithSize(newDictionary.length))); + assertThat(updatedDictionarySettings, is(equalTo(newDictionary))); + assertThat(updatedDictionarySettings, is(not(arrayWithSize(initialDictionary.length)))); + assertThat(dictionaryAfterReset, is(not(arrayWithSize(updatedDictionarySettings.length)))); + assertThat(dictionaryAfterReset, is(arrayWithSize(initialDictionary.length))); + assertThat(dictionaryAfterReset, is(equalTo(initialDictionary))); + } + + private Index createIndex(String indexUid) throws Exception { + Index index = client.index(indexUid); + TaskInfo updateInfo = index.addDocuments(testData.getRaw()); + index.waitForTask(updateInfo.getTaskUid()); + + return index; + } +}