Skip to content

Commit

Permalink
Merge #591
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
3 people authored Jan 8, 2025
2 parents 1dadb40 + 267c833 commit 9ee4f3b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 14 deletions.
36 changes: 25 additions & 11 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,19 +793,22 @@ 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()
{
RankingScoreThreshold = 0.2M
};
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();
facet_search_2: |-
var newFaceting = new Faceting
{
SortFacetValuesBy = new Dictionary<string, SortFacetValuesByType>
{
["genres"] = SortFacetValuesByType.Count
}
};
await client.Index("books").UpdateFacetingAsync(newFaceting);
facet_search_1: |-
var query = new SearchFacetsQuery()
{
Expand Down
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

0 comments on commit 9ee4f3b

Please sign in to comment.