Skip to content

Commit 700e053

Browse files
committed
fix code examples for merge
2 parents 7473027 + 5bde104 commit 700e053

File tree

8 files changed

+134
-8
lines changed

8 files changed

+134
-8
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Collections.Generic;
2+
using System.Net.Http.Json;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
namespace Meilisearch
7+
{
8+
public partial class Index
9+
{
10+
/// <summary>
11+
/// Gets the dictionary of an index.
12+
/// </summary>
13+
/// <param name="cancellationToken">The cancellation token for this call.</param>
14+
/// <returns>Returns the dictionary.</returns>
15+
public async Task<IEnumerable<string>> GetDictionaryAsync(CancellationToken cancellationToken = default)
16+
{
17+
return await _http.GetFromJsonAsync<string[]>($"indexes/{Uid}/settings/dictionary", cancellationToken: cancellationToken)
18+
.ConfigureAwait(false);
19+
}
20+
21+
/// <summary>
22+
/// Updates the dictionary of an index.
23+
/// </summary>
24+
/// <param name="dictionary">Dictionary object.</param>
25+
/// <param name="cancellationToken">The cancellation token for this call.</param>
26+
/// <returns>Returns the task info of the asynchronous task.</returns>
27+
public async Task<TaskInfo> UpdateDictionaryAsync(IEnumerable<string> dictionary, CancellationToken cancellationToken = default)
28+
{
29+
var responseMessage =
30+
await _http.PutAsJsonAsync($"indexes/{Uid}/settings/dictionary", dictionary, cancellationToken: cancellationToken)
31+
.ConfigureAwait(false);
32+
return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
33+
}
34+
35+
/// <summary>
36+
/// Resets the dictionary to their default values.
37+
/// </summary>
38+
/// <param name="cancellationToken">The cancellation token for this call.</param>
39+
/// <returns>Returns the task info of the asynchronous task.</returns>
40+
public async Task<TaskInfo> ResetDictionaryAsync(CancellationToken cancellationToken = default)
41+
{
42+
var httpResponse = await _http.DeleteAsync($"indexes/{Uid}/settings/dictionary", cancellationToken).ConfigureAwait(false);
43+
return await httpResponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
44+
}
45+
}
46+
}

src/Meilisearch/Meilisearch.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<OutputType>Library</OutputType>
66
<PackageId>MeiliSearch</PackageId>
7-
<Version>0.15.3</Version>
7+
<Version>0.15.5</Version>
88
<Description>.NET wrapper for Meilisearch, an open-source search engine</Description>
99
<RepositoryUrl>https://github.com/meilisearch/meilisearch-dotnet</RepositoryUrl>
1010
<PackageTags>meilisearch;dotnet;sdk;search-engine;search;instant-search</PackageTags>
@@ -22,7 +22,7 @@
2222

2323
<ItemGroup>
2424
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.2" />
25-
<PackageReference Include="System.Net.Http.Json" Version="6.0.0" />
25+
<PackageReference Include="System.Net.Http.Json" Version="6.0.2" />
2626
</ItemGroup>
2727

2828
<ItemGroup>
@@ -39,6 +39,9 @@
3939
<Compile Update="Index.Documents.cs">
4040
<DependentUpon>Index.cs</DependentUpon>
4141
</Compile>
42+
<Compile Update="Index.Dictionary.cs">
43+
<DependentUpon>Index.cs</DependentUpon>
44+
</Compile>
4245
<Compile Update="Index.Tasks.cs">
4346
<DependentUpon>Index.cs</DependentUpon>
4447
</Compile>

src/Meilisearch/SearchQuery.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,11 @@ public class SearchQuery
142142
/// </summary>
143143
[JsonPropertyName("distinct")]
144144
public string Distinct { get; set; }
145+
146+
/// <summary>
147+
/// Gets or sets rankingScoreThreshold, a number between 0.0 and 1.0.
148+
/// </summary>
149+
[JsonPropertyName("rankingScoreThreshold")]
150+
public decimal RankingScoreThreshold { get; set; }
145151
}
146152
}

src/Meilisearch/Settings.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ public class Settings
8686
[JsonPropertyName("pagination")]
8787
public Pagination Pagination { get; set; }
8888

89+
/// <summary>
90+
/// Gets or sets the dictionary object.
91+
/// </summary>
92+
[JsonPropertyName("dictionary")]
93+
public IEnumerable<string> Dictionary { get; set; }
94+
8995
/// <summary>
9096
/// Gets or sets the proximity precision attribute.
9197
/// </summary>

