diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 19471adc..3072295c 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -4,8 +4,13 @@ # You can read more on https://github.com/meilisearch/documentation/tree/master/.vuepress/code-samples --- getting_started_faceting: |- - var faceting = new Faceting { - MaxValuesPerFacet = 2 + var faceting = new Faceting + { + MaxValuesPerFacet = 2, + SortFacetValuesBy = new Dictionary + { + ["*"] = SortFacetValuesByType.Count + } }; await client.Index("movies").UpdateFacetingAsync(faceting); getting_started_pagination: |- @@ -716,10 +721,16 @@ reset_pagination_settings_1: |- get_faceting_settings_1: |- await client.Index("movies").GetFacetingAsync(); update_faceting_settings_1: |- - var faceting = new Faceting { - MaxValuesPerFacet = 2 + var faceting = new Faceting + { + MaxValuesPerFacet = 2, + SortFacetValuesBy = new Dictionary + { + ["*"] = SortFacetValuesByType.Alpha, + ["genres"] = SortFacetValuesByType.Count + } }; - await client.Index("movies").UpdateFacetingAsync(faceting); + await client.Index("books").UpdateFacetingAsync(faceting); reset_faceting_settings_1: |- await client.Index("movies").ResetFacetingAsync(); multi_search_1: |- @@ -782,16 +793,12 @@ distinct_attribute_guide_distinct_parameter_1: |- Distinct = "sku" }; await client.Index("products").SearchAsync("white shirt", params); -search_parameter_reference_ranking_score_threshold_1: |- - var params = new SearchQuery() +facet_search_2: |- + var newFaceting = new Faceting { - RankingScoreThreshold = 0.2M + SortFacetValuesBy = new Dictionary + { + ["genres"] = SortFacetValuesByType.Count + } }; - await client.Index("INDEX_NAME").SearchAsync("badman", params); -get_dictionary_1: |- - var indexDictionary = await client.Index("books").GetDictionaryAsync(); -update_dictionary_1: |- - var newDictionary = new string[] { "J. R. R.", "W. E. B." }; - await client.Index("books").UpdateDictionaryAsync(newDictionary); -reset_dictionary_1: |- - await client.Index("books").ResetDictionaryAsync(); + await client.Index("books").UpdateFacetingAsync(newFaceting); diff --git a/src/Meilisearch/Converters/SortFacetValuesConverter.cs b/src/Meilisearch/Converters/SortFacetValuesConverter.cs new file mode 100644 index 00000000..ddb1d678 --- /dev/null +++ b/src/Meilisearch/Converters/SortFacetValuesConverter.cs @@ -0,0 +1,29 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Meilisearch.Converters +{ + public class SortFacetValuesConverter : JsonConverter + { + public override SortFacetValuesByType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType == JsonTokenType.String) + { + var enumValue = reader.GetString(); + if (Enum.TryParse(enumValue, true, out var sortFacetValues)) + { + return sortFacetValues; + } + } + + // If we reach here, it means we encountered an unknown value, so we'll use meilisearch default of Alpha + return SortFacetValuesByType.Alpha; + } + + public override void Write(Utf8JsonWriter writer, SortFacetValuesByType value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString().ToLower()); + } + } +} diff --git a/src/Meilisearch/Faceting.cs b/src/Meilisearch/Faceting.cs index e6452efb..d860708b 100644 --- a/src/Meilisearch/Faceting.cs +++ b/src/Meilisearch/Faceting.cs @@ -1,5 +1,8 @@ +using System.Collections.Generic; using System.Text.Json.Serialization; +using Meilisearch.Converters; + namespace Meilisearch { /// @@ -7,7 +10,30 @@ namespace Meilisearch /// public class Faceting { + /// + /// Gets or sets maxValuesPerFacet. + /// [JsonPropertyName("maxValuesPerFacet")] public int MaxValuesPerFacet { get; set; } + + /// + /// Gets or sets sortFacetValuesBy. + /// + [JsonPropertyName("sortFacetValuesBy")] + public Dictionary SortFacetValuesBy { get; set; } + } + + [JsonConverter(typeof(SortFacetValuesConverter))] + public enum SortFacetValuesByType + { + /// + /// Sort by alphanumerical value. + /// + Alpha, + + /// + /// Sort by count value. + /// + Count } } diff --git a/tests/Meilisearch.Tests/SettingsTests.cs b/tests/Meilisearch.Tests/SettingsTests.cs index 08c60177..4c7fb224 100644 --- a/tests/Meilisearch.Tests/SettingsTests.cs +++ b/tests/Meilisearch.Tests/SettingsTests.cs @@ -55,7 +55,11 @@ public SettingsTests(TFixture fixture) }, Faceting = new Faceting { - MaxValuesPerFacet = 100 + MaxValuesPerFacet = 100, + SortFacetValuesBy = new Dictionary() + { + ["*"] = SortFacetValuesByType.Alpha + } }, Pagination = new Pagination { @@ -517,7 +521,11 @@ public async Task UpdateFaceting() { var newFaceting = new Faceting { - MaxValuesPerFacet = 20 + MaxValuesPerFacet = 20, + SortFacetValuesBy = new Dictionary + { + ["*"] = SortFacetValuesByType.Count + } }; await AssertUpdateSuccess(_index.UpdateFacetingAsync, newFaceting); @@ -529,7 +537,11 @@ public async Task ResetFaceting() { var newFaceting = new Faceting { - MaxValuesPerFacet = 30 + MaxValuesPerFacet = 30, + SortFacetValuesBy = new Dictionary + { + ["*"] = SortFacetValuesByType.Count + } }; await AssertUpdateSuccess(_index.UpdateFacetingAsync, newFaceting);