diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 3bac4a86..7ca0633d 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -283,6 +283,10 @@ get_faceting_settings_1: |- update_faceting_settings_1: |- Faceting newFaceting = new Faceting(); newFaceting.setMaxValuesPerFacet(2); + HashMap facetSortValues = new HashMap<>(); + facetSortValues.put("*", FacetSortValue.ALPHA); + facetSortValues.put("genres", FacetSortValue.COUNT); + newFaceting.setSortFacetValuesBy(facetSortValues); client.index("books").updateFacetingSettings(newFaceting); reset_faceting_settings_1: |- client.index("books").resetFacetingSettings(); @@ -535,6 +539,9 @@ getting_started_configure_settings: |- getting_started_faceting: |- Faceting newFaceting = new Faceting(); newFaceting.setMaxValuesPerFacet(2); + HashMap facetSortValues = new HashMap<>(); + facetSortValues.put("*", FacetSortValue.COUNT); + newFaceting.setSortFacetValuesBy(facetSortValues); client.index("movies").updateFacetingSettings(newFaceting); getting_started_pagination: |- Pagination newPagination = new Pagination(); @@ -635,6 +642,12 @@ reset_sortable_attributes_1: |- facet_search_1: |- FacetSearchRequest fsr = FacetSearchRequest.builder().facetName("genres").facetQuery("fiction").filter(new String[]{"rating > 3"}).build(); client.index("books").facetSearch(fsr); +facet_search_2: |- + Faceting newFaceting = new Faceting(); + HashMap facetSortValues = new HashMap<>(); + facetSortValues.put("genres", FacetSortValue.COUNT); + newFaceting.setSortFacetValuesBy(facetSortValues); + client.index("books").updateFacetingSettings(newFaceting); facet_search_3: |- FacetSearchRequest fsr = FacetSearchRequest.builder().facetName("genres").facetQuery("c").build(); client.index("books").facetSearch(fsr); diff --git a/src/main/java/com/meilisearch/sdk/model/FacetSortValue.java b/src/main/java/com/meilisearch/sdk/model/FacetSortValue.java new file mode 100644 index 00000000..9cbeaa6c --- /dev/null +++ b/src/main/java/com/meilisearch/sdk/model/FacetSortValue.java @@ -0,0 +1,29 @@ +package com.meilisearch.sdk.model; + +import com.fasterxml.jackson.annotation.JsonValue; +import com.google.gson.annotations.SerializedName; + +/** + * Enum for Sorting Facet Values + * + * @see API + * specification + */ +public enum FacetSortValue { + @SerializedName("alpha") + ALPHA("alpha"), + @SerializedName("count") + COUNT("count"); + + public final String facetSortValue; + + FacetSortValue(String facetSortValue) { + this.facetSortValue = facetSortValue; + } + + @JsonValue + @Override + public String toString() { + return this.facetSortValue; + } +} diff --git a/src/main/java/com/meilisearch/sdk/model/Faceting.java b/src/main/java/com/meilisearch/sdk/model/Faceting.java index 2083d59d..d36d2599 100644 --- a/src/main/java/com/meilisearch/sdk/model/Faceting.java +++ b/src/main/java/com/meilisearch/sdk/model/Faceting.java @@ -1,5 +1,6 @@ package com.meilisearch.sdk.model; +import java.util.HashMap; import lombok.Getter; import lombok.Setter; @@ -7,6 +8,7 @@ @Getter public class Faceting { protected int maxValuesPerFacet; + protected HashMap sortFacetValuesBy; public Faceting() {} } diff --git a/src/test/java/com/meilisearch/integration/SettingsTest.java b/src/test/java/com/meilisearch/integration/SettingsTest.java index 35cdb8dd..3e0eed95 100644 --- a/src/test/java/com/meilisearch/integration/SettingsTest.java +++ b/src/test/java/com/meilisearch/integration/SettingsTest.java @@ -8,6 +8,7 @@ import static org.hamcrest.Matchers.emptyArray; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; @@ -15,6 +16,7 @@ import com.meilisearch.integration.classes.AbstractIT; import com.meilisearch.integration.classes.TestData; import com.meilisearch.sdk.Index; +import com.meilisearch.sdk.model.FacetSortValue; import com.meilisearch.sdk.model.Faceting; import com.meilisearch.sdk.model.Pagination; import com.meilisearch.sdk.model.Settings; @@ -1051,6 +1053,11 @@ public void testGetFacetingSettings() throws Exception { assertThat(initialSettings.getFaceting().getMaxValuesPerFacet(), is(equalTo(100))); assertThat(initialFaceting.getMaxValuesPerFacet(), is(notNullValue())); + assertThat(initialFaceting.getSortFacetValuesBy(), is(notNullValue())); + assertThat(initialSettings.getFaceting().getSortFacetValuesBy(), instanceOf(HashMap.class)); + assertThat( + initialSettings.getFaceting().getSortFacetValuesBy().get("*"), + is(FacetSortValue.ALPHA)); } @Test @@ -1060,12 +1067,17 @@ public void testUpdateFacetingSettings() throws Exception { Faceting newFaceting = new Faceting(); int MaxValuesPerFacetTypos = 200; + HashMap facetSortValues = new HashMap<>(); + facetSortValues.put("*", FacetSortValue.COUNT); newFaceting.setMaxValuesPerFacet(MaxValuesPerFacetTypos); + newFaceting.setSortFacetValuesBy(facetSortValues); index.waitForTask(index.updateFacetingSettings(newFaceting).getTaskUid()); Faceting updatedFaceting = index.getFacetingSettings(); assertThat(updatedFaceting.getMaxValuesPerFacet(), is(equalTo(200))); + assertThat(updatedFaceting.getSortFacetValuesBy(), instanceOf(HashMap.class)); + assertThat(updatedFaceting.getSortFacetValuesBy().get("*"), is(FacetSortValue.COUNT)); } @Test @@ -1077,7 +1089,10 @@ public void testResetFacetingSettings() throws Exception { Faceting newFaceting = new Faceting(); int MaxValuesPerFacetTypos = 200; + HashMap facetSortValues = new HashMap<>(); + facetSortValues.put("*", FacetSortValue.COUNT); newFaceting.setMaxValuesPerFacet(MaxValuesPerFacetTypos); + newFaceting.setSortFacetValuesBy(facetSortValues); index.waitForTask(index.updateFacetingSettings(newFaceting).getTaskUid()); Faceting updatedFaceting = index.getFacetingSettings(); @@ -1087,6 +1102,10 @@ public void testResetFacetingSettings() throws Exception { assertThat(initialFaceting.getMaxValuesPerFacet(), is(equalTo(100))); assertThat(updatedFaceting.getMaxValuesPerFacet(), is(equalTo(200))); assertThat(facetingAfterReset.getMaxValuesPerFacet(), is(equalTo(100))); + + assertThat(initialFaceting.getSortFacetValuesBy().get("*"), is(FacetSortValue.ALPHA)); + assertThat(updatedFaceting.getSortFacetValuesBy().get("*"), is(FacetSortValue.COUNT)); + assertThat(facetingAfterReset.getSortFacetValuesBy().get("*"), is(FacetSortValue.ALPHA)); } /** Tests of the proximity precision setting methods */