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 Typo Tolerance settings #263

Merged
merged 4 commits into from
May 18, 2022
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
50 changes: 50 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,53 @@ tenant_token_guide_generate_sdk_1: |-
tenant_token_guide_search_sdk_1: |-
frontEndClient = new MeilisearchClient("http://127.0.0.1:7700", token);
SearchResult<Patient> searchResult = await frontEndClient.Index("patient_medical_records").SearchAsync<Patient>("blood test");
get_typo_tolerance_1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UpdateSetting method doesn't exist in dotnet right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't understand your question, but I realized that I missed the suffix "Async" :)

await client.Index("books").GetTypoToleranceAsync();
update_typo_tolerance_1: |-
var typoTolerance = new TypoTolerance {
DisableOnAttributes = new string[] { "title" },
MinWordSizeTypos = new TypoTolerance.TypoSize {
OneTypo = 4,
TwoTypos = 10
}
};

await client.Index("books").UpdateTypoToleranceAsync(typoTolerance);
reset_typo_tolerance_1: |-
await client.Index("books").ResetTypoToleranceAsync();
typo_tolerance_guide_1: |-
var typoTolerance = new TypoTolerance {
Enabled = false
};

await client.Index("mvoies").UpdateTypoToleranceAsync(typoTolerance);
typo_tolerance_guide_2: |-
var typoTolerance = new TypoTolerance {
DisableOnAttributes = new string[] { "title" }
};

await client.Index("mvoies").UpdateTypoToleranceAsync(typoTolerance);
typo_tolerance_guide_3: |-
var typoTolerance = new TypoTolerance {
DisableOnWords = new string[] { "shrek" }
};

await client.Index("mvoies").UpdateTypoToleranceAsync(typoTolerance);
typo_tolerance_guide_4: |-
var typoTolerance = new TypoTolerance {
MinWordSizeTypos = new TypoTolerance.TypoSize {
OneTypo = 4,
TwoTypos = 10
}
};

await client.Index("mvoies").UpdateTypoToleranceAsync(typoTolerance);
settings_guide_typo_tolerance_1: |-
var typoTolerance = new TypoTolerance {
DisableOnAttributes = new string[] { "title" },
MinWordSizeTypos = new TypoTolerance.TypoSize {
TwoTypos = 12
}
};

await client.Index("mvoies").UpdateTypoToleranceAsync(typoTolerance);
40 changes: 40 additions & 0 deletions src/Meilisearch/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,46 @@ public async Task<TaskInfo> ResetSynonymsAsync(CancellationToken cancellationTok
return await httpresponse.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}

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

/// <summary>
/// Updates the typo tolerance setting.
/// </summary>
/// <param name="typoTolerance">TypoTolerance instance</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> UpdateTypoToleranceAsync(TypoTolerance typoTolerance, CancellationToken cancellationToken = default)
{
var responseMessage =
await _http.PostAsJsonAsync($"indexes/{Uid}/settings/typo-tolerance", typoTolerance, Constants.JsonSerializerOptionsRemoveNulls, cancellationToken: cancellationToken)
.ConfigureAwait(false);

return await responseMessage.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken)
.ConfigureAwait(false);
}

/// <summary>
/// Resets the typo tolerance 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> ResetTypoToleranceAsync(CancellationToken cancellationToken = default)
{
var response = await _http.DeleteAsync($"indexes/{Uid}/settings/typo-tolerance", cancellationToken)
.ConfigureAwait(false);

return await response.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Get stats.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Meilisearch/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,11 @@ public class Settings
/// </summary>
[JsonPropertyName("sortableAttributes")]
public IEnumerable<string> SortableAttributes { get; set; }

/// <summary>
/// Gets or sets the typo tolerance attributes.
/// </summary>
[JsonPropertyName("typoTolerance")]
public TypoTolerance TypoTolerance { get; set; }
}
}
50 changes: 50 additions & 0 deletions src/Meilisearch/TypoTolerance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Meilisearch
{
/// <summary>
/// Typo Tolerance configuration.
/// </summary>
public class TypoTolerance
{
/// <summary>
/// Whether the typo tolerance feature is enabled
/// </summary>
[JsonPropertyName("enabled")]
public bool Enabled { get; set; }

/// <summary>
/// Disable the typo tolerance feature on the specified document attributes.
/// </summary>
[JsonPropertyName("disableOnAttributes")]
public IEnumerable<string> DisableOnAttributes { get; set; }

/// <summary>
/// Disable the typo tolerance feature for a given set of terms in a search query.
/// </summary>
[JsonPropertyName("disableOnWords")]
public IEnumerable<string> DisableOnWords { get; set; }

/// <summary>
/// Customize the minimum word size to tolerate typos.
/// </summary>
[JsonPropertyName("minWordSizeForTypos")]
public TypoSize MinWordSizeForTypos { get; set; }

public class TypoSize
{
/// <summary>
/// Customize the minimum word size to tolerate 1 typo.
/// </summary>
[JsonPropertyName("oneTypo")]
public int? OneTypo { get; set; }

/// <summary>
/// Customize the minimum word size to tolerate 2 typos.
/// </summary>
[JsonPropertyName("twoTypos")]
public int? TwoTypos { get; set; }
}
}
}
106 changes: 105 additions & 1 deletion tests/Meilisearch.Tests/SettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ public SettingsTests(TFixture fixture)
Synonyms = new Dictionary<string, IEnumerable<string>> { },
FilterableAttributes = new string[] { },
SortableAttributes = new string[] { },
TypoTolerance = new TypoTolerance
{
Enabled = true,
DisableOnAttributes = new string[] { },
DisableOnWords = new string[] { },
MinWordSizeForTypos = new TypoTolerance.TypoSize
{
OneTypo = 5,
TwoTypos = 9
}
}
};
}

