From 7d704b915a748fe8e342791e74c25d0d86df92ee Mon Sep 17 00:00:00 2001 From: Dan Fehrenbach Date: Mon, 13 Jan 2025 10:08:44 -0600 Subject: [PATCH 1/6] initial locales work --- src/Meilisearch/Index.Attributes.cs | 37 +++++++++++++++++++++ src/Meilisearch/LocalizedAttributeLocale.cs | 23 +++++++++++++ src/Meilisearch/SearchQuery.cs | 6 ++++ src/Meilisearch/Settings.cs | 6 ++++ 4 files changed, 72 insertions(+) create mode 100644 src/Meilisearch/LocalizedAttributeLocale.cs diff --git a/src/Meilisearch/Index.Attributes.cs b/src/Meilisearch/Index.Attributes.cs index b808f57b..ee93af85 100644 --- a/src/Meilisearch/Index.Attributes.cs +++ b/src/Meilisearch/Index.Attributes.cs @@ -119,6 +119,43 @@ public async Task ResetFilterableAttributesAsync(CancellationToken can return await httpresponse.Content.ReadFromJsonAsync(cancellationToken: cancellationToken).ConfigureAwait(false); } + /// + /// Gets the localized attributes setting. + /// + /// The cancellation token for this call. + /// Returns the localized attributes setting. + public async Task> GetLocalizedAttributesAsync(CancellationToken cancellationToken = default) + { + return await _http.GetFromJsonAsync>($"indexes/{Uid}/settings/localized-attributes", cancellationToken: cancellationToken) + .ConfigureAwait(false); + } + + /// + /// Updates the localized attributes setting. + /// + /// Collection of localized attributes. + /// The cancellation token for this call. + /// Returns the task info of the asynchronous task. + public async Task UpdateLocalizedAttributesAsync(IEnumerable localizedAttributes, CancellationToken cancellationToken = default) + { + var responseMessage = + await _http.PutAsJsonAsync($"indexes/{Uid}/settings/localized-attributes", localizedAttributes, Constants.JsonSerializerOptionsRemoveNulls, cancellationToken: cancellationToken) + .ConfigureAwait(false); + return await responseMessage.Content.ReadFromJsonAsync(cancellationToken: cancellationToken).ConfigureAwait(false); + } + + /// + /// Resets the filterable attributes setting. + /// + /// The cancellation token for this call. + /// Returns the task info of the asynchronous task. + public async Task ResetLocalizedAttributesAsync(CancellationToken cancellationToken = default) + { + var httpresponse = await _http.DeleteAsync($"indexes/{Uid}/settings/localized-attributes", cancellationToken) + .ConfigureAwait(false); + return await httpresponse.Content.ReadFromJsonAsync(cancellationToken: cancellationToken).ConfigureAwait(false); + } + /// /// Gets the searchable attributes setting. /// diff --git a/src/Meilisearch/LocalizedAttributeLocale.cs b/src/Meilisearch/LocalizedAttributeLocale.cs new file mode 100644 index 00000000..8d1591ca --- /dev/null +++ b/src/Meilisearch/LocalizedAttributeLocale.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using System.Text.Json.Serialization; + +namespace Meilisearch +{ + /// + /// Localized attribute locale. + /// + public class LocalizedAttributeLocale + { + /// + /// Gets or sets the locales. + /// + [JsonPropertyName("locale")] + public IEnumerable Locale { get; set; } + + /// + /// Gets or sets the attribute patterns. + /// + [JsonPropertyName("attributePatterns")] + public IEnumerable AttributePatterns { get; set; } + } +} diff --git a/src/Meilisearch/SearchQuery.cs b/src/Meilisearch/SearchQuery.cs index fcfcbf19..44c1c097 100644 --- a/src/Meilisearch/SearchQuery.cs +++ b/src/Meilisearch/SearchQuery.cs @@ -148,5 +148,11 @@ public class SearchQuery /// [JsonPropertyName("rankingScoreThreshold")] public decimal? RankingScoreThreshold { get; set; } + + /// + /// Gets or sets locales. + /// + [JsonPropertyName("locales")] + public IEnumerable Locales { get; set; } } } diff --git a/src/Meilisearch/Settings.cs b/src/Meilisearch/Settings.cs index 236ed77e..5a3a4106 100644 --- a/src/Meilisearch/Settings.cs +++ b/src/Meilisearch/Settings.cs @@ -62,6 +62,12 @@ public class Settings [JsonPropertyName("filterableAttributes")] public IEnumerable FilterableAttributes { get; set; } + /// + /// Gets or sets the localized attributes. + /// + [JsonPropertyName("localizedAttributes")] + public IEnumerable LocalizedAttributes { get; set; } + /// /// Gets or sets the sortable attributes. /// From 5f6776c2a3c69eecf35c5c21d3202025ad011630 Mon Sep 17 00:00:00 2001 From: Dan Fehrenbach Date: Tue, 14 Jan 2025 12:30:08 -0600 Subject: [PATCH 2/6] correct json property name - wrong in MS core docs, sent seperate PR. --- src/Meilisearch/LocalizedAttributeLocale.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Meilisearch/LocalizedAttributeLocale.cs b/src/Meilisearch/LocalizedAttributeLocale.cs index 8d1591ca..f48b882b 100644 --- a/src/Meilisearch/LocalizedAttributeLocale.cs +++ b/src/Meilisearch/LocalizedAttributeLocale.cs @@ -11,8 +11,8 @@ public class LocalizedAttributeLocale /// /// Gets or sets the locales. /// - [JsonPropertyName("locale")] - public IEnumerable Locale { get; set; } + [JsonPropertyName("locales")] + public IEnumerable Locales { get; set; } /// /// Gets or sets the attribute patterns. From 0aae289cd0fd8a57d113f6c58df1d77bf34fc2b4 Mon Sep 17 00:00:00 2001 From: Dan Fehrenbach Date: Tue, 14 Jan 2025 12:30:24 -0600 Subject: [PATCH 3/6] settings tests. --- tests/Meilisearch.Tests/SettingsTests.cs | 49 +++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/tests/Meilisearch.Tests/SettingsTests.cs b/tests/Meilisearch.Tests/SettingsTests.cs index 504f29a8..ccbbd408 100644 --- a/tests/Meilisearch.Tests/SettingsTests.cs +++ b/tests/Meilisearch.Tests/SettingsTests.cs @@ -98,6 +98,13 @@ public async Task UpdateSettings() DistinctAttribute = "name", Dictionary = new string[] { "dictionary" }, SearchCutoffMs = 1000, + LocalizedAttributes = new LocalizedAttributeLocale[] + { + new LocalizedAttributeLocale() { + Locales = new[] { "eng" }, + AttributePatterns = new[] { "en_*" } + } + } }; await AssertUpdateSuccess(_index.UpdateSettingsAsync, newSettings); await AssertGetInequality(_index.GetSettingsAsync, newSettings); // fields omitted in newSettings shouldn't have changed @@ -233,6 +240,45 @@ public async Task ResetFilterableAttributes() await AssertGetEquality(_index.GetFilterableAttributesAsync, _defaultSettings.FilterableAttributes); } + [Fact] + public async Task GetLocaliztedAttributes() + { + await AssertGetEquality(_index.GetLocalizedAttributesAsync, _defaultSettings.LocalizedAttributes); + } + + [Fact] + public async Task UpdateLocalizedAttributes() + { + var newLocalizedAttributes = new LocalizedAttributeLocale[] + { + new LocalizedAttributeLocale() { + Locales = new[] { "eng" }, + AttributePatterns = new[] { "en_*" } + } + }; + + await AssertUpdateSuccess(_index.UpdateLocalizedAttributesAsync, newLocalizedAttributes); + await AssertGetEquality(_index.GetLocalizedAttributesAsync, newLocalizedAttributes); + } + + [Fact] + public async Task ResetLocalizedAttributes() + { + var newLocalizedAttributes = new LocalizedAttributeLocale[] + { + new LocalizedAttributeLocale() { + Locales = new[] { "eng" }, + AttributePatterns = new[] { "en_*" } + } + }; + + await AssertUpdateSuccess(_index.UpdateLocalizedAttributesAsync, newLocalizedAttributes); + await AssertGetEquality(_index.GetLocalizedAttributesAsync, newLocalizedAttributes); + + await AssertResetSuccess(_index.ResetLocalizedAttributesAsync); + await AssertGetEquality(_index.GetLocalizedAttributesAsync, _defaultSettings.LocalizedAttributes); + } + [Fact] public async Task GetRankingRules() { @@ -683,7 +729,8 @@ private static Settings SettingsWithDefaultedNullFields(Settings inputSettings, Pagination = inputSettings.Pagination ?? defaultSettings.Pagination, ProximityPrecision = inputSettings.ProximityPrecision ?? defaultSettings.ProximityPrecision, Dictionary = inputSettings.Dictionary ?? defaultSettings.Dictionary, - SearchCutoffMs = inputSettings.SearchCutoffMs ?? defaultSettings.SearchCutoffMs + SearchCutoffMs = inputSettings.SearchCutoffMs ?? defaultSettings.SearchCutoffMs, + LocalizedAttributes = inputSettings.LocalizedAttributes ?? defaultSettings.LocalizedAttributes }; } From f8002987025b0359880f57eed9f7685ef6089d8f Mon Sep 17 00:00:00 2001 From: Dan Fehrenbach Date: Tue, 14 Jan 2025 12:30:37 -0600 Subject: [PATCH 4/6] search tests. WIP needs input --- tests/Meilisearch.Tests/SearchTests.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/Meilisearch.Tests/SearchTests.cs b/tests/Meilisearch.Tests/SearchTests.cs index 580ac12e..7be26c71 100644 --- a/tests/Meilisearch.Tests/SearchTests.cs +++ b/tests/Meilisearch.Tests/SearchTests.cs @@ -554,5 +554,14 @@ public async Task CustomSearchWithRankingScoreThreshold() movies.Hits.First().Id.Should().Be("13"); movies.Hits.First().Name.Should().Be("Harry Potter"); } + + [Fact] + public async Task CustomSearchWithLocalizedAttribute() + { + var searchQuery = new SearchQuery { Locales = new[] { "eng" } }; + var movies = await _nestedIndex.SearchAsync("a wizard movie", searchQuery); + + movies.Hits.Count.Should().Be(4); + } } } From c0e50dcf8b724cefd6548ce1c3d14e7ea17ed219 Mon Sep 17 00:00:00 2001 From: Dan Fehrenbach Date: Tue, 14 Jan 2025 13:15:01 -0600 Subject: [PATCH 5/6] add code samples --- .code-samples.meilisearch.yaml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 454efca6..42de14fe 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -843,3 +843,18 @@ facet_search_3: |- FacetQuery = "c" }; await client.Index("books").FacetSearchAsync("genres", query); +search_parameter_reference_locales_1: |- + var searchQuery = new SearchQuery { Locales = new[] { "jpn" } }; + await client.index('INDEX_NAME').SearchAsync('進撃の巨人', searchQuery); +get_localized_attribute_settings_1: |- + await client.index('INDEX_NAME').GetLocalizedAttributesAsync() +update_localized_attribute_settings_1: |- + await client.index('INDEX_NAME').UpdateLocalizedAttributesAsync(new LocalizedAttributeLocale[] + { + new LocalizedAttributeLocale() { + Locales = new[] { "jpn" }, + AttributePatterns = new[] { "*_ja" } + } + }); +reset_localized_attribute_settings_1: |- + await client.index('INDEX_NAME').ResetLocalizedAttributesAsync(); From a48299e6fcf8980c4fd89df81f396e3e8bb92da0 Mon Sep 17 00:00:00 2001 From: Dan Fehrenbach Date: Wed, 22 Jan 2025 15:39:21 -0600 Subject: [PATCH 6/6] whitespace --- .code-samples.meilisearch.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 42de14fe..d25cc2ff 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -843,7 +843,7 @@ facet_search_3: |- FacetQuery = "c" }; await client.Index("books").FacetSearchAsync("genres", query); -search_parameter_reference_locales_1: |- +search_parameter_reference_locales_1: |- var searchQuery = new SearchQuery { Locales = new[] { "jpn" } }; await client.index('INDEX_NAME').SearchAsync('進撃の巨人', searchQuery); get_localized_attribute_settings_1: |-