diff --git a/src/main/java/com/meilisearch/sdk/Index.java b/src/main/java/com/meilisearch/sdk/Index.java index 1e9c3c1a..502dd6e7 100644 --- a/src/main/java/com/meilisearch/sdk/Index.java +++ b/src/main/java/com/meilisearch/sdk/Index.java @@ -17,6 +17,7 @@ public class Index implements Serializable { @Getter protected String primaryKey; @Getter protected String createdAt; @Getter protected Pagination pagination; + @Getter protected Faceting faceting; @Getter @ToString.Exclude protected String updatedAt; @Getter @ToString.Exclude protected Config config; @ToString.Exclude protected Documents documents; @@ -711,6 +712,40 @@ public TaskInfo resetPaginationSettings() throws MeilisearchException { return this.settingsHandler.resetPaginationSettings(this.uid); } + /** + * Gets the faceting field of the index Refer + * https://docs.meilisearch.com/reference/api/settings.html#get-faceting-settings + * + * @return Faceting instance from Index + * @throws MeilisearchException if an error occurs + */ + public Faceting getFacetingSettings() throws MeilisearchException { + return this.settingsHandler.getFacetingSettings(this.uid); + } + + /** + * Updates the faceting field of the index Refer + * https://docs.meilisearch.com/reference/api/settings.html#update-faceting-settings + * + * @param faceting A faceting instance + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + */ + public TaskInfo updateFacetingSettings(Faceting faceting) throws MeilisearchException { + return this.settingsHandler.updateFacetingSettings(this.uid, faceting); + } + + /** + * Resets the faceting field of the index Refer + * https://docs.meilisearch.com/reference/api/settings.html#reset-faceting-settings + * + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + */ + public TaskInfo resetFacetingSettings() throws MeilisearchException { + return this.settingsHandler.resetFacetingSettings(this.uid); + } + /** * Gets extended information and metrics about indexes and the Meilisearch database Refer * https://docs.meilisearch.com/reference/api/stats.html#stats-object diff --git a/src/main/java/com/meilisearch/sdk/SettingsHandler.java b/src/main/java/com/meilisearch/sdk/SettingsHandler.java index d7cc4d55..5de1dc1a 100644 --- a/src/main/java/com/meilisearch/sdk/SettingsHandler.java +++ b/src/main/java/com/meilisearch/sdk/SettingsHandler.java @@ -2,6 +2,7 @@ import com.meilisearch.sdk.exceptions.MeilisearchException; import com.meilisearch.sdk.http.URLBuilder; +import com.meilisearch.sdk.model.Faceting; import com.meilisearch.sdk.model.Pagination; import com.meilisearch.sdk.model.Settings; import com.meilisearch.sdk.model.TaskInfo; @@ -475,6 +476,44 @@ TaskInfo resetPaginationSettings(String uid) throws MeilisearchException { settingsPath(uid).addSubroute("pagination").getURL(), TaskInfo.class); } + /** + * Gets the faceting settings of the index + * + * @param uid Index identifier + * @return a Faceting instance that contains all faceting settings + * @throws MeilisearchException if an error occurs + */ + Faceting getFacetingSettings(String uid) throws MeilisearchException { + return httpClient.get(settingsPath(uid).addSubroute("faceting").getURL(), Faceting.class); + } + + /** + * Updates the pagination settings of the index + * + * @param uid Index identifier + * @param faceting a Faceting instance that contains the new faceting settings + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + */ + TaskInfo updateFacetingSettings(String uid, Faceting faceting) throws MeilisearchException { + return httpClient.patch( + settingsPath(uid).addSubroute("faceting").getURL(), + faceting == null ? httpClient.jsonHandler.encode(faceting) : faceting, + TaskInfo.class); + } + + /** + * Reset the faceting settings of the index + * + * @param uid Index identifier + * @return TaskInfo instance + * @throws MeilisearchException if an error occurs + */ + TaskInfo resetFacetingSettings(String uid) throws MeilisearchException { + return httpClient.delete( + settingsPath(uid).addSubroute("faceting").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 b9ef6156..515984ca 100644 --- a/src/main/java/com/meilisearch/sdk/model/Settings.java +++ b/src/main/java/com/meilisearch/sdk/model/Settings.java @@ -25,6 +25,7 @@ public class Settings { protected String[] sortableAttributes; protected TypoTolerance typoTolerance; protected Pagination pagination; + protected Faceting faceting; public Settings() {} } diff --git a/src/test/java/com/meilisearch/integration/SettingsTest.java b/src/test/java/com/meilisearch/integration/SettingsTest.java index aedadcca..4da727bf 100644 --- a/src/test/java/com/meilisearch/integration/SettingsTest.java +++ b/src/test/java/com/meilisearch/integration/SettingsTest.java @@ -11,6 +11,7 @@ import com.meilisearch.integration.classes.AbstractIT; import com.meilisearch.integration.classes.TestData; import com.meilisearch.sdk.Index; +import com.meilisearch.sdk.model.Faceting; import com.meilisearch.sdk.model.Pagination; import com.meilisearch.sdk.model.Settings; import com.meilisearch.sdk.model.TaskInfo; @@ -950,6 +951,54 @@ public void testResetPaginationSettings() throws Exception { assertEquals(1000, paginationAfterReset.getMaxTotalHits()); } + /** Tests of the faceting setting methods */ + @Test + @DisplayName("Test get faceting settings by uid") + public void testGetFacetingSettings() throws Exception { + Index index = createIndex("testGetFacetingSettings"); + Settings initialSettings = index.getSettings(); + Faceting initialFaceting = index.getFacetingSettings(); + + assertEquals(initialSettings.getFaceting().getMaxValuesPerFacet(), 100); + assertNotNull(initialFaceting.getMaxValuesPerFacet()); + } + + @Test + @DisplayName("Test update faceting settings") + public void testUpdateFacetingSettings() throws Exception { + Index index = createIndex("testUpdateFacetingSettings"); + Faceting newFaceting = new Faceting(); + + Integer MaxValuesPerFacetTypos = 200; + + newFaceting.setMaxValuesPerFacet(MaxValuesPerFacetTypos); + index.waitForTask(index.updateFacetingSettings(newFaceting).getTaskUid()); + Faceting updatedFaceting = index.getFacetingSettings(); + + assertEquals(200, updatedFaceting.getMaxValuesPerFacet()); + } + + @Test + @DisplayName("Test reset faceting settings") + public void testResetFacetingSettings() throws Exception { + Index index = createIndex("testResetFacetingSettings"); + + Faceting initialFaceting = index.getFacetingSettings(); + Faceting newFaceting = new Faceting(); + + Integer MaxValuesPerFacetTypos = 200; + newFaceting.setMaxValuesPerFacet(MaxValuesPerFacetTypos); + index.waitForTask(index.updateFacetingSettings(newFaceting).getTaskUid()); + Faceting updatedFaceting = index.getFacetingSettings(); + + index.waitForTask(index.resetFacetingSettings().getTaskUid()); + Faceting facetingAfterReset = index.getFacetingSettings(); + + assertEquals(100, initialFaceting.getMaxValuesPerFacet()); + assertEquals(200, updatedFaceting.getMaxValuesPerFacet()); + assertEquals(100, facetingAfterReset.getMaxValuesPerFacet()); + } + private Index createIndex(String indexUid) throws Exception { Index index = client.index(indexUid); TaskInfo updateInfo = index.addDocuments(testData.getRaw());