1414import com .azure .search .documents .models .VectorQuery ;
1515import com .azure .search .documents .models .VectorizableTextQuery ;
1616import com .azure .search .documents .models .VectorizedQuery ;
17- import com .microsoft .semantickernel .data .vectorsearch .VectorizableSearch ;
17+ import com .microsoft .semantickernel .data .vectorsearch .VectorizableTextSearch ;
1818import com .microsoft .semantickernel .data .vectorsearch .VectorSearchResult ;
1919import com .microsoft .semantickernel .data .vectorsearch .VectorizedSearch ;
2020import com .microsoft .semantickernel .data .vectorstorage .VectorStoreRecordCollection ;
2828import com .microsoft .semantickernel .data .vectorstorage .options .GetRecordOptions ;
2929import com .microsoft .semantickernel .data .vectorstorage .options .UpsertRecordOptions ;
3030import com .microsoft .semantickernel .data .vectorstorage .options .VectorSearchOptions ;
31- import com .microsoft .semantickernel .data .vectorsearch .queries .VectorSearchQuery ;
32- import com .microsoft .semantickernel .data .vectorsearch .queries .VectorizableTextSearchQuery ;
33- import com .microsoft .semantickernel .data .vectorsearch .queries .VectorizedSearchQuery ;
3431import com .microsoft .semantickernel .exceptions .SKException ;
3532import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
3633import java .time .OffsetDateTime ;
4946public class AzureAISearchVectorStoreRecordCollection <Record > implements
5047 VectorStoreRecordCollection <String , Record >,
5148 VectorizedSearch <Record >,
52- VectorizableSearch <Record > {
49+ VectorizableTextSearch <Record > {
5350
5451 private static final HashSet <Class <?>> supportedKeyTypes = new HashSet <>(
5552 Collections .singletonList (
@@ -277,8 +274,25 @@ public Mono<Void> deleteBatchAsync(List<String> keys, DeleteRecordOptions option
277274 }).collect (Collectors .toList ())).then ();
278275 }
279276
280- private Mono <List <VectorSearchResult <Record >>> searchAndMapAsync (SearchOptions searchOptions ,
277+ private Mono <List <VectorSearchResult <Record >>> searchAndMapAsync (
278+ List <VectorQuery > vectorQueries , VectorSearchOptions options ,
281279 GetRecordOptions getRecordOptions ) {
280+
281+ String filter = AzureAISearchVectorStoreCollectionSearchMapping
282+ .buildFilterString (options .getVectorSearchFilter (), recordDefinition );
283+
284+ SearchOptions searchOptions = new SearchOptions ()
285+ .setFilter (filter )
286+ .setTop (options .getLimit ())
287+ .setSkip (options .getOffset ())
288+ .setScoringParameters ()
289+ .setVectorSearchOptions (new com .azure .search .documents .models .VectorSearchOptions ()
290+ .setQueries (vectorQueries ));
291+
292+ if (!options .isIncludeVectors ()) {
293+ searchOptions .setSelect (nonVectorFields .toArray (new String [0 ]));
294+ }
295+
282296 VectorStoreRecordMapper <Record , SearchDocument > mapper = this .options
283297 .getVectorStoreRecordMapper ();
284298
@@ -300,70 +314,32 @@ record = response.getDocument(this.options.getRecordClass());
300314 }
301315
302316 /**
303- * Search the vector store for records that match the given embedding and filter .
317+ * Vectorizable text search. This method searches for records that are similar to the given text .
304318 *
305- * @param query The vector search query.
319+ * @param searchText The text to search with.
320+ * @param options The options to use for the search.
306321 * @return A list of search results.
307322 */
308323 @ Override
309- public Mono <List <VectorSearchResult <Record >>> searchAsync (VectorSearchQuery query ) {
324+ public Mono <List <VectorSearchResult <Record >>> searchAsync (String searchText ,
325+ VectorSearchOptions options ) {
310326 if (firstVectorFieldName == null ) {
311327 throw new SKException ("No vector fields defined. Cannot perform vector search" );
312328 }
313329
314- VectorSearchOptions options = query .getSearchOptions ();
315330 if (options == null ) {
316331 options = VectorSearchOptions .createDefault (firstVectorFieldName );
317332 }
318333
319334 List <VectorQuery > vectorQueries = new ArrayList <>();
320-
321- if (query instanceof VectorizedSearchQuery ) {
322- vectorQueries .add (new VectorizedQuery (((VectorizedSearchQuery ) query ).getVector ())
323- .setFields (recordDefinition .getField (options .getVectorFieldName () != null
324- ? options .getVectorFieldName ()
325- : firstVectorFieldName ).getEffectiveStorageName ())
326- .setKNearestNeighborsCount (options .getLimit ()));
327- } else if (query instanceof VectorizableTextSearchQuery ) {
328- vectorQueries
329- .add (new VectorizableTextQuery (((VectorizableTextSearchQuery ) query ).getQueryText ())
330- .setFields (recordDefinition .getField (options .getVectorFieldName () != null
331- ? options .getVectorFieldName ()
332- : firstVectorFieldName ).getEffectiveStorageName ())
333- .setKNearestNeighborsCount (options .getLimit ()));
334- } else {
335- throw new SKException ("Unsupported query type: " + query .getQueryType ());
336- }
337-
338- String filter = AzureAISearchVectorStoreCollectionSearchMapping
339- .buildFilterString (options .getVectorSearchFilter (), recordDefinition );
340-
341- SearchOptions searchOptions = new SearchOptions ()
342- .setFilter (filter )
343- .setTop (options .getLimit ())
344- .setSkip (options .getOffset ())
345- .setScoringParameters ()
346- .setVectorSearchOptions (new com .azure .search .documents .models .VectorSearchOptions ()
347- .setQueries (vectorQueries ));
348-
349- if (!options .isIncludeVectors ()) {
350- searchOptions .setSelect (nonVectorFields .toArray (new String [0 ]));
351- }
352-
353- return searchAndMapAsync (searchOptions , new GetRecordOptions (options .isIncludeVectors ()));
354- }
355-
356- /**
357- * Vectorizable text search. This method searches for records that are similar to the given text.
358- *
359- * @param searchText The text to search with.
360- * @param options The options to use for the search.
361- * @return A list of search results.
362- */
363- @ Override
364- public Mono <List <VectorSearchResult <Record >>> searchAsync (String searchText ,
365- VectorSearchOptions options ) {
366- return searchAsync (VectorSearchQuery .createQuery (searchText , options ));
335+ vectorQueries .add (new VectorizableTextQuery (searchText )
336+ .setFields (recordDefinition .getField (options .getVectorFieldName () != null
337+ ? options .getVectorFieldName ()
338+ : firstVectorFieldName ).getEffectiveStorageName ())
339+ .setKNearestNeighborsCount (options .getLimit ()));
340+
341+ return searchAndMapAsync (vectorQueries , options ,
342+ new GetRecordOptions (options .isIncludeVectors ()));
367343 }
368344
369345 /**
@@ -376,6 +352,22 @@ public Mono<List<VectorSearchResult<Record>>> searchAsync(String searchText,
376352 @ Override
377353 public Mono <List <VectorSearchResult <Record >>> searchAsync (List <Float > vector ,
378354 VectorSearchOptions options ) {
379- return searchAsync (VectorSearchQuery .createQuery (vector , options ));
355+ if (firstVectorFieldName == null ) {
356+ throw new SKException ("No vector fields defined. Cannot perform vector search" );
357+ }
358+
359+ if (options == null ) {
360+ options = VectorSearchOptions .createDefault (firstVectorFieldName );
361+ }
362+
363+ List <VectorQuery > vectorQueries = new ArrayList <>();
364+ vectorQueries .add (new VectorizedQuery (vector )
365+ .setFields (recordDefinition .getField (options .getVectorFieldName () != null
366+ ? options .getVectorFieldName ()
367+ : firstVectorFieldName ).getEffectiveStorageName ())
368+ .setKNearestNeighborsCount (options .getLimit ()));
369+
370+ return searchAndMapAsync (vectorQueries , options ,
371+ new GetRecordOptions (options .isIncludeVectors ()));
380372 }
381373}
0 commit comments