Skip to content

Commit 46e2f37

Browse files
committed
feat: Add LINQ filtering examples to GettingStartedWithTextSearch sample
- Add BingSearchWithLinqFilteringAsync() and GoogleSearchWithLinqFilteringAsync() demonstrating ITextSearch<TRecord> with type-safe LINQ filtering - Add XML documentation following Microsoft standards for all public properties - Include C# 14 compatibility guidance for Contains() method usage in filtering examples - Maintain backward compatibility with existing simple search examples Addresses #10456
1 parent 16ec4db commit 46e2f37

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

dotnet/samples/GettingStartedWithTextSearch/InMemoryVectorStoreFixture.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,24 @@ namespace GettingStartedWithTextSearch;
1515
/// </summary>
1616
public class InMemoryVectorStoreFixture : IAsyncLifetime
1717
{
18+
/// <summary>
19+
/// Gets the embedding generator used for creating vector embeddings.
20+
/// </summary>
1821
public IEmbeddingGenerator<string, Embedding<float>> EmbeddingGenerator { get; private set; }
1922

23+
/// <summary>
24+
/// Gets the in-memory vector store instance.
25+
/// </summary>
2026
public InMemoryVectorStore InMemoryVectorStore { get; private set; }
2127

28+
/// <summary>
29+
/// Gets the vector store record collection for data models.
30+
/// </summary>
2231
public VectorStoreCollection<Guid, DataModel> VectorStoreRecordCollection { get; private set; }
2332

33+
/// <summary>
34+
/// Gets the name of the collection used for storing records.
35+
/// </summary>
2436
public string CollectionName => "records";
2537

2638
/// <summary>
@@ -138,21 +150,36 @@ private async Task<VectorStoreCollection<TKey, TRecord>> CreateCollectionFromLis
138150
/// </remarks>
139151
public sealed class DataModel
140152
{
153+
/// <summary>
154+
/// Gets or sets the unique identifier for this record.
155+
/// </summary>
141156
[VectorStoreKey]
142157
[TextSearchResultName]
143158
public Guid Key { get; init; }
144159

160+
/// <summary>
161+
/// Gets or sets the text content of this record.
162+
/// </summary>
145163
[VectorStoreData]
146164
[TextSearchResultValue]
147165
public string Text { get; init; }
148166

167+
/// <summary>
168+
/// Gets or sets the link associated with this record.
169+
/// </summary>
149170
[VectorStoreData]
150171
[TextSearchResultLink]
151172
public string Link { get; init; }
152173

174+
/// <summary>
175+
/// Gets or sets the tag for categorizing this record.
176+
/// </summary>
153177
[VectorStoreData(IsIndexed = true)]
154178
public required string Tag { get; init; }
155179

180+
/// <summary>
181+
/// Gets the embedding representation of the text content.
182+
/// </summary>
156183
[VectorStoreVector(1536)]
157184
public string Embedding => Text;
158185
}

dotnet/samples/GettingStartedWithTextSearch/Step1_Web_Search.cs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,79 @@ public async Task GoogleSearchAsync()
5353
}
5454
}
5555

56+
/// <summary>
57+
/// Show how to use <see cref="BingTextSearch"/> with the new generic <see cref="ITextSearch{TRecord}"/>
58+
/// interface and LINQ filtering for type-safe searches.
59+
/// </summary>
60+
[Fact]
61+
public async Task BingSearchWithLinqFilteringAsync()
62+
{
63+
// Create a BingTextSearch instance
64+
var bingSearch = new BingTextSearch(apiKey: TestConfiguration.Bing.ApiKey);
65+
66+
// Use the new generic interface for type-safe searches
67+
ITextSearch<BingWebPage> textSearch = bingSearch;
68+
69+
var query = "What is the Semantic Kernel?";
70+
71+
// Use LINQ filtering for type-safe search with compile-time validation
72+
var options = new TextSearchOptions<BingWebPage>
73+
{
74+
Top = 4,
75+
Filter = page => page.Language == "en" && page.IsFamilyFriendly == true
76+
};
77+
78+
// Search and return strongly-typed results
79+
Console.WriteLine("\n--- Bing Search with LINQ Filtering ---\n");
80+
KernelSearchResults<BingWebPage> searchResults = await textSearch.GetSearchResultsAsync(query, options);
81+
await foreach (BingWebPage page in searchResults.Results)
82+
{
83+
Console.WriteLine($"Name: {page.Name}");
84+
Console.WriteLine($"Snippet: {page.Snippet}");
85+
Console.WriteLine($"Url: {page.Url}");
86+
Console.WriteLine($"Language: {page.Language}");
87+
Console.WriteLine($"Family Friendly: {page.IsFamilyFriendly}");
88+
Console.WriteLine("---");
89+
}
90+
}
91+
92+
/// <summary>
93+
/// Show how to use <see cref="GoogleTextSearch"/> with the new generic <see cref="ITextSearch{TRecord}"/>
94+
/// interface and LINQ filtering for type-safe searches.
95+
/// </summary>
96+
[Fact]
97+
public async Task GoogleSearchWithLinqFilteringAsync()
98+
{
99+
// Create a GoogleTextSearch instance
100+
var googleSearch = new GoogleTextSearch(
101+
searchEngineId: TestConfiguration.Google.SearchEngineId,
102+
apiKey: TestConfiguration.Google.ApiKey);
103+
104+
// Use the new generic interface for type-safe searches
105+
ITextSearch<GoogleWebPage> textSearch = googleSearch;
106+
107+
var query = "What is the Semantic Kernel?";
108+
109+
// Use LINQ filtering for type-safe search with compile-time validation
110+
var options = new TextSearchOptions<GoogleWebPage>
111+
{
112+
Top = 4,
113+
Filter = page => page.Title.Contains("Semantic") && page.DisplayLink.EndsWith(".com")
114+
};
115+
116+
// Search and return strongly-typed results
117+
Console.WriteLine("\n--- Google Search with LINQ Filtering ---\n");
118+
KernelSearchResults<GoogleWebPage> searchResults = await textSearch.GetSearchResultsAsync(query, options);
119+
await foreach (GoogleWebPage page in searchResults.Results)
120+
{
121+
Console.WriteLine($"Title: {page.Title}");
122+
Console.WriteLine($"Snippet: {page.Snippet}");
123+
Console.WriteLine($"Link: {page.Link}");
124+
Console.WriteLine($"Display Link: {page.DisplayLink}");
125+
Console.WriteLine("---");
126+
}
127+
}
128+
56129
/// <summary>
57130
/// Show how to create a <see cref="BingTextSearch"/> and use it to perform a search
58131
/// and return results as a collection of <see cref="BingWebPage"/> instances.
@@ -86,7 +159,7 @@ public async Task SearchForWebPagesAsync()
86159
}
87160
else
88161
{
89-
Console.WriteLine("\n——— Google Web Page Results ———\n");
162+
Console.WriteLine("\n��� Google Web Page Results ���\n");
90163
await foreach (Google.Apis.CustomSearchAPI.v1.Data.Result result in objectResults.Results)
91164
{
92165
Console.WriteLine($"Title: {result.Title}");

0 commit comments

Comments
 (0)