Skip to content
This repository was archived by the owner on Jan 31, 2022. It is now read-only.

Commit fc406b8

Browse files
authored
feat(Query): implement sortFacetValuesBy (#450)
* feat(Query): implement sortFacetValuesBy * refactor(Query): sortFacetValuesBy as enum * refactor(Query): sortFacetValuesBy using enum's toString
1 parent 9fae209 commit fc406b8

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

algoliasearch/src/main/java/com/algolia/search/saas/Query.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ public enum AlternativesAsExact {
8282
MULTI_WORDS_SYNONYM
8383
}
8484

85+
public enum SortFacetValuesBy {
86+
ALPHA {
87+
@Override
88+
public String toString() {
89+
return "alpha";
90+
}
91+
},
92+
COUNT {
93+
@Override
94+
public String toString() {
95+
return "count";
96+
}
97+
}
98+
}
99+
85100
// ----------------------------------------------------------------------
86101
// Construction
87102
// ----------------------------------------------------------------------
@@ -1209,9 +1224,10 @@ public String[] getRestrictSearchableAttributes() {
12091224

12101225
/**
12111226
* Set a list of contexts for which rules are enabled.
1212-
*
1227+
* <p>
12131228
* Contextual rules matching any of these contexts are eligible, as well as generic rules.
12141229
* When empty, only generic rules are eligible.
1230+
*
12151231
* @param ruleContexts one or several contexts.
12161232
*/
12171233
public @NonNull Query setRuleContexts(String... ruleContexts) {
@@ -1236,6 +1252,24 @@ public String[] getRestrictSearchableAttributes() {
12361252
return get(KEY_SNIPPET_ELLIPSIS_TEXT);
12371253
}
12381254

1255+
public static final String KEY_SORT_FACET_VALUES_BY = "sortFacetValuesBy";
1256+
1257+
1258+
/**
1259+
* When using {@link #setFacets}, Algolia retrieves a list of matching facet values for each faceted attribute.
1260+
* This parameter controls how the facet values are sorted within each faceted attribute.
1261+
*
1262+
* @param order supported options are `count` (sort by decreasing count) and `alpha` (sort by increasing alphabetical order)
1263+
* @return This instance (used to chain calls).
1264+
*/
1265+
public @NonNull Query setSortFacetValuesBy(SortFacetValuesBy order) {
1266+
return set(KEY_SORT_FACET_VALUES_BY, order.toString());
1267+
}
1268+
1269+
public String getSortFacetValuesBy() {
1270+
return get(KEY_SORT_FACET_VALUES_BY);
1271+
}
1272+
12391273
private static final String KEY_SYNONYMS = "synonyms";
12401274

12411275
/**

algoliasearch/src/test/java/com/algolia/search/saas/QueryTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,21 @@ public void analytics() {
278278
assertEquals(query.getAnalytics(), Query.parse(query.build()).getAnalytics());
279279
}
280280

281+
282+
@Test
283+
public void sortFacetValuesBy() {
284+
Query query = new Query();
285+
assertNull(query.getSortFacetValuesBy());
286+
query.setSortFacetValuesBy(Query.SortFacetValuesBy.COUNT);
287+
assertEquals("count", query.getSortFacetValuesBy());
288+
assertEquals("count", query.get("sortFacetValuesBy"));
289+
query.setSortFacetValuesBy(Query.SortFacetValuesBy.ALPHA);
290+
assertEquals("alpha", query.getSortFacetValuesBy());
291+
assertEquals("alpha", query.get("sortFacetValuesBy"));
292+
Query query2 = Query.parse(query.build());
293+
assertEquals(query.getSortFacetValuesBy(), query2.getSortFacetValuesBy());
294+
}
295+
281296
@Test
282297
public void synonyms() {
283298
Query query = new Query();

0 commit comments

Comments
 (0)