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

Add dictionary support #583

Merged
merged 14 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
7 changes: 7 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -782,3 +782,10 @@ distinct_attribute_guide_distinct_parameter_1: |-
Distinct = "sku"
};
await client.Index("products").SearchAsync<Product>("white shirt", params);
get_dictionary_1: |-
var indexDictionary = await client.Index("products").GetDictionaryAsync();
curquiza marked this conversation as resolved.
Show resolved Hide resolved
update_dictionary_1: |-
var newDictionary = new string[] { "J. R. R.", "W. E. B." };
await client.Index("products").UpdateDictionaryAsync(newDictionary);
curquiza marked this conversation as resolved.
Show resolved Hide resolved
reset_dictionary_1: |-
await client.Index("products").ResetDictionaryAsync();
curquiza marked this conversation as resolved.
Show resolved Hide resolved
45 changes: 45 additions & 0 deletions src/Meilisearch/Index.Dictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Net.Http.Json;
using System.Threading;
using System.Threading.Tasks;

namespace Meilisearch
{
public partial class Index
{
/// <summary>
/// Gets the dictionary of an index.
/// </summary>
/// <param name="cancellationToken">The cancellation token for this call.</param>
/// <returns>Returns the dictionary.</returns>
public async Task<string[]> GetDictionaryAsync(CancellationToken cancellationToken = default)
{
return await _http.GetFromJsonAsync<string[]>($"indexes/{Uid}/settings/dictionary", cancellationToken: cancellationToken)
.ConfigureAwait(false);
}

/// <summary>
/// Updates the dictionary of an index.
/// </summary>
/// <param name="dictionary">Dictionary object.</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> UpdateDictionaryAsync(string[] dictionary, CancellationToken cancellationToken = default)
{
var responseMessage =
await _http.PutAsJsonAsync($"indexes/{Uid}/settings/dictionary", dictionary, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Resets the dictionary to their default values.
/// </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> ResetDictionaryAsync(CancellationToken cancellationToken = default)
{
var httpResponse = await _http.DeleteAsync($"indexes/{Uid}/settings/dictionary", cancellationToken).ConfigureAwait(false);
return await httpResponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
}
3 changes: 3 additions & 0 deletions src/Meilisearch/Meilisearch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<Compile Update="Index.Documents.cs">
<DependentUpon>Index.cs</DependentUpon>
</Compile>
<Compile Update="Index.Dictionary.cs">
<DependentUpon>Index.cs</DependentUpon>
</Compile>
<Compile Update="Index.Tasks.cs">
<DependentUpon>Index.cs</DependentUpon>
</Compile>
Expand Down
6 changes: 6 additions & 0 deletions src/Meilisearch/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public class Settings
[JsonPropertyName("pagination")]
public Pagination Pagination { get; set; }

/// <summary>
/// Gets or sets the dictionary object.
/// </summary>
[JsonPropertyName("dictionary")]
public string[] Dictionary { get; set; }

/// <summary>
/// Gets or sets the proximity precision attribute.
/// </summary>
Expand Down
31 changes: 29 additions & 2 deletions tests/Meilisearch.Tests/SettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public SettingsTests(TFixture fixture)
DistinctAttribute = null,
SearchableAttributes = new string[] { "*" },
DisplayedAttributes = new string[] { "*" },
Dictionary = new string[] { },
StopWords = new string[] { },
SeparatorTokens = new List<string> { },
NonSeparatorTokens = new List<string> { },
Expand Down Expand Up @@ -591,11 +592,37 @@ public async Task ResetProximityPrecision()
await AssertUpdateSuccess(_index.UpdateProximityPrecisionAsync, newPrecision);
await AssertGetEquality(_index.GetProximityPrecisionAsync, newPrecision);

await AssertResetSuccess(_index.ResetProximityPrecisionAsync
);
await AssertResetSuccess(_index.ResetProximityPrecisionAsync);
await AssertGetEquality(_index.GetProximityPrecisionAsync, _defaultSettings.ProximityPrecision);
}

[Fact]
public async Task GetDictionaryAsync()
{
await AssertGetEquality(_index.GetDictionaryAsync, _defaultSettings.Dictionary);
}

[Fact]
public async Task UpdateDictionaryAsync()
{
var newDictionary = new string[] { "W. E. B.", "W.E.B." };

await AssertUpdateSuccess(_index.UpdateDictionaryAsync, newDictionary);
await AssertGetEquality(_index.GetDictionaryAsync, newDictionary);
}

[Fact]
public async Task ResetDictionaryAsync()
{
var newDictionary = new string[] { "W. E. B.", "W.E.B." };

await AssertUpdateSuccess(_index.UpdateDictionaryAsync, newDictionary);
await AssertGetEquality(_index.GetDictionaryAsync, newDictionary);

await AssertResetSuccess(_index.ResetDictionaryAsync);
await AssertGetEquality(_index.GetDictionaryAsync, _defaultSettings.Dictionary);
}

private static Settings SettingsWithDefaultedNullFields(Settings inputSettings, Settings defaultSettings)
{
return new Settings
Expand Down
Loading