Skip to content

Commit

Permalink
Merge #583
Browse files Browse the repository at this point in the history
583: Add dictionary support r=curquiza a=danFbach

# Pull Request

## Related issue
Fixes #483

## What does this PR do?
- Adds dictionary support
- tests added and passing
- code samples added

## 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?


Co-authored-by: Dan Fehrenbach <[email protected]>
Co-authored-by: Clémentine <[email protected]>
  • Loading branch information
3 people authored Nov 27, 2024
2 parents bd65e68 + 5402ba9 commit de4e10f
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 4 deletions.
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

0 comments on commit de4e10f

Please sign in to comment.