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 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
7 changes: 7 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,10 @@ search_parameter_reference_ranking_score_threshold_1: |-
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();
46 changes: 46 additions & 0 deletions src/Meilisearch/Index.Dictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.Generic;
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<IEnumerable<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(IEnumerable<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 IEnumerable<string> Dictionary { get; set; }

/// <summary>
/// Gets or sets the proximity precision attribute.
/// </summary>
Expand Down
39 changes: 35 additions & 4 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 @@ -91,6 +92,7 @@ public async Task UpdateSettings()
SearchableAttributes = new string[] { "name", "genre" },
StopWords = new string[] { "of", "the" },
DistinctAttribute = "name",
Dictionary = new string[] { "dictionary" }
};
await AssertUpdateSuccess(_index.UpdateSettingsAsync, newSettings);
await AssertGetInequality(_index.GetSettingsAsync, newSettings); // fields omitted in newSettings shouldn't have changed
Expand All @@ -110,6 +112,7 @@ public async Task TwoStepUpdateSettings()
{ "hp", new string[] { "harry potter" } },
{ "harry potter", new string[] { "hp" } },
},
Dictionary = new string[] { "dictionary" }
};
await AssertUpdateSuccess(_index.UpdateSettingsAsync, newSettingsOne);

Expand Down Expand Up @@ -139,7 +142,8 @@ public async Task ResetSettings()
DistinctAttribute = "name",
DisplayedAttributes = new string[] { "name" },
RankingRules = new string[] { "typo" },
FilterableAttributes = new string[] { "genre" }
FilterableAttributes = new string[] { "genre" },
Dictionary = new string[] { "dictionary" }
};
await AssertUpdateSuccess(_index.UpdateSettingsAsync, newSettings);
await AssertGetInequality(_index.GetSettingsAsync, newSettings); // fields omitted in newSettings shouldn't have changed
Expand Down Expand Up @@ -591,11 +595,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 All @@ -613,7 +643,8 @@ private static Settings SettingsWithDefaultedNullFields(Settings inputSettings,
TypoTolerance = inputSettings.TypoTolerance ?? defaultSettings.TypoTolerance,
Faceting = inputSettings.Faceting ?? defaultSettings.Faceting,
Pagination = inputSettings.Pagination ?? defaultSettings.Pagination,
ProximityPrecision = inputSettings.ProximityPrecision ?? defaultSettings.ProximityPrecision
ProximityPrecision = inputSettings.ProximityPrecision ?? defaultSettings.ProximityPrecision,
Dictionary = inputSettings.Dictionary ?? defaultSettings.Dictionary,
};
}

Expand Down
Loading