VectorQuery<T> class

A query that finds the documents whose vector field is closest to a given query vector. Create one with Query.findNearest.

Vector queries are always executed on the server: they fail with an unavailable error when the client is offline. Results are ordered by distance, nearest first.

final VectorQuerySnapshot nearest = await firestore
    .collection('items')
    .where('category', isEqualTo: 'shoes')
    .findNearest(
      'embedding',
      <double>[0.1, 0.2, 0.3],
      5,
      DistanceMeasure.cosine,
      distanceResultField: 'distance',
    )
    .get();
for (final QueryDocumentSnapshot<Object?> doc in nearest) {
  print('${doc.id}: ${doc.get('distance')}');
}

The filters of the query run first; documents without a vector in the field are skipped. The local cache is not used and not updated, and there is no listener for a vector query. On a real database the field needs a vector index, which you create with the Google Cloud tools.

The JS client SDK has no findNearest on a classic query (there it is a pipeline stage); this follows the Android SDK.

Properties

distanceMeasure → DistanceMeasure
How the distance between vectors is measured.
final
distanceResultField → String?
If set, the computed distance is written to this field of each result (only in the results, not in the stored documents). Read it with doc.get(field).
final
distanceThreshold → double?
If set, only documents within this distance of queryVector are returned.
final
hashCode → int
The hash code for this object.
no setteroverride
limit → int
The maximum number of results, between 1 and 1000.
final
query → Query<T>
The query whose results are searched: its filters select the candidates.
final
queryVector → VectorValue
The vector to find the nearest neighbours of, with between 1 and 2048 dimensions.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
vectorField → FieldPath
The field holding the vectors to search.
final

Methods

get() → Future<VectorQuerySnapshot<T>>
Executes the query on the server and returns the nearest documents, nearest first.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
override