tests/Meilisearch.Tests/IndexFixture.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public async Task<Index> SetUpIndexForNestedSearch(string indexUid)
124124

125125
return index;
126126
}
127+
127128
public async Task<Index> SetUpIndexForDistinctProductsSearch(string indexUid)
128129
{
129130
var index = DefaultClient.Index(indexUid);
@@ -156,6 +157,22 @@ public async Task<Index> SetUpIndexForDistinctProductsSearch(string indexUid)
156157
return index;
157158
}
158159

160+
public async Task<Index> SetUpIndexForRankingScoreThreshold(string indexUid)
161+
{
162+
var index = DefaultClient.Index(indexUid);
163+
var movies = await JsonFileReader.ReadAsync<List<MovieWithInfo>>(Datasets.MoviesWithInfoJsonPath);
164+
var task = await index.AddDocumentsAsync(movies);
165+
166+
// Check the documents have been added
167+
var finishedTask = await index.WaitForTaskAsync(task.TaskUid);
168+
if (finishedTask.Status != TaskInfoStatus.Succeeded)
169+
{
170+
throw new Exception("The documents were not added during SetUpIndexForRankingScoreThreshold. Impossible to run the tests.");
171+
}
172+
173+
return index;
174+
}
175+
159176
public async Task DeleteAllIndexes()
160177
{
161178
var indexes = await DefaultClient.GetAllIndexesAsync();

tests/Meilisearch.Tests/Meilisearch.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="FluentAssertions" Version="6.12.1" />
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
10+
<PackageReference Include="FluentAssertions" Version="7.0.0" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
1212
<PackageReference Include="xunit" Version="2.7.0" />
1313
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
1414
</ItemGroup>

tests/Meilisearch.Tests/SearchTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public abstract class SearchTests<TFixture> : IAsyncLifetime where TFixture : In
1414
private Index _indexForFaceting;
1515
private Index _indexWithIntId;
1616
private Index _productIndexForDistinct;
17+
private Index _indexForRankingScoreThreshold;
1718

1819
private readonly TFixture _fixture;
1920

@@ -30,6 +31,7 @@ public async Task InitializeAsync()
3031
_indexWithIntId = await _fixture.SetUpBasicIndexWithIntId("IndexWithIntId-SearchTests");
3132
_nestedIndex = await _fixture.SetUpIndexForNestedSearch("IndexForNestedDocs-SearchTests");
3233
_productIndexForDistinct = await _fixture.SetUpIndexForDistinctProductsSearch("IndexForDistinctProducts-SearchTests");
34+
_indexForRankingScoreThreshold = await _fixture.SetUpIndexForRankingScoreThreshold("IndexForRankingThreshold-SearchTests");
3335
}
3436

3537
public Task DisposeAsync() => Task.CompletedTask;
@@ -527,6 +529,7 @@ public async Task CustomSearchProductsWithoutDistinct()
527529
var products = await _productIndexForDistinct.SearchAsync<Product>("", searchQuery);
528530
products.Hits.Count.Should().Be(14);
529531
}
532+
530533
[Fact]
531534
public async Task CustomSearchProductsWithDistinct()
532535
{
@@ -537,5 +540,19 @@ public async Task CustomSearchProductsWithDistinct()
537540
var products = await _productIndexForDistinct.SearchAsync<Product>("", searchQuery);
538541
products.Hits.Count.Should().Be(6);
539542
}
543+
544+
[Fact]
545+
public async Task CustomSearchWithRankingScoreThreshold()
546+
{
547+
var searchQuery = new SearchQuery { };
548+
var movies = await _indexForRankingScoreThreshold.SearchAsync<MovieWithInfo>("a wizard movie", searchQuery);
549+
movies.Hits.Count.Should().Be(4);
550+
551+
searchQuery.RankingScoreThreshold = 0.5M;
552+
movies = await _indexForRankingScoreThreshold.SearchAsync<MovieWithInfo>("a wizard movie", searchQuery);
553+
movies.Hits.Count.Should().Be(1);
554+
movies.Hits.First().Id.Should().Be("13");
555+
movies.Hits.First().Name.Should().Be("Harry Potter");
556+
}
540557
}
541558
}

