@@ -34,6 +34,7 @@ public class AzureAISearchMemory : IMemoryDb, IMemoryDbUpsertBatch
3434 private readonly ITextEmbeddingGenerator _embeddingGenerator ;
3535 private readonly ILogger < AzureAISearchMemory > _log ;
3636 private readonly bool _useHybridSearch ;
37+ private readonly bool _useSessionId ;
3738
3839 /// <summary>
3940 /// Create a new instance
@@ -49,6 +50,7 @@ public AzureAISearchMemory(
4950 this . _embeddingGenerator = embeddingGenerator ;
5051 this . _log = ( loggerFactory ?? DefaultLogger . Factory ) . CreateLogger < AzureAISearchMemory > ( ) ;
5152 this . _useHybridSearch = config . UseHybridSearch ;
53+ this . _useSessionId = config . UseSessionId ;
5254
5355 if ( string . IsNullOrEmpty ( config . Endpoint ) )
5456 {
@@ -190,7 +192,7 @@ await client.IndexDocumentsAsync(
190192 FilterMode = VectorFilterMode . PreFilter
191193 }
192194 } ;
193- DefineFieldsToSelect ( options , withEmbeddings ) ;
195+ this . ApplyCommonSearchOptions ( options , withEmbeddings , filters ) ;
194196
195197 if ( limit > 0 )
196198 {
@@ -199,15 +201,6 @@ await client.IndexDocumentsAsync(
199201 this . _log . LogDebug ( "KNearestNeighborsCount and max results: {0}" , limit ) ;
200202 }
201203
202- // Remove empty filters
203- filters = filters ? . Where ( f => ! f . IsEmpty ( ) ) . ToList ( ) ;
204-
205- if ( filters is { Count : > 0 } )
206- {
207- options . Filter = AzureAISearchFiltering . BuildSearchFilter ( filters ) ;
208- this . _log . LogDebug ( "Filtering vectors, condition: {0}" , options . Filter ) ;
209- }
210-
211204 Response < SearchResults < AzureAISearchMemoryRecord > > ? searchResult = null ;
212205 try
213206 {
@@ -254,33 +247,14 @@ public async IAsyncEnumerable<MemoryRecord> GetListAsync(
254247 var client = this . GetSearchClient ( index ) ;
255248
256249 SearchOptions options = new ( ) ;
257- DefineFieldsToSelect ( options , withEmbeddings ) ;
250+ this . ApplyCommonSearchOptions ( options , withEmbeddings , filters ) ;
258251
259252 if ( limit > 0 )
260253 {
261254 options . Size = limit ;
262255 this . _log . LogDebug ( "Max results: {0}" , limit ) ;
263256 }
264257
265- // Remove empty filters
266- filters = filters ? . Where ( f => ! f . IsEmpty ( ) ) . ToList ( ) ;
267-
268- if ( filters is { Count : > 0 } )
269- {
270- options . Filter = AzureAISearchFiltering . BuildSearchFilter ( filters ) ;
271- this . _log . LogDebug ( "Filtering vectors, condition: {0}" , options . Filter ) ;
272- }
273-
274- // See: https://learn.microsoft.com/azure/search/search-query-understand-collection-filters
275- // fieldValue = fieldValue.Replace("'", "''", StringComparison.Ordinal);
276- // var options = new SearchOptions
277- // {
278- // Filter = fieldIsCollection
279- // ? $"{fieldName}/any(s: s eq '{fieldValue}')"
280- // : $"{fieldName} eq '{fieldValue}')",
281- // Size = limit
282- // };
283-
284258 Response < SearchResults < AzureAISearchMemoryRecord > > ? searchResult = null ;
285259 try
286260 {
@@ -627,7 +601,10 @@ at Azure.Search.Documents.SearchClient.SearchInternal[T](SearchOptions options,
627601 return indexSchema ;
628602 }
629603
630- private static void DefineFieldsToSelect ( SearchOptions options , bool withEmbeddings )
604+ private void ApplyCommonSearchOptions (
605+ SearchOptions options ,
606+ bool withEmbeddings ,
607+ ICollection < MemoryFilter > ? filters = null )
631608 {
632609 options . Select . Add ( AzureAISearchMemoryRecord . IdField ) ;
633610 options . Select . Add ( AzureAISearchMemoryRecord . TagsField ) ;
@@ -636,6 +613,30 @@ private static void DefineFieldsToSelect(SearchOptions options, bool withEmbeddi
636613 {
637614 options . Select . Add ( AzureAISearchMemoryRecord . VectorField ) ;
638615 }
616+
617+ // Remove empty filters
618+ filters = filters ? . Where ( f => ! f . IsEmpty ( ) ) . ToList ( ) ;
619+
620+ if ( filters is { Count : > 0 } )
621+ {
622+ options . Filter = AzureAISearchFiltering . BuildSearchFilter ( filters ) ;
623+ this . _log . LogDebug ( "Filtering vectors, condition: {0}" , options . Filter ) ;
624+ }
625+
626+ // See: https://learn.microsoft.com/azure/search/search-query-understand-collection-filters
627+ // fieldValue = fieldValue.Replace("'", "''", StringComparison.Ordinal);
628+ // var options = new SearchOptions
629+ // {
630+ // Filter = fieldIsCollection
631+ // ? $"{fieldName}/any(s: s eq '{fieldValue}')"
632+ // : $"{fieldName} eq '{fieldValue}')",
633+ // Size = limit
634+ // };
635+
636+ if ( this . _useSessionId )
637+ {
638+ options . SessionId = Guid . NewGuid ( ) . ToString ( "N" ) ;
639+ }
639640 }
640641
641642 private static double ScoreToCosineSimilarity ( double score )
0 commit comments