Skip to content

.Net: Add Filter to TextSearchProvider #12662

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

Merged
merged 7 commits into from
Jul 7, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public override async Task<AIContext> ModelInvokingAsync(ICollection<ChatMessage

var searchResults = await this._textSearch.GetTextSearchResultsAsync(
input,
new() { Top = this.Options.Top },
new() { Top = this.Options.Top, Filter = this.Options.Filter },
cancellationToken: cancellationToken).ConfigureAwait(false);

var results = await searchResults.Results.ToListAsync(cancellationToken).ConfigureAwait(false);
Expand All @@ -96,7 +96,7 @@ internal async Task<string> SearchAsync(string userQuestion, CancellationToken c
{
var searchResults = await this._textSearch.GetTextSearchResultsAsync(
userQuestion,
new() { Top = this.Options.Top },
new() { Top = this.Options.Top, Filter = this.Options.Filter },
cancellationToken: cancellationToken).ConfigureAwait(false);

var results = await searchResults.Results.ToListAsync(cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public int Top
}
}

/// <summary>
/// Gets or sets the filter expression to apply to the search query.
/// </summary>
public TextSearchFilter? Filter { get; init; }

/// <summary>
/// Gets or sets the time at which the text search is performed.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,56 @@ public async Task ModelInvokingShouldUseOverrideContextFormatterIfProvidedAsync(
// Assert
Assert.Equal("Custom formatted context with 2 results.", result.Instructions);
}

[Fact]
public async Task SearchAsyncRespectsFilterOption()
{
// Arrange
var mockTextSearch = new Mock<ITextSearch>();
var searchResults = new Mock<IAsyncEnumerable<TextSearchResult>>();
var mockEnumerator = new Mock<IAsyncEnumerator<TextSearchResult>>();

// Simulate the filtered results
var filteredResult = new TextSearchResult("Filtered Content") { Name = "FilteredDoc", Link = "http://example.com/filtered" };
var results = new List<TextSearchResult> { filteredResult };

mockEnumerator.SetupSequence(e => e.MoveNextAsync())
.ReturnsAsync(true)
.ReturnsAsync(false);

mockEnumerator.SetupSequence(e => e.Current)
.Returns(filteredResult);

searchResults.Setup(r => r.GetAsyncEnumerator(It.IsAny<CancellationToken>()))
.Returns(mockEnumerator.Object);

TextSearchFilter? capturedFilter = null;
mockTextSearch.Setup(ts => ts.GetTextSearchResultsAsync(
It.IsAny<string>(),
It.IsAny<TextSearchOptions>(),
It.IsAny<CancellationToken>()))
.Callback<string, TextSearchOptions?, CancellationToken>((q, opts, ct) =>
{
capturedFilter = opts?.Filter;
})
.ReturnsAsync(new KernelSearchResults<TextSearchResult>(searchResults.Object));

var filter = new TextSearchFilter().Equality("Name", "FilteredDoc");
var options = new TextSearchProviderOptions
{
Filter = filter
};

var provider = new TextSearchProvider(mockTextSearch.Object, options: options);

// Act
var result = await provider.SearchAsync("Sample user question?", CancellationToken.None);

// Assert
Assert.Contains("Filtered Content", result);
Assert.Contains("SourceDocName: FilteredDoc", result);
Assert.Contains("SourceDocLink: http://example.com/filtered", result);
Assert.NotNull(capturedFilter);
Assert.Equal(filter, capturedFilter);
}
}
Loading