From 849589382495f1fb98e2e54b5f31c9b742383c7e Mon Sep 17 00:00:00 2001 From: Shalabh Agarwal Date: Wed, 22 May 2024 22:27:51 +0530 Subject: [PATCH] Support text-separator --- .code-samples.meilisearch.yaml | 14 ++ src/main/java/com/meilisearch/sdk/Index.java | 82 +++++++++++ .../com/meilisearch/sdk/SettingsHandler.java | 80 +++++++++++ .../com/meilisearch/sdk/model/Settings.java | 2 + .../meilisearch/integration/SettingsTest.java | 127 ++++++++++++++++++ 5 files changed, 305 insertions(+) diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 836fbdf6..4d736ac2 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -286,6 +286,20 @@ update_faceting_settings_1: |- client.index("books").updateFacetingSettings(newFaceting); reset_faceting_settings_1: |- client.index("books").resetFacetingSettings(); +get_separator_tokens_1: |- + client.index("articles").getSeparatorTokensSettings(); +update_separator_tokens_1: |- + String[] newSeparatorTokens = { "|", "…" }; + client.index("articles").updateSeparatorTokensSettings(newSeparatorTokens); +reset_separator_tokens_1: |- + client.index("articles").resetSeparatorTokensSettings(); +get_non_separator_tokens_1: |- + client.index("articles").getNonSeparatorTokensSettings(); +update_non_separator_tokens_1: |- + String[] newSeparatorTokens = { "@", "#" }; + client.index("articles").updateNonSeparatorTokensSettings(newSeparatorTokens); +reset_non_separator_tokens_1: |- + client.index("articles").resetNonSeparatorTokensSettings(); get_dictionary_1: |- client.index("books").getDictionarySettings(); update_dictionary_1: |- diff --git a/src/main/java/com/meilisearch/sdk/Index.java b/src/main/java/com/meilisearch/sdk/Index.java index cdf415ff..af1fa125 100644 --- a/src/main/java/com/meilisearch/sdk/Index.java +++ b/src/main/java/com/meilisearch/sdk/Index.java @@ -905,6 +905,88 @@ public TaskInfo resetDictionarySettings() throws MeilisearchException { return this.settingsHandler.resetDictionarySettings(this.uid); } + /** + * Gets the separator tokens of the index + * + * @return separator tokens of a given uid as String + * @throws MeilisearchException if an error occurs + * @see API + * specification + */ + public String[] getSeparatorTokensSettings() throws MeilisearchException { + return this.settingsHandler.getSeparatorTokensSettings(this.uid); + } + + /** + * Updates the separator tokens settings of the index + * + * @param separatorTokens An array of strings that contains the separator tokens. + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + * @see API + * specification + */ + public TaskInfo updateSeparatorTokensSettings(String[] separatorTokens) + throws MeilisearchException { + return this.settingsHandler.updateSeparatorTokensSettings(this.uid, separatorTokens); + } + + /** + * Resets the separator tokens settings of the index + * + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + * @see API + * specification + */ + public TaskInfo resetSeparatorTokensSettings() throws MeilisearchException { + return this.settingsHandler.resetSeparatorTokensSettings(this.uid); + } + + /** + * Gets the non-separator tokens of the index + * + * @return non-separator tokens of a given uid as String + * @throws MeilisearchException if an error occurs + * @see API + * specification + */ + public String[] getNonSeparatorTokensSettings() throws MeilisearchException { + return this.settingsHandler.getNonSeparatorTokensSettings(this.uid); + } + + /** + * Updates the non-separator tokens settings of the index + * + * @param separatorTokens An array of strings that contains the non-separator tokens. + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + * @see API + * specification + */ + public TaskInfo updateNonSeparatorTokensSettings(String[] separatorTokens) + throws MeilisearchException { + return this.settingsHandler.updateNonSeparatorTokensSettings(this.uid, separatorTokens); + } + + /** + * Resets the non-separator tokens settings of the index + * + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + * @see API + * specification + */ + public TaskInfo resetNonSeparatorTokensSettings() throws MeilisearchException { + return this.settingsHandler.resetNonSeparatorTokensSettings(this.uid); + } + /** * Gets the proximity precision level of the index * diff --git a/src/main/java/com/meilisearch/sdk/SettingsHandler.java b/src/main/java/com/meilisearch/sdk/SettingsHandler.java index 72a503f5..cc7f9190 100644 --- a/src/main/java/com/meilisearch/sdk/SettingsHandler.java +++ b/src/main/java/com/meilisearch/sdk/SettingsHandler.java @@ -644,4 +644,84 @@ TaskInfo resetSearchCutoffMsSettings(String uid) throws MeilisearchException { return httpClient.delete( settingsPath(uid).addSubroute("search-cutoff-ms").getURL(), TaskInfo.class); } + + /** + * Gets the separator tokens of the index + * + * @param uid Index identifier + * @return separator tokens of a given uid as String + * @throws MeilisearchException if an error occurs + */ + public String[] getSeparatorTokensSettings(String uid) { + return httpClient.get( + settingsPath(uid).addSubroute("separator-tokens").getURL(), String[].class); + } + + /** + * Updates the separator tokens settings of the index + * + * @param uid Index identifier + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + */ + public TaskInfo updateSeparatorTokensSettings(String uid, String[] separatorTokens) { + return httpClient.put( + settingsPath(uid).addSubroute("separator-tokens").getURL(), + separatorTokens == null + ? httpClient.jsonHandler.encode(separatorTokens) + : separatorTokens, + TaskInfo.class); + } + + /** + * Resets the separator tokens settings of the index + * + * @param uid Index identifier + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + */ + public TaskInfo resetSeparatorTokensSettings(String uid) { + return httpClient.delete( + settingsPath(uid).addSubroute("separator-tokens").getURL(), TaskInfo.class); + } + + /** + * Gets the non-separator tokens of the index + * + * @param uid Index identifier + * @return non-separator tokens of a given uid as String + * @throws MeilisearchException if an error occurs + */ + public String[] getNonSeparatorTokensSettings(String uid) { + return httpClient.get( + settingsPath(uid).addSubroute("non-separator-tokens").getURL(), String[].class); + } + + /** + * Updates the non-separator tokens settings of the index + * + * @param uid Index identifier + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + */ + public TaskInfo updateNonSeparatorTokensSettings(String uid, String[] separatorTokens) { + return httpClient.put( + settingsPath(uid).addSubroute("non-separator-tokens").getURL(), + separatorTokens == null + ? httpClient.jsonHandler.encode(separatorTokens) + : separatorTokens, + TaskInfo.class); + } + + /** + * Resets the non-separator tokens settings of the index + * + * @param uid Index identifier + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + */ + public TaskInfo resetNonSeparatorTokensSettings(String uid) { + return httpClient.delete( + settingsPath(uid).addSubroute("non-separator-tokens").getURL(), TaskInfo.class); + } } diff --git a/src/main/java/com/meilisearch/sdk/model/Settings.java b/src/main/java/com/meilisearch/sdk/model/Settings.java index ce4454e4..9f553ee5 100644 --- a/src/main/java/com/meilisearch/sdk/model/Settings.java +++ b/src/main/java/com/meilisearch/sdk/model/Settings.java @@ -29,6 +29,8 @@ public class Settings { protected String[] dictionary; protected String proximityPrecision; protected Integer searchCutoffMs; + protected String[] separatorTokens; + protected String[] nonSeparatorTokens; public Settings() {} } diff --git a/src/test/java/com/meilisearch/integration/SettingsTest.java b/src/test/java/com/meilisearch/integration/SettingsTest.java index 35cdb8dd..301560a5 100644 --- a/src/test/java/com/meilisearch/integration/SettingsTest.java +++ b/src/test/java/com/meilisearch/integration/SettingsTest.java @@ -1194,4 +1194,131 @@ private Index createIndex(String indexUid) throws Exception { return index; } + + /** Tests of the separator tokens setting methods */ + @Test + @DisplayName("Test get separator tokens settings by uid") + public void testGetSeparatorTokensSettings() throws Exception { + Index index = createIndex("testGetSeparatorTokensSettings"); + Settings initialSettings = index.getSettings(); + String[] initialSeparatorTokens = index.getSeparatorTokensSettings(); + + assertThat( + initialSeparatorTokens, + is(arrayWithSize(initialSettings.getSeparatorTokens().length))); + assertThat(initialSeparatorTokens, is(equalTo(initialSettings.getSeparatorTokens()))); + } + + @Test + @DisplayName("Test update separator tokens settings") + public void testUpdateSeparatorTokensSettings() throws Exception { + Index index = createIndex("testUpdateSeparatorTokensSettings"); + String[] initialSeparatorTokens = index.getSeparatorTokensSettings(); + + String[] newSeparatorTokens = {"|", "…"}; + + index.waitForTask(index.updateSeparatorTokensSettings(newSeparatorTokens).getTaskUid()); + String[] updatedSeparatorTokens = index.getSeparatorTokensSettings(); + + Arrays.sort(newSeparatorTokens); + Arrays.sort(updatedSeparatorTokens); + + assertThat(updatedSeparatorTokens, is(arrayWithSize(newSeparatorTokens.length))); + assertThat(updatedSeparatorTokens, is(equalTo(newSeparatorTokens))); + assertThat(updatedSeparatorTokens, is(not(arrayWithSize(initialSeparatorTokens.length)))); + } + + @Test + @DisplayName("Test reset separator tokens settings") + public void testResetSeparatorTokensSettings() throws Exception { + Index index = createIndex("testResetSeparatorTokensSettings"); + String[] initialSeparatorTokens = index.getSeparatorTokensSettings(); + String[] newSeparatorTokens = {"|", "…"}; + + index.waitForTask(index.updateSeparatorTokensSettings(newSeparatorTokens).getTaskUid()); + String[] updatedSeparatorTokens = index.getSeparatorTokensSettings(); + + index.waitForTask(index.resetSeparatorTokensSettings().getTaskUid()); + String[] separatorTokensAfterReset = index.getSeparatorTokensSettings(); + + Arrays.sort(initialSeparatorTokens); + Arrays.sort(newSeparatorTokens); + Arrays.sort(updatedSeparatorTokens); + Arrays.sort(separatorTokensAfterReset); + + assertThat(updatedSeparatorTokens, is(arrayWithSize(newSeparatorTokens.length))); + assertThat(updatedSeparatorTokens, is(equalTo(newSeparatorTokens))); + assertThat(updatedSeparatorTokens, is(not(arrayWithSize(initialSeparatorTokens.length)))); + assertThat( + separatorTokensAfterReset, is(not(arrayWithSize(updatedSeparatorTokens.length)))); + assertThat(separatorTokensAfterReset, is(arrayWithSize(initialSeparatorTokens.length))); + assertThat(separatorTokensAfterReset, is(equalTo(initialSeparatorTokens))); + } + + /** Tests of the non-separator tokens setting methods */ + @Test + @DisplayName("Test get non-separator tokens settings by uid") + public void testGetNonSeparatorTokensSettings() throws Exception { + Index index = createIndex("testGetNonSeparatorTokensSettings"); + Settings initialSettings = index.getSettings(); + String[] initialNonSeparatorTokens = index.getNonSeparatorTokensSettings(); + + assertThat( + initialNonSeparatorTokens, + is(arrayWithSize(initialSettings.getNonSeparatorTokens().length))); + assertThat(initialNonSeparatorTokens, is(equalTo(initialSettings.getNonSeparatorTokens()))); + } + + @Test + @DisplayName("Test update non-separator tokens settings") + public void testUpdateNonSeparatorTokensSettings() throws Exception { + Index index = createIndex("testUpdateNonSeparatorTokensSettings"); + String[] initialNonSeparatorTokens = index.getNonSeparatorTokensSettings(); + String[] newNonSeparatorTokens = {"@", "#"}; + + index.waitForTask( + index.updateNonSeparatorTokensSettings(newNonSeparatorTokens).getTaskUid()); + String[] updatedNonSeparatorTokens = index.getNonSeparatorTokensSettings(); + + Arrays.sort(newNonSeparatorTokens); + Arrays.sort(updatedNonSeparatorTokens); + + assertThat(updatedNonSeparatorTokens, is(arrayWithSize(newNonSeparatorTokens.length))); + assertThat(updatedNonSeparatorTokens, is(equalTo(newNonSeparatorTokens))); + assertThat( + updatedNonSeparatorTokens, + is(not(arrayWithSize(initialNonSeparatorTokens.length)))); + } + + @Test + @DisplayName("Test reset non-separator tokens settings") + public void testResetNonSeparatorTokensSettings() throws Exception { + Index index = createIndex("testResetNonSeparatorTokensSettings"); + String[] initialNonSeparatorTokens = index.getNonSeparatorTokensSettings(); + String[] newNonSeparatorTokens = {"@", "#"}; + + index.waitForTask( + index.updateNonSeparatorTokensSettings(newNonSeparatorTokens).getTaskUid()); + String[] updatedNonSeparatorTokens = index.getNonSeparatorTokensSettings(); + + index.waitForTask(index.resetNonSeparatorTokensSettings().getTaskUid()); + String[] nonSeparatorTokensAfterReset = index.getNonSeparatorTokensSettings(); + + Arrays.sort(initialNonSeparatorTokens); + Arrays.sort(newNonSeparatorTokens); + Arrays.sort(updatedNonSeparatorTokens); + Arrays.sort(nonSeparatorTokensAfterReset); + + assertThat(updatedNonSeparatorTokens, is(arrayWithSize(newNonSeparatorTokens.length))); + assertThat(updatedNonSeparatorTokens, is(equalTo(newNonSeparatorTokens))); + assertThat( + updatedNonSeparatorTokens, + is(not(arrayWithSize(initialNonSeparatorTokens.length)))); + assertThat( + nonSeparatorTokensAfterReset, + is(not(arrayWithSize(updatedNonSeparatorTokens.length)))); + assertThat( + nonSeparatorTokensAfterReset, is(arrayWithSize(initialNonSeparatorTokens.length))); + assertThat(nonSeparatorTokensAfterReset, is(equalTo(initialNonSeparatorTokens))); + } }