Skip to content

Commit

Permalink
Merge #739
Browse files Browse the repository at this point in the history
739: Add sort facets value feature r=curquiza a=the-sinner

# Pull Request

## Related issue
Fixes #634 

## What does this PR do?
- added feature to sort facets value by alphanumerical or count order

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Shalabh Agarwal <[email protected]>
Co-authored-by: Shalabh Agarwal <[email protected]>
  • Loading branch information
3 people authored May 29, 2024
2 parents f0d6f69 + 7c61f5a commit e56846a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,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 @@ -555,6 +559,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 @@ -655,6 +662,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

0 comments on commit e56846a

Please sign in to comment.