Skip to content

Commit 7473027

Browse files
committed
SortFacetValuesBy support
1 parent 7010b39 commit 7473027

File tree

4 files changed

+95
-8
lines changed

4 files changed

+95
-8
lines changed

.code-samples.meilisearch.yaml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
# You can read more on https://github.com/meilisearch/documentation/tree/master/.vuepress/code-samples
55
---
66
getting_started_faceting: |-
7-
var faceting = new Faceting {
8-
MaxValuesPerFacet = 2
7+
var faceting = new Faceting
8+
{
9+
MaxValuesPerFacet = 2,
10+
SortFacetValuesBy = new Dictionary<string, SortFacetValuesByType>
11+
{
12+
["*"] = SortFacetValuesByType.Count
13+
}
914
};
1015
await client.Index("movies").UpdateFacetingAsync(faceting);
1116
getting_started_pagination: |-
@@ -716,10 +721,16 @@ reset_pagination_settings_1: |-
716721
get_faceting_settings_1: |-
717722
await client.Index("movies").GetFacetingAsync();
718723
update_faceting_settings_1: |-
719-
var faceting = new Faceting {
720-
MaxValuesPerFacet = 2
724+
var faceting = new Faceting
725+
{
726+
MaxValuesPerFacet = 2,
727+
SortFacetValuesBy = new Dictionary<string, SortFacetValuesByType>
728+
{
729+
["*"] = SortFacetValuesByType.Alpha,
730+
["genres"] = SortFacetValuesByType.Count
731+
}
721732
};
722-
await client.Index("movies").UpdateFacetingAsync(faceting);
733+
await client.Index("books").UpdateFacetingAsync(faceting);
723734
reset_faceting_settings_1: |-
724735
await client.Index("movies").ResetFacetingAsync();
725736
multi_search_1: |-
@@ -782,3 +793,12 @@ distinct_attribute_guide_distinct_parameter_1: |-
782793
Distinct = "sku"
783794
};
784795
await client.Index("products").SearchAsync<Product>("white shirt", params);
796+
facet_search_2: |-
797+
var newFaceting = new Faceting
798+
{
799+
SortFacetValuesBy = new Dictionary<string, SortFacetValuesByType>
800+
{
801+
["genres"] = SortFacetValuesByType.Count
802+
}
803+
};
804+
await client.Index("books").UpdateFacetingAsync(newFaceting);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
5+
namespace Meilisearch.Converters
6+
{
7+
public class SortFacetValuesConverter : JsonConverter<SortFacetValuesByType>
8+
{
9+
public override SortFacetValuesByType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
10+
{
11+
if (reader.TokenType == JsonTokenType.String)
12+
{
13+
var enumValue = reader.GetString();
14+
if (Enum.TryParse<SortFacetValuesByType>(enumValue, true, out var sortFacetValues))
15+
{
16+
return sortFacetValues;
17+
}
18+
}
19+
20+
// If we reach here, it means we encountered an unknown value, so we'll use meilisearch default of Alpha
21+
return SortFacetValuesByType.Alpha;
22+
}
23+
24+
public override void Write(Utf8JsonWriter writer, SortFacetValuesByType value, JsonSerializerOptions options)
25+
{
26+
writer.WriteStringValue(value.ToString().ToLower());
27+
}
28+
}
29+
}

src/Meilisearch/Faceting.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
1+
using System.Collections.Generic;
12
using System.Text.Json.Serialization;
23

4+
using Meilisearch.Converters;
5+
36
namespace Meilisearch
47
{
58
/// <summary>
69
/// Faceting configuration.
710
/// </summary>
811
public class Faceting
912
{
13+
/// <summary>
14+
/// Gets or sets maxValuesPerFacet.
15+
/// </summary>
1016
[JsonPropertyName("maxValuesPerFacet")]
1117
public int MaxValuesPerFacet { get; set; }
18+
19+
/// <summary>
20+
/// Gets or sets sortFacetValuesBy.
21+
/// </summary>
22+
[JsonPropertyName("sortFacetValuesBy")]
23+
public Dictionary<string, SortFacetValuesByType> SortFacetValuesBy { get; set; }
24+
}
25+
26+
[JsonConverter(typeof(SortFacetValuesConverter))]
27+
public enum SortFacetValuesByType
28+
{
29+
/// <summary>
30+
/// Sort by alphanumerical value.
31+
/// </summary>
32+
Alpha,
33+
34+
/// <summary>
35+
/// Sort by count value.
36+
/// </summary>
37+
Count
1238
}
1339
}

tests/Meilisearch.Tests/SettingsTests.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ public SettingsTests(TFixture fixture)
5454
},
5555
Faceting = new Faceting
5656
{
57-
MaxValuesPerFacet = 100
57+
MaxValuesPerFacet = 100,
58+
SortFacetValuesBy = new Dictionary<string, SortFacetValuesByType>()
59+
{
60+
["*"] = SortFacetValuesByType.Alpha
61+
}
5862
},
5963
Pagination = new Pagination
6064
{
@@ -513,7 +517,11 @@ public async Task UpdateFaceting()
513517
{
514518
var newFaceting = new Faceting
515519
{
516-
MaxValuesPerFacet = 20
520+
MaxValuesPerFacet = 20,
521+
SortFacetValuesBy = new Dictionary<string, SortFacetValuesByType>
522+
{
523+
["*"] = SortFacetValuesByType.Count
524+
}
517525
};
518526

519527
await AssertUpdateSuccess(_index.UpdateFacetingAsync, newFaceting);
@@ -525,7 +533,11 @@ public async Task ResetFaceting()
525533
{
526534
var newFaceting = new Faceting
527535
{
528-
MaxValuesPerFacet = 30
536+
MaxValuesPerFacet = 30,
537+
SortFacetValuesBy = new Dictionary<string, SortFacetValuesByType>
538+
{
539+
["*"] = SortFacetValuesByType.Count
540+
}
529541
};
530542

531543
await AssertUpdateSuccess(_index.UpdateFacetingAsync, newFaceting);

0 commit comments

Comments
 (0)