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

Language settings and search parameter #610

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
15 changes: 15 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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();
37 changes: 37 additions & 0 deletions src/Meilisearch/Index.Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,43 @@ public async Task<TaskInfo> ResetFilterableAttributesAsync(CancellationToken can
return await httpresponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Gets the localized attributes setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the localized attributes setting.</returns>
public async Task<IEnumerable<LocalizedAttributeLocale>> GetLocalizedAttributesAsync(CancellationToken cancellationToken = default)
{
return await _http.GetFromJsonAsync<IEnumerable<LocalizedAttributeLocale>>($"indexes/{Uid}/settings/localized-attributes", cancellationToken: cancellationToken)
.ConfigureAwait(false);
}

/// <summary>
/// Updates the localized attributes setting.
/// </summary>
/// <param name="localizedAttributes">Collection of localized attributes.</param>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> UpdateLocalizedAttributesAsync(IEnumerable<LocalizedAttributeLocale> 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<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Resets the filterable attributes setting.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the task info of the asynchronous task.</returns>
public async Task<TaskInfo> ResetLocalizedAttributesAsync(CancellationToken cancellationToken = default)
{
var httpresponse = await _http.DeleteAsync($"indexes/{Uid}/settings/localized-attributes", cancellationToken)
.ConfigureAwait(false);
return await httpresponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Gets the searchable attributes setting.
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions src/Meilisearch/LocalizedAttributeLocale.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Meilisearch
{
/// <summary>
/// Localized attribute locale.
/// </summary>
public class LocalizedAttributeLocale
{
/// <summary>
/// Gets or sets the locales.
/// </summary>
[JsonPropertyName("locales")]
public IEnumerable<string> Locales { get; set; }

/// <summary>
/// Gets or sets the attribute patterns.
/// </summary>
[JsonPropertyName("attributePatterns")]
public IEnumerable<string> AttributePatterns { get; set; }
}
}
6 changes: 6 additions & 0 deletions src/Meilisearch/SearchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,11 @@ public class SearchQuery
/// </summary>
[JsonPropertyName("rankingScoreThreshold")]
public decimal? RankingScoreThreshold { get; set; }

/// <summary>
/// Gets or sets locales.
/// </summary>
[JsonPropertyName("locales")]
public IEnumerable<string> Locales { get; set; }
}
}
6 changes: 6 additions & 0 deletions src/Meilisearch/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public class Settings
[JsonPropertyName("filterableAttributes")]
public IEnumerable<string> FilterableAttributes { get; set; }

/// <summary>
/// Gets or sets the localized attributes.
/// </summary>
[JsonPropertyName("localizedAttributes")]
public IEnumerable<LocalizedAttributeLocale> LocalizedAttributes { get; set; }

/// <summary>
/// Gets or sets the sortable attributes.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions tests/Meilisearch.Tests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MovieWithInfo>("a wizard movie", searchQuery);

movies.Hits.Count.Should().Be(4);
}
}
}
49 changes: 48 additions & 1 deletion tests/Meilisearch.Tests/SettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,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
Expand Down Expand Up @@ -232,6 +239,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()
{
Expand Down Expand Up @@ -682,7 +728,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
};
}

Expand Down
Loading