Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sort facets value feature #739

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ get_faceting_settings_1: |-
update_faceting_settings_1: |-
Faceting newFaceting = new Faceting();
newFaceting.setMaxValuesPerFacet(2);
HashMap<String, FacetSortValue> 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();
Expand Down Expand Up @@ -535,6 +539,9 @@ getting_started_configure_settings: |-
getting_started_faceting: |-
Faceting newFaceting = new Faceting();
newFaceting.setMaxValuesPerFacet(2);
HashMap<String, FacetSortValue> facetSortValues = new HashMap<>();
facetSortValues.put("*", FacetSortValue.COUNT);
newFaceting.setSortFacetValuesBy(facetSortValues);
client.index("movies").updateFacetingSettings(newFaceting);
getting_started_pagination: |-
Pagination newPagination = new Pagination();
Expand Down Expand Up @@ -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<String, FacetSortValue> 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);
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/FacetSortValue.java
Original file line number Diff line number Diff line change
@@ -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 <a href="https://www.meilisearch.com/docs/reference/api/settings#faceting-object">API
* specification</a>
*/
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;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/Faceting.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.meilisearch.sdk.model;

import java.util.HashMap;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class Faceting {
protected int maxValuesPerFacet;
protected HashMap<String, FacetSortValue> sortFacetValuesBy;

public Faceting() {}
}
19 changes: 19 additions & 0 deletions src/test/java/com/meilisearch/integration/SettingsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
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;

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;
Expand Down Expand Up @@ -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
Expand All @@ -1060,12 +1067,17 @@ public void testUpdateFacetingSettings() throws Exception {
Faceting newFaceting = new Faceting();

int MaxValuesPerFacetTypos = 200;
HashMap<String, FacetSortValue> 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
Expand All @@ -1077,7 +1089,10 @@ public void testResetFacetingSettings() throws Exception {
Faceting newFaceting = new Faceting();

int MaxValuesPerFacetTypos = 200;
HashMap<String, FacetSortValue> facetSortValues = new HashMap<>();
facetSortValues.put("*", FacetSortValue.COUNT);
newFaceting.setMaxValuesPerFacet(MaxValuesPerFacetTypos);
newFaceting.setSortFacetValuesBy(facetSortValues);
index.waitForTask(index.updateFacetingSettings(newFaceting).getTaskUid());
Faceting updatedFaceting = index.getFacetingSettings();

Expand All @@ -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 */
Expand Down
Loading