From 8a78b19b60edbf6fdce05fa99c673bfe5e72bcb9 Mon Sep 17 00:00:00 2001 From: Shalabh Agarwal Date: Wed, 22 May 2024 21:16:05 +0530 Subject: [PATCH] added sort facets value feature --- .code-samples.meilisearch.yaml | 13 +++++++++ .../meilisearch/sdk/model/FacetSortValue.java | 29 +++++++++++++++++++ .../com/meilisearch/sdk/model/Faceting.java | 2 ++ .../meilisearch/integration/SettingsTest.java | 19 ++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 src/main/java/com/meilisearch/sdk/model/FacetSortValue.java diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 836fbdf6..7c0df834 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(); @@ -532,6 +536,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(); @@ -629,6 +636,12 @@ update_sortable_attributes_1: |- client.index("books").updateSortableAttributesSettings(new String[] {"price", "author"}); reset_sortable_attributes_1: |- client.index("books").resetSortableAttributesSettings(); +facet_search_2: |- + Faceting newFaceting = new Faceting(); + HashMap facetSortValues = new HashMap<>(); + facetSortValues.put("genres", FacetSortValue.COUNT); + newFaceting.setSortFacetValuesBy(facetSortValues); + client.index("books").updateFacetingSettings(newFaceting); search_parameter_guide_sort_1: |- SearchRequest searchRequest = SearchRequest.builder().q("science fiction").sort(new String[] {"price:asc"}).build(); client.index("search_parameter_guide_sort_1").search(searchRequest); 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 */