Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SortFacetValuesBy support #591

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, SortFacetValuesByType>
{
["*"] = SortFacetValuesByType.Count
}
};
await client.Index("movies").UpdateFacetingAsync(faceting);
getting_started_pagination: |-
Expand Down Expand Up @@ -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<string, SortFacetValuesByType>
{
["*"] = 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: |-
Expand Down Expand Up @@ -782,16 +793,12 @@ distinct_attribute_guide_distinct_parameter_1: |-
Distinct = "sku"
};
await client.Index("products").SearchAsync<Product>("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<string, SortFacetValuesByType>
{
["genres"] = SortFacetValuesByType.Count
}
};
await client.Index("INDEX_NAME").SearchAsync<T>("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);
29 changes: 29 additions & 0 deletions src/Meilisearch/Converters/SortFacetValuesConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Meilisearch.Converters
{
public class SortFacetValuesConverter : JsonConverter<SortFacetValuesByType>
{
public override SortFacetValuesByType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
var enumValue = reader.GetString();
if (Enum.TryParse<SortFacetValuesByType>(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());
}
}
}
26 changes: 26 additions & 0 deletions src/Meilisearch/Faceting.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

using Meilisearch.Converters;

namespace Meilisearch
{
/// <summary>
/// Faceting configuration.
/// </summary>
public class Faceting
{
/// <summary>
/// Gets or sets maxValuesPerFacet.
/// </summary>
[JsonPropertyName("maxValuesPerFacet")]
public int MaxValuesPerFacet { get; set; }

/// <summary>
/// Gets or sets sortFacetValuesBy.
/// </summary>
[JsonPropertyName("sortFacetValuesBy")]
public Dictionary<string, SortFacetValuesByType> SortFacetValuesBy { get; set; }
}

[JsonConverter(typeof(SortFacetValuesConverter))]
public enum SortFacetValuesByType
{
/// <summary>
/// Sort by alphanumerical value.
/// </summary>
Alpha,

/// <summary>
/// Sort by count value.
/// </summary>
Count
}
}
18 changes: 15 additions & 3 deletions tests/Meilisearch.Tests/SettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public SettingsTests(TFixture fixture)
},
Faceting = new Faceting
{
MaxValuesPerFacet = 100
MaxValuesPerFacet = 100,
SortFacetValuesBy = new Dictionary<string, SortFacetValuesByType>()
{
["*"] = SortFacetValuesByType.Alpha
}
},
Pagination = new Pagination
{
Expand Down Expand Up @@ -517,7 +521,11 @@ public async Task UpdateFaceting()
{
var newFaceting = new Faceting
{
MaxValuesPerFacet = 20
MaxValuesPerFacet = 20,
SortFacetValuesBy = new Dictionary<string, SortFacetValuesByType>
{
["*"] = SortFacetValuesByType.Count
}
};

await AssertUpdateSuccess(_index.UpdateFacetingAsync, newFaceting);
Expand All @@ -529,7 +537,11 @@ public async Task ResetFaceting()
{
var newFaceting = new Faceting
{
MaxValuesPerFacet = 30
MaxValuesPerFacet = 30,
SortFacetValuesBy = new Dictionary<string, SortFacetValuesByType>
{
["*"] = SortFacetValuesByType.Count
}
};

await AssertUpdateSuccess(_index.UpdateFacetingAsync, newFaceting);
Expand Down
Loading