Expand Down Expand Up @@ -117,7 +128,7 @@ public async Task ResetSettings()
DistinctAttribute = "name",
DisplayedAttributes = new string[] { "name" },
RankingRules = new string[] { "typo" },
FilterableAttributes = new string[] { "genre" },
FilterableAttributes = new string[] { "genre" }
};
await AssertUpdateSuccess(_index.UpdateSettingsAsync, newSettings);
await AssertGetInequality(_index.GetSettingsAsync, newSettings); // fields omitted in newSettings shouldn't have changed
Expand Down Expand Up @@ -335,6 +346,98 @@ public async Task ResetSynonyms()
await AssertGetEquality(_index.GetSynonymsAsync, _defaultSettings.Synonyms);
}

[Fact]
public async Task GetTypoTolerance()
{
await AssertGetEquality(_index.GetTypoToleranceAsync, _defaultSettings.TypoTolerance);
}

[Fact]
public async Task UpdateTypoTolerance()
{
var newTypoTolerance = new TypoTolerance
{
DisableOnWords = new string[] { "harry", "potter" },
MinWordSizeForTypos = new TypoTolerance.TypoSize
{
TwoTypos = 12
}
};

var returnedTypoTolerance = new TypoTolerance
{
DisableOnAttributes = new string[] { },
DisableOnWords = new string[] { "harry", "potter" },
MinWordSizeForTypos = new TypoTolerance.TypoSize
{
TwoTypos = 12,
OneTypo = 5
}
};

await AssertUpdateSuccess(_index.UpdateTypoToleranceAsync, newTypoTolerance);
await AssertGetEquality(_index.GetTypoToleranceAsync, returnedTypoTolerance);
}

[Fact]
public async Task UpdateTypoTolerancePartially()
{
var newTypoTolerance = new TypoTolerance
{
DisableOnWords = new string[] { "harry", "potter" },
};

var otherUpdateTypoTolerance = new TypoTolerance
{
DisableOnAttributes = new string[] { "title" },
};

var returnedTypoTolerance = new TypoTolerance
{
DisableOnAttributes = new string[] { "title" },
DisableOnWords = new string[] { "harry", "potter" },
MinWordSizeForTypos = new TypoTolerance.TypoSize
{
TwoTypos = 9,
OneTypo = 5
}
};

await AssertUpdateSuccess(_index.UpdateTypoToleranceAsync, newTypoTolerance);
await AssertUpdateSuccess(_index.UpdateTypoToleranceAsync, otherUpdateTypoTolerance);
await AssertGetEquality(_index.GetTypoToleranceAsync, returnedTypoTolerance);
}

[Fact]
public async Task ResetTypoTolerance()
{
var newTypoTolerance = new TypoTolerance
{
DisableOnWords = new string[] { "harry", "potter" },
MinWordSizeForTypos = new TypoTolerance.TypoSize
{
TwoTypos = 12
}
};

var returnedTypoTolerance = new TypoTolerance
{
DisableOnAttributes = new string[] { },
DisableOnWords = new string[] { "harry", "potter" },
MinWordSizeForTypos = new TypoTolerance.TypoSize
{
TwoTypos = 12,
OneTypo = 5
}
};

await AssertUpdateSuccess(_index.UpdateTypoToleranceAsync, newTypoTolerance);
await AssertGetEquality(_index.GetTypoToleranceAsync, returnedTypoTolerance);

await AssertResetSuccess(_index.ResetTypoToleranceAsync);
await AssertGetEquality(_index.GetTypoToleranceAsync, _defaultSettings.TypoTolerance);
}

private static Settings SettingsWithDefaultedNullFields(Settings inputSettings, Settings defaultSettings)
{
return new Settings
Expand All @@ -347,6 +450,7 @@ private static Settings SettingsWithDefaultedNullFields(Settings inputSettings,
Synonyms = inputSettings.Synonyms ?? defaultSettings.Synonyms,
FilterableAttributes = inputSettings.FilterableAttributes ?? defaultSettings.FilterableAttributes,
SortableAttributes = inputSettings.SortableAttributes ?? defaultSettings.SortableAttributes,
TypoTolerance = inputSettings.TypoTolerance ?? defaultSettings.TypoTolerance
};
}

Expand Down