-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
3 changed files
with
80 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,57 @@ | ||
const _defaultmaxValuesPerFacet = 100; | ||
import 'package:meilisearch/src/annotations.dart'; | ||
|
||
enum FacetingSortTypes { | ||
alpha('alpha'), | ||
count('count'); | ||
|
||
final String value; | ||
|
||
const FacetingSortTypes(this.value); | ||
} | ||
|
||
class Faceting { | ||
//Define maximum number of value returned for a facet for a **search query**. | ||
//It means that with the default value of `100`, | ||
//it is not possible to have `101` different colors if the `color`` field is defined as a facet at search time. | ||
int maxValuesPerFacet; | ||
/// Define maximum number of value returned for a facet for a **search query**. | ||
/// It means that with the default value of `100`, | ||
/// it is not possible to have `101` different colors if the `color`` field is defined as a facet at search time. | ||
final int? maxValuesPerFacet; | ||
|
||
/// Defines how facet values are sorted. | ||
/// | ||
/// By default, all facets (`*`) are sorted by name, alphanumerically in ascending order (`alpha`). | ||
/// | ||
/// `count` sorts facet values by the number of documents containing a facet value in descending order. | ||
/// | ||
/// example: | ||
/// "*": 'alpha | ||
/// "genres": count | ||
@RequiredMeiliServerVersion('1.3.0') | ||
final Map<String, FacetingSortTypes>? sortFacetValuesBy; | ||
|
||
Faceting({ | ||
this.maxValuesPerFacet = _defaultmaxValuesPerFacet, | ||
const Faceting({ | ||
this.maxValuesPerFacet, | ||
this.sortFacetValuesBy, | ||
}); | ||
|
||
Map<String, dynamic> toMap() { | ||
return { | ||
'maxValuesPerFacet': maxValuesPerFacet, | ||
if (maxValuesPerFacet != null) 'maxValuesPerFacet': maxValuesPerFacet, | ||
if (sortFacetValuesBy != null) | ||
'sortFacetValuesBy': | ||
sortFacetValuesBy?.map((key, value) => MapEntry(key, value.value)), | ||
}; | ||
} | ||
|
||
factory Faceting.fromMap(Map<String, dynamic> map) { | ||
return Faceting( | ||
maxValuesPerFacet: | ||
map['maxValuesPerFacet'] as int? ?? _defaultmaxValuesPerFacet, | ||
maxValuesPerFacet: map['maxValuesPerFacet'] as int?, | ||
sortFacetValuesBy: | ||
(map['sortFacetValuesBy'] as Map<String, dynamic>?)?.map( | ||
(key, value) => MapEntry( | ||
key, | ||
FacetingSortTypes.values | ||
.firstWhere((element) => element.value == value), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters