From 76a9639440b70857f08a934ec7d88308b7f1a0f0 Mon Sep 17 00:00:00 2001 From: Mohamed Emad Date: Sun, 4 Feb 2024 02:05:15 +0200 Subject: [PATCH 1/3] add support for the proximity precision setting --- src/main/java/com/meilisearch/sdk/Index.java | 41 +++++++++++++++++ .../com/meilisearch/sdk/SettingsHandler.java | 45 +++++++++++++++++++ .../com/meilisearch/sdk/model/Settings.java | 1 + 3 files changed, 87 insertions(+) diff --git a/src/main/java/com/meilisearch/sdk/Index.java b/src/main/java/com/meilisearch/sdk/Index.java index b282f8ea..2a81862c 100644 --- a/src/main/java/com/meilisearch/sdk/Index.java +++ b/src/main/java/com/meilisearch/sdk/Index.java @@ -869,6 +869,47 @@ public TaskInfo resetDictionarySettings() throws MeilisearchException { return this.settingsHandler.resetDictionarySettings(this.uid); } + /** + * Gets the proximity precision level of the index + * + * @return proximity precision level of a given uid as String + * @throws MeilisearchException if an error occurs + * @see API + * specification + */ + public String getProximityPrecisionSettings() throws MeilisearchException { + return this.settingsHandler.getProximityPrecisionSettings(this.uid); + } + + /** + * Updates the proximity precision level of the index + * + * @param proximityPrecision A String: the proximity precision level. + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + * @see API + * specification + */ + public TaskInfo updateProximityPrecisionSettings(String proximityPrecision) + throws MeilisearchException { + return this.settingsHandler.updateProximityPrecisionSettings(this.uid, proximityPrecision); + } + + /** + * Resets the proximity precision level of the index + * + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + * @see API + * specification + */ + public TaskInfo resetProximityPrecisionSettings() throws MeilisearchException { + return this.settingsHandler.resetProximityPrecisionSettings(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 7ff4825b..163a1037 100644 --- a/src/main/java/com/meilisearch/sdk/SettingsHandler.java +++ b/src/main/java/com/meilisearch/sdk/SettingsHandler.java @@ -553,6 +553,51 @@ TaskInfo resetDictionarySettings(String uid) throws MeilisearchException { settingsPath(uid).addSubroute("dictionary").getURL(), TaskInfo.class); } + /** + * Gets the proximity precision level of the index + * + * @param uid Index identifier + * @return a string of the proximity precision level + * @throws MeilisearchException if an error occurs + */ + String getProximityPrecisionSettings(String uid) throws MeilisearchException { + String response = + httpClient.get( + settingsPath(uid).addSubroute("proximity-precision").getURL(), + String.class); + return response.substring(1, response.length() - 1); + } + + /** + * Updates the proximity precision level of the index + * + * @param uid Index identifier + * @param proximityPrecision a String that contains the new proximity precision level settings + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + */ + TaskInfo updateProximityPrecisionSettings(String uid, String proximityPrecision) + throws MeilisearchException { + return httpClient.put( + settingsPath(uid).addSubroute("proximity-precision").getURL(), + proximityPrecision == null + ? httpClient.jsonHandler.encode(proximityPrecision) + : "\"" + proximityPrecision + "\"", + TaskInfo.class); + } + + /** + * Resets the proximity precision level of the index + * + * @param uid Index identifier + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + */ + TaskInfo resetProximityPrecisionSettings(String uid) throws MeilisearchException { + return httpClient.delete( + settingsPath(uid).addSubroute("proximity-precision").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 631362e5..8263581c 100644 --- a/src/main/java/com/meilisearch/sdk/model/Settings.java +++ b/src/main/java/com/meilisearch/sdk/model/Settings.java @@ -27,6 +27,7 @@ public class Settings { protected Pagination pagination; protected Faceting faceting; protected String[] dictionary; + protected String proximityPrecision; public Settings() {} } From 7b857e393e194188b178cd129382544b51987d0e Mon Sep 17 00:00:00 2001 From: Mohamed Emad Date: Sun, 4 Feb 2024 04:36:48 +0200 Subject: [PATCH 2/3] Add integration tests --- .../meilisearch/integration/SettingsTest.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/test/java/com/meilisearch/integration/SettingsTest.java b/src/test/java/com/meilisearch/integration/SettingsTest.java index 36325bc3..4f524c24 100644 --- a/src/test/java/com/meilisearch/integration/SettingsTest.java +++ b/src/test/java/com/meilisearch/integration/SettingsTest.java @@ -1089,6 +1089,57 @@ public void testResetFacetingSettings() throws Exception { assertThat(facetingAfterReset.getMaxValuesPerFacet(), is(equalTo(100))); } + /** Tests of the proximity precision setting methods */ + @Test + @DisplayName("Test get proximity precision settings by uid") + public void testGetProximityPrecisionSettings() throws Exception { + Index index = createIndex("testGetProximityPrecisionSettings"); + Settings initialSettings = index.getSettings(); + String initialProximityPrecision = index.getProximityPrecisionSettings(); + + assertThat(initialProximityPrecision, is(equalTo(initialSettings.getProximityPrecision()))); + } + + @Test + @DisplayName("Test update proximity precision settings") + public void testUpdateProximityPrecisionSettings() throws Exception { + Index index = createIndex("testUpdateProximityPrecisionSettings"); + String initialProximityPrecision = index.getProximityPrecisionSettings(); + String newProximityPrecision = "byAttribute"; + + index.waitForTask( + index.updateProximityPrecisionSettings(newProximityPrecision).getTaskUid()); + String updatedProximityPrecision = index.getProximityPrecisionSettings(); + + assertThat(updatedProximityPrecision, is(equalTo(newProximityPrecision))); + assertThat(updatedProximityPrecision, is(not(equalTo(initialProximityPrecision)))); + } + + @Test + @DisplayName("Test reset proximity precision settings") + public void testResetProximityPrecisionSettings() throws Exception { + Index index = createIndex("testResetProximityPrecisionSettings"); + String initialProximityPrecision = index.getProximityPrecisionSettings(); + String newProximityPrecision = "byAttribute"; + + index.waitForTask( + index.updateProximityPrecisionSettings(newProximityPrecision).getTaskUid()); + String updatedProximityPrecision = index.getProximityPrecisionSettings(); + + index.waitForTask(index.resetProximityPrecisionSettings().getTaskUid()); + String proximityPrecisionAfterReset = index.getProximityPrecisionSettings(); + + assertThat(updatedProximityPrecision, is(equalTo(newProximityPrecision))); + assertThat(updatedProximityPrecision, is(not(equalTo(initialProximityPrecision)))); + assertThat(proximityPrecisionAfterReset, is(not(equalTo(updatedProximityPrecision)))); + assertThat( + proximityPrecisionAfterReset, + is(equalTo(initialProximityPrecision))); // Resetting proximity precision + // changes it back to the default + // "byWord" + } + + private Index createIndex(String indexUid) throws Exception { Index index = client.index(indexUid); TaskInfo updateInfo = index.addDocuments(testData.getRaw()); From 3ac9a0ec54eea682f943a10fbb67ecf437bb69b6 Mon Sep 17 00:00:00 2001 From: Mohamed Emad Date: Sun, 4 Feb 2024 04:45:44 +0200 Subject: [PATCH 3/3] Update code samples --- .code-samples.meilisearch.yaml | 6 ++++++ .../java/com/meilisearch/integration/SettingsTest.java | 9 ++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 2de1c29b..96b676bc 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -286,6 +286,12 @@ update_dictionary_1: |- client.index("books").updateDictionarySettings(new String[] {"J. R. R.", "W. E. B."}); reset_dictionary_1: |- client.index("books").resetDictionarySettings(); +get_proximity_precision_settings_1: |- + client.index("books").getProximityPrecisionSettings(); +update_proximity_precision_settings_1: |- + client.index("books").updateProximityPrecisionSettings("byAttribute"); +reset_proximity_precision_settings_1: |- + client.index("books").resetProximityPrecisionSettings(); get_index_stats_1: |- client.index("movies").getStats(); get_indexes_stats_1: |- diff --git a/src/test/java/com/meilisearch/integration/SettingsTest.java b/src/test/java/com/meilisearch/integration/SettingsTest.java index 4f524c24..95c83a3f 100644 --- a/src/test/java/com/meilisearch/integration/SettingsTest.java +++ b/src/test/java/com/meilisearch/integration/SettingsTest.java @@ -1108,7 +1108,7 @@ public void testUpdateProximityPrecisionSettings() throws Exception { String newProximityPrecision = "byAttribute"; index.waitForTask( - index.updateProximityPrecisionSettings(newProximityPrecision).getTaskUid()); + index.updateProximityPrecisionSettings(newProximityPrecision).getTaskUid()); String updatedProximityPrecision = index.getProximityPrecisionSettings(); assertThat(updatedProximityPrecision, is(equalTo(newProximityPrecision))); @@ -1123,7 +1123,7 @@ public void testResetProximityPrecisionSettings() throws Exception { String newProximityPrecision = "byAttribute"; index.waitForTask( - index.updateProximityPrecisionSettings(newProximityPrecision).getTaskUid()); + index.updateProximityPrecisionSettings(newProximityPrecision).getTaskUid()); String updatedProximityPrecision = index.getProximityPrecisionSettings(); index.waitForTask(index.resetProximityPrecisionSettings().getTaskUid()); @@ -1133,13 +1133,12 @@ public void testResetProximityPrecisionSettings() throws Exception { assertThat(updatedProximityPrecision, is(not(equalTo(initialProximityPrecision)))); assertThat(proximityPrecisionAfterReset, is(not(equalTo(updatedProximityPrecision)))); assertThat( - proximityPrecisionAfterReset, - is(equalTo(initialProximityPrecision))); // Resetting proximity precision + proximityPrecisionAfterReset, + is(equalTo(initialProximityPrecision))); // Resetting proximity precision // changes it back to the default // "byWord" } - private Index createIndex(String indexUid) throws Exception { Index index = client.index(indexUid); TaskInfo updateInfo = index.addDocuments(testData.getRaw());