Skip to content

Commit 9ee4f3b

Browse files
meili-bors[bot]danFbachcurquiza
authored
Merge #591
591: SortFacetValuesBy support r=curquiza a=danFbach # Pull Request ## Related issue Fixes #460 ## What does this PR do? -implements sort facets feature ## 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? let me know if i missed anything. Co-authored-by: Dan Fehrenbach <[email protected]> Co-authored-by: Clémentine <[email protected]>
2 parents 1dadb40 + 267c833 commit 9ee4f3b

File tree

4 files changed

+95
-14
lines changed

4 files changed

+95
-14
lines changed

.code-samples.meilisearch.yaml

Lines changed: 25 additions & 11 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,19 +793,22 @@ distinct_attribute_guide_distinct_parameter_1: |-
782793
Distinct = "sku"
783794
};
784795
await client.Index("products").SearchAsync<Product>("white shirt", params);
785-
search_parameter_reference_ranking_score_threshold_1: |-
786-
var params = new SearchQuery()
787-
{
788-
RankingScoreThreshold = 0.2M
789-
};
790-
await client.Index("INDEX_NAME").SearchAsync<T>("badman", params);
791796
get_dictionary_1: |-
792797
var indexDictionary = await client.Index("books").GetDictionaryAsync();
793798
update_dictionary_1: |-
794799
var newDictionary = new string[] { "J. R. R.", "W. E. B." };
795800
await client.Index("books").UpdateDictionaryAsync(newDictionary);
796801
reset_dictionary_1: |-
797802
await client.Index("books").ResetDictionaryAsync();
803+
facet_search_2: |-
804+
var newFaceting = new Faceting
805+
{
806+
SortFacetValuesBy = new Dictionary<string, SortFacetValuesByType>
807+
{
808+
["genres"] = SortFacetValuesByType.Count
809+
}
810+
};
811+
await client.Index("books").UpdateFacetingAsync(newFaceting);
798812
facet_search_1: |-
799813
var query = new SearchFacetsQuery()
800814
{
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
@@ -55,7 +55,11 @@ public SettingsTests(TFixture fixture)
5555
},
5656
Faceting = new Faceting
5757
{
58-
MaxValuesPerFacet = 100
58+
MaxValuesPerFacet = 100,
59+
SortFacetValuesBy = new Dictionary<string, SortFacetValuesByType>()
60+
{
61+
["*"] = SortFacetValuesByType.Alpha
62+
}
5963
},
6064
Pagination = new Pagination
6165
{
@@ -517,7 +521,11 @@ public async Task UpdateFaceting()
517521
{
518522
var newFaceting = new Faceting
519523
{
520-
MaxValuesPerFacet = 20
524+
MaxValuesPerFacet = 20,
525+
SortFacetValuesBy = new Dictionary<string, SortFacetValuesByType>
526+
{
527+
["*"] = SortFacetValuesByType.Count
528+
}
521529
};
522530

523531
await AssertUpdateSuccess(_index.UpdateFacetingAsync, newFaceting);
@@ -529,7 +537,11 @@ public async Task ResetFaceting()
529537
{
530538
var newFaceting = new Faceting
531539
{
532-
MaxValuesPerFacet = 30
540+
MaxValuesPerFacet = 30,
541+
SortFacetValuesBy = new Dictionary<string, SortFacetValuesByType>
542+
{
543+
["*"] = SortFacetValuesByType.Count
544+
}
533545
};
534546

535547
await AssertUpdateSuccess(_index.UpdateFacetingAsync, newFaceting);

0 commit comments

Comments
 (0)