@@ -629,6 +629,9 @@ export class Query<
629629 * @param options - Options control the vector query. `limit` specifies the upper bound of documents to return, must
630630 * be a positive integer with a maximum value of 1000. `distanceMeasure` specifies what type of distance is calculated
631631 * when performing the query.
632+ *
633+ * @deprecated Use the new {@link findNearest} implementation
634+ * accepting a single `options` param.
632635 */
633636 findNearest (
634637 vectorField : string | firestore . FieldPath ,
@@ -637,30 +640,85 @@ export class Query<
637640 limit : number ;
638641 distanceMeasure : 'EUCLIDEAN' | 'COSINE' | 'DOT_PRODUCT' ;
639642 }
643+ ) : VectorQuery < AppModelType , DbModelType > ;
644+
645+ /**
646+ * Returns a query that can perform vector distance (similarity) search with given parameters.
647+ *
648+ * The returned query, when executed, performs a distance (similarity) search on the specified
649+ * `vectorField` against the given `queryVector` and returns the top documents that are closest
650+ * to the `queryVector`.
651+ *
652+ * Only documents whose `vectorField` field is a {@link VectorValue} of the same dimension as `queryVector`
653+ * participate in the query, all other documents are ignored.
654+ *
655+ * @example
656+ * ```
657+ * // Returns the closest 10 documents whose Euclidean distance from their 'embedding' fields are closed to [41, 42].
658+ * const vectorQuery = col.findNearest({
659+ * vectorField: 'embedding',
660+ * queryVector: [41, 42],
661+ * limit: 10,
662+ * distanceMeasure: 'EUCLIDEAN',
663+ * distanceResultField: 'distance',
664+ * distanceThreshold: 0.125
665+ * });
666+ *
667+ * const querySnapshot = await aggregateQuery.get();
668+ * querySnapshot.forEach(...);
669+ * ```
670+ * @param options - An argument specifying the behavior of the {@link VectorQuery} returned by this function.
671+ * See {@link VectorQueryOptions}.
672+ */
673+ findNearest (
674+ options : VectorQueryOptions
675+ ) : VectorQuery < AppModelType , DbModelType > ;
676+
677+ findNearest (
678+ vectorFieldOrOptions : string | firestore . FieldPath | VectorQueryOptions ,
679+ queryVector ?: firestore . VectorValue | Array < number > ,
680+ options ?: {
681+ limit ?: number ;
682+ distanceMeasure ?: 'EUCLIDEAN' | 'COSINE' | 'DOT_PRODUCT' ;
683+ }
640684 ) : VectorQuery < AppModelType , DbModelType > {
641- validateFieldPath ( 'vectorField' , vectorField ) ;
685+ if (
686+ typeof vectorFieldOrOptions === 'string' ||
687+ vectorFieldOrOptions instanceof FieldPath
688+ ) {
689+ const vqOptions : VectorQueryOptions = {
690+ distanceMeasure : options ! . distanceMeasure ! ,
691+ limit : options ! . limit ! ,
692+ queryVector : queryVector ! ,
693+ vectorField : vectorFieldOrOptions ,
694+ } ;
695+ return this . _findNearest ( vqOptions ) ;
696+ } else {
697+ return this . _findNearest ( vectorFieldOrOptions as VectorQueryOptions ) ;
698+ }
699+ }
700+
701+ _findNearest (
702+ options : VectorQueryOptions
703+ ) : VectorQuery < AppModelType , DbModelType > {
704+ validateFieldPath ( 'vectorField' , options . vectorField ) ;
642705
643706 if ( options . limit <= 0 ) {
644- throw invalidArgumentMessage ( 'options. limit' , 'positive limit number' ) ;
707+ throw invalidArgumentMessage ( 'limit' , 'positive limit number' ) ;
645708 }
646709
647710 if (
648- ( Array . isArray ( queryVector )
649- ? queryVector . length
650- : queryVector . toArray ( ) . length ) === 0
711+ ( Array . isArray ( options . queryVector )
712+ ? options . queryVector . length
713+ : options . queryVector . toArray ( ) . length ) === 0
651714 ) {
652715 throw invalidArgumentMessage (
653716 'queryVector' ,
654717 'vector size must be larger than 0'
655718 ) ;
656719 }
657720
658- return new VectorQuery < AppModelType , DbModelType > (
659- this ,
660- vectorField ,
661- queryVector ,
662- new VectorQueryOptions ( options . limit , options . distanceMeasure )
663- ) ;
721+ return new VectorQuery < AppModelType , DbModelType > ( this , options ) ;
664722 }
665723
666724 /**
0 commit comments