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 rank threshold #582

Merged
merged 11 commits into from
Nov 27, 2024
6 changes: 6 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -782,3 +782,9 @@ distinct_attribute_guide_distinct_parameter_1: |-
Distinct = "sku"
};
await client.Index("products").SearchAsync<Product>("white shirt", params);
search_parameter_reference_ranking_score_threshold_1: |-
var params = new SearchQuery()
{
RankingScoreThreshold = 0.2M
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

c# requires M when setting a decimal value. 0.2 is interpreted as a double and 0.2M is a decimal.
ex:
image

};
await client.Index("INDEX_NAME").SearchAsync<T>("badman", params);
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove the "no new line" at the end of the line?

Copy link
Member

Choose a reason for hiding this comment

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

we follow the editorconfig rules

6 changes: 6 additions & 0 deletions src/Meilisearch/SearchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,11 @@ public class SearchQuery
/// </summary>
[JsonPropertyName("distinct")]
public string Distinct { get; set; }

/// <summary>
/// Gets or sets rankingScoreThreshold, a number between 0.0 and 1.0.
/// </summary>
[JsonPropertyName("rankingScoreThreshold")]
public decimal RankingScoreThreshold { get; set; }
}
}
17 changes: 17 additions & 0 deletions tests/Meilisearch.Tests/IndexFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public async Task<Index> SetUpIndexForNestedSearch(string indexUid)

return index;
}

public async Task<Index> SetUpIndexForDistinctProductsSearch(string indexUid)
{
var index = DefaultClient.Index(indexUid);
Expand Down Expand Up @@ -156,6 +157,22 @@ public async Task<Index> SetUpIndexForDistinctProductsSearch(string indexUid)
return index;
}

public async Task<Index> SetUpIndexForRankingScoreThreshold(string indexUid)
{
var index = DefaultClient.Index(indexUid);
var movies = await JsonFileReader.ReadAsync<List<MovieWithInfo>>(Datasets.MoviesWithInfoJsonPath);
var task = await index.AddDocumentsAsync(movies);

// Check the documents have been added
var finishedTask = await index.WaitForTaskAsync(task.TaskUid);
if (finishedTask.Status != TaskInfoStatus.Succeeded)
{
throw new Exception("The documents were not added during SetUpIndexForRankingScoreThreshold. Impossible to run the tests.");
}

return index;
}

public async Task DeleteAllIndexes()
{
var indexes = await DefaultClient.GetAllIndexesAsync();
Expand Down
17 changes: 17 additions & 0 deletions tests/Meilisearch.Tests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public abstract class SearchTests<TFixture> : IAsyncLifetime where TFixture : In
private Index _indexForFaceting;
private Index _indexWithIntId;
private Index _productIndexForDistinct;
private Index _indexForRankingScoreThreshold;

private readonly TFixture _fixture;

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

public Task DisposeAsync() => Task.CompletedTask;
Expand Down Expand Up @@ -527,6 +529,7 @@ public async Task CustomSearchProductsWithoutDistinct()
var products = await _productIndexForDistinct.SearchAsync<Product>("", searchQuery);
products.Hits.Count.Should().Be(14);
}

[Fact]
public async Task CustomSearchProductsWithDistinct()
{
Expand All @@ -537,5 +540,19 @@ public async Task CustomSearchProductsWithDistinct()
var products = await _productIndexForDistinct.SearchAsync<Product>("", searchQuery);
products.Hits.Count.Should().Be(6);
}

[Fact]
public async Task CustomSearchWithRankingScoreThreshold()
{
var searchQuery = new SearchQuery { };
var movies = await _indexForRankingScoreThreshold.SearchAsync<MovieWithInfo>("a wizard movie", searchQuery);
movies.Hits.Count.Should().Be(4);

searchQuery.RankingScoreThreshold = 0.5M;
movies = await _indexForRankingScoreThreshold.SearchAsync<MovieWithInfo>("a wizard movie", searchQuery);
movies.Hits.Count.Should().Be(1);
movies.Hits.First().Id.Should().Be("13");
movies.Hits.First().Name.Should().Be("Harry Potter");
}
}
}
Loading