tests/Meilisearch.Tests/SettingsTests.cs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public SettingsTests(TFixture fixture)
3434
DistinctAttribute = null,
3535
SearchableAttributes = new string[] { "*" },
3636
DisplayedAttributes = new string[] { "*" },
37+
Dictionary = new string[] { },
3738
StopWords = new string[] { },
3839
SeparatorTokens = new List<string> { },
3940
NonSeparatorTokens = new List<string> { },
@@ -95,6 +96,7 @@ public async Task UpdateSettings()
9596
SearchableAttributes = new string[] { "name", "genre" },
9697
StopWords = new string[] { "of", "the" },
9798
DistinctAttribute = "name",
99+
Dictionary = new string[] { "dictionary" }
98100
};
99101
await AssertUpdateSuccess(_index.UpdateSettingsAsync, newSettings);
100102
await AssertGetInequality(_index.GetSettingsAsync, newSettings); // fields omitted in newSettings shouldn't have changed
@@ -114,6 +116,7 @@ public async Task TwoStepUpdateSettings()
114116
{ "hp", new string[] { "harry potter" } },
115117
{ "harry potter", new string[] { "hp" } },
116118
},
119+
Dictionary = new string[] { "dictionary" }
117120
};
118121
await AssertUpdateSuccess(_index.UpdateSettingsAsync, newSettingsOne);
119122

@@ -143,7 +146,8 @@ public async Task ResetSettings()
143146
DistinctAttribute = "name",
144147
DisplayedAttributes = new string[] { "name" },
145148
RankingRules = new string[] { "typo" },
146-
FilterableAttributes = new string[] { "genre" }
149+
FilterableAttributes = new string[] { "genre" },
150+
Dictionary = new string[] { "dictionary" }
147151
};
148152
await AssertUpdateSuccess(_index.UpdateSettingsAsync, newSettings);
149153
await AssertGetInequality(_index.GetSettingsAsync, newSettings); // fields omitted in newSettings shouldn't have changed
@@ -603,11 +607,37 @@ public async Task ResetProximityPrecision()
603607
await AssertUpdateSuccess(_index.UpdateProximityPrecisionAsync, newPrecision);
604608
await AssertGetEquality(_index.GetProximityPrecisionAsync, newPrecision);
605609

606-
await AssertResetSuccess(_index.ResetProximityPrecisionAsync
607-
);
610+
await AssertResetSuccess(_index.ResetProximityPrecisionAsync);
608611
await AssertGetEquality(_index.GetProximityPrecisionAsync, _defaultSettings.ProximityPrecision);
609612
}
610613

614+
[Fact]
615+
public async Task GetDictionaryAsync()
616+
{
617+
await AssertGetEquality(_index.GetDictionaryAsync, _defaultSettings.Dictionary);
618+
}
619+
620+
[Fact]
621+
public async Task UpdateDictionaryAsync()
622+
{
623+
var newDictionary = new string[] { "W. E. B.", "W.E.B." };
624+
625+
await AssertUpdateSuccess(_index.UpdateDictionaryAsync, newDictionary);
626+
await AssertGetEquality(_index.GetDictionaryAsync, newDictionary);
627+
}
628+
629+
[Fact]
630+
public async Task ResetDictionaryAsync()
631+
{
632+
var newDictionary = new string[] { "W. E. B.", "W.E.B." };
633+
634+
await AssertUpdateSuccess(_index.UpdateDictionaryAsync, newDictionary);
635+
await AssertGetEquality(_index.GetDictionaryAsync, newDictionary);
636+
637+
await AssertResetSuccess(_index.ResetDictionaryAsync);
638+
await AssertGetEquality(_index.GetDictionaryAsync, _defaultSettings.Dictionary);
639+
}
640+
611641
private static Settings SettingsWithDefaultedNullFields(Settings inputSettings, Settings defaultSettings)
612642
{
613643
return new Settings
@@ -625,7 +655,8 @@ private static Settings SettingsWithDefaultedNullFields(Settings inputSettings,
625655
TypoTolerance = inputSettings.TypoTolerance ?? defaultSettings.TypoTolerance,
626656
Faceting = inputSettings.Faceting ?? defaultSettings.Faceting,
627657
Pagination = inputSettings.Pagination ?? defaultSettings.Pagination,
628-
ProximityPrecision = inputSettings.ProximityPrecision ?? defaultSettings.ProximityPrecision
658+
ProximityPrecision = inputSettings.ProximityPrecision ?? defaultSettings.ProximityPrecision,
659+
Dictionary = inputSettings.Dictionary ?? defaultSettings.Dictionary,
629660
};
630661
}
631662

0 commit comments

Comments
 (0)