Skip to content

Commit 7f67035

Browse files
Merge #351
351: feat(settings): Add sortFacetValuesBy r=brunoocasali a=ahmednfwela # Pull Request ## Related issue Fixes #339 ## What does this PR do? - Add `enum FacetingSortTypes` - Add `Map<String, FacetingSortTypes>? sortFacetValuesBy` to `Faceting` - Updated `.code-samples.meilisearch.yaml` - [BREAKING] change members of `Faceting` to be final, and remove the default values set there. ## 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: Ahmed Fwela <[email protected]>
2 parents a804f2b + 5331151 commit 7f67035

File tree

3 files changed

+80
-15
lines changed

3 files changed

+80
-15
lines changed

.code-samples.meilisearch.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
# the documentation on build
44
# You can read more on https://github.com/meilisearch/documentation/tree/master/.vuepress/code-samples
55
---
6+
facet_search_2: |-
7+
await client.index('books').updateFaceting(Faceting(sortFacetValuesBy: {'genres': 'count'}));
8+
getting_started_faceting: |-
9+
await client.index('books').updateFaceting(Faceting(maxValuesPerFacet: 2, sortFacetValuesBy: {'*': 'count'}));
10+
update_faceting_settings_1: |-
11+
await client.index('books').updateFaceting(Faceting(maxValuesPerFacet: 2, sortFacetValuesBy: {'*': 'alpha', 'genres': 'count'}));
612
search_parameter_guide_attributes_to_search_on_1: |-
713
await client.index('movies').search('adventure', SearchQuery(attributesToSearchOn: ['overview']));
814
get_documents_post_1: |-
@@ -88,7 +94,6 @@ search_parameter_guide_hitsperpage_1: |-
8894
await client.index('movies').search('', SearchQuery(hitsPerPage: 15)) as PaginatedSearchResult;
8995
search_parameter_guide_page_1: |-
9096
await client.index('movies').search('', SearchQuery(page: 2)) as PaginatedSearchResult;
91-
getting_started_faceting: |-
9297
getting_started_pagination: |-
9398
synonyms_guide_1: |-
9499
await client.index('movies').updateSynonyms({
@@ -122,7 +127,7 @@ get_pagination_settings_1: |-
122127
update_pagination_settings_1: |-
123128
reset_pagination_settings_1: |-
124129
get_faceting_settings_1: |-
125-
update_faceting_settings_1: |-
130+
126131
reset_faceting_settings_1: |-
127132
get_one_index_1: |-
128133
await client.getIndex('movies');

lib/src/settings/faceting.dart

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,57 @@
1-
const _defaultmaxValuesPerFacet = 100;
1+
import 'package:meilisearch/src/annotations.dart';
2+
3+
enum FacetingSortTypes {
4+
alpha('alpha'),
5+
count('count');
6+
7+
final String value;
8+
9+
const FacetingSortTypes(this.value);
10+
}
211

312
class Faceting {
4-
//Define maximum number of value returned for a facet for a **search query**.
5-
//It means that with the default value of `100`,
6-
//it is not possible to have `101` different colors if the `color`` field is defined as a facet at search time.
7-
int maxValuesPerFacet;
13+
/// Define maximum number of value returned for a facet for a **search query**.
14+
/// It means that with the default value of `100`,
15+
/// it is not possible to have `101` different colors if the `color`` field is defined as a facet at search time.
16+
final int? maxValuesPerFacet;
17+
18+
/// Defines how facet values are sorted.
19+
///
20+
/// By default, all facets (`*`) are sorted by name, alphanumerically in ascending order (`alpha`).
21+
///
22+
/// `count` sorts facet values by the number of documents containing a facet value in descending order.
23+
///
24+
/// example:
25+
/// "*": 'alpha
26+
/// "genres": count
27+
@RequiredMeiliServerVersion('1.3.0')
28+
final Map<String, FacetingSortTypes>? sortFacetValuesBy;
829

9-
Faceting({
10-
this.maxValuesPerFacet = _defaultmaxValuesPerFacet,
30+
const Faceting({
31+
this.maxValuesPerFacet,
32+
this.sortFacetValuesBy,
1133
});
1234

1335
Map<String, dynamic> toMap() {
1436
return {
15-
'maxValuesPerFacet': maxValuesPerFacet,
37+
if (maxValuesPerFacet != null) 'maxValuesPerFacet': maxValuesPerFacet,
38+
if (sortFacetValuesBy != null)
39+
'sortFacetValuesBy':
40+
sortFacetValuesBy?.map((key, value) => MapEntry(key, value.value)),
1641
};
1742
}
1843

1944
factory Faceting.fromMap(Map<String, dynamic> map) {
2045
return Faceting(
21-
maxValuesPerFacet:
22-
map['maxValuesPerFacet'] as int? ?? _defaultmaxValuesPerFacet,
46+
maxValuesPerFacet: map['maxValuesPerFacet'] as int?,
47+
sortFacetValuesBy:
48+
(map['sortFacetValuesBy'] as Map<String, dynamic>?)?.map(
49+
(key, value) => MapEntry(
50+
key,
51+
FacetingSortTypes.values
52+
.firstWhere((element) => element.value == value),
53+
),
54+
),
2355
);
2456
}
2557
}

test/settings_test.dart

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ void main() {
4545
),
4646
faceting: Faceting(
4747
maxValuesPerFacet: 200,
48+
sortFacetValuesBy: {
49+
'*': FacetingSortTypes.count,
50+
'genres': FacetingSortTypes.alpha,
51+
},
4852
),
4953
),
5054
)
@@ -67,6 +71,15 @@ void main() {
6771
expect(settings.typoTolerance?.disableOnWords, contains('prince'));
6872
expect(settings.typoTolerance?.minWordSizeForTypos?.oneTypo, equals(3));
6973
expect(settings.faceting?.maxValuesPerFacet, equals(200));
74+
expect(
75+
settings.faceting?.sortFacetValuesBy,
76+
allOf(
77+
isNotNull,
78+
isNotEmpty,
79+
containsPair('*', FacetingSortTypes.count),
80+
containsPair('genres', FacetingSortTypes.alpha),
81+
),
82+
);
7083
});
7184

7285
test('Reseting the settings', () async {
@@ -314,9 +327,20 @@ void main() {
314327
});
315328

316329
group('Faceting', () {
330+
const defaultFaceting = Faceting(
331+
maxValuesPerFacet: 100,
332+
sortFacetValuesBy: {
333+
'*': FacetingSortTypes.alpha,
334+
},
335+
);
336+
317337
Future<Faceting> doUpdate() async {
318338
final toUpdate = Faceting(
319339
maxValuesPerFacet: 200,
340+
sortFacetValuesBy: {
341+
'*': FacetingSortTypes.count,
342+
'genres': FacetingSortTypes.alpha,
343+
},
320344
);
321345
var response =
322346
await index.updateFaceting(toUpdate).waitFor(client: client);
@@ -332,7 +356,11 @@ void main() {
332356

333357
expect(
334358
initial.toMap(),
335-
equals(initialFromSettings?.toMap()),
359+
initialFromSettings?.toMap(),
360+
);
361+
expect(
362+
initial.toMap(),
363+
defaultFaceting.toMap(),
336364
);
337365
});
338366

@@ -363,11 +391,11 @@ void main() {
363391
await index.getSettings().then((value) => value.faceting);
364392
expect(
365393
afterReset.toMap(),
366-
equals(Faceting().toMap()),
394+
equals(defaultFaceting.toMap()),
367395
);
368396
expect(
369397
afterResetFromSettings?.toMap(),
370-
equals(Faceting().toMap()),
398+
equals(defaultFaceting.toMap()),
371399
);
372400
});
373401
});

0 commit comments

Comments
 (0)