Class Query
- Direct Known Subclasses:
CollectionGroup,CollectionReference
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedQuery(com.google.cloud.firestore.FirestoreRpcContext<?> rpcContext, com.google.cloud.firestore.Query.QueryOptions queryOptions) -
Method Summary
Modifier and TypeMethodDescriptionaddSnapshotListener(EventListener<QuerySnapshot> listener) Starts listening to this query.addSnapshotListener(Executor executor, EventListener<QuerySnapshot> listener) Starts listening to this query.aggregate(AggregateField aggregateField1, AggregateField... aggregateFields) Calculates the specified aggregations over the documents in the result set of the given query without actually downloading the documents.count()Returns a query that counts the documents in the result set of this query.endAt(DocumentSnapshot snapshot) Creates and returns a new Query that ends at the provided document (inclusive).Creates and returns a new Query that ends at the provided fields relative to the order of the query.endBefore(DocumentSnapshot snapshot) Creates and returns a new Query that ends before the provided document (exclusive).Creates and returns a new Query that ends before the provided fields relative to the order of the query.booleanReturns true if this Query is equal to the provided object.com.google.api.core.ApiFuture<ExplainResults<QuerySnapshot>>explain(ExplainOptions options) Plans and optionally executes this query.com.google.api.core.ApiFuture<ExplainMetrics>explainStream(ExplainOptions options, com.google.api.gax.rpc.ApiStreamObserver<DocumentSnapshot> documentObserver) Executes the query, streams the results as a StreamObserver of DocumentSnapshots, and returns an ApiFuture that will be resolved with the associatedExplainMetrics.findNearest(FieldPath vectorField, VectorValue queryVector, int limit, VectorQuery.DistanceMeasure distanceMeasure) Returns a VectorQuery that can perform vector distance (similarity) search with given parameters.findNearest(FieldPath vectorField, VectorValue queryVector, int limit, VectorQuery.DistanceMeasure distanceMeasure, VectorQueryOptions vectorQueryOptions) Returns a VectorQuery that can perform vector distance (similarity) search with given parameters.findNearest(String vectorField, double[] queryVector, int limit, VectorQuery.DistanceMeasure distanceMeasure) Returns a VectorQuery that can perform vector distance (similarity) search with given parameters.findNearest(String vectorField, double[] queryVector, int limit, VectorQuery.DistanceMeasure distanceMeasure, VectorQueryOptions vectorQueryOptions) Returns a VectorQuery that can perform vector distance (similarity) search with given parameters.static QueryfromProto(Firestore firestore, RunQueryRequest proto) Returns a Query instance that can be used to execute the providedRunQueryRequest.com.google.api.core.ApiFuture<QuerySnapshot>get()Executes the query and returns the results as QuerySnapshot.inthashCode()limit(int limit) Creates and returns a new Query that only returns the first matching documents.limitToLast(int limit) Creates and returns a new Query that only returns the last matching documents.offset(int offset) Creates and returns a new Query that skips the first n results.Creates and returns a new Query that's additionally sorted by the specified field.orderBy(FieldPath fieldPath, Query.Direction direction) Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.Creates and returns a new Query that's additionally sorted by the specified field.orderBy(String field, Query.Direction direction) Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.Creates and returns a new Query instance that applies a field mask to the result and returns the specified subset of fields.Creates and returns a new Query instance that applies a field mask to the result and returns the specified subset of fields.startAfter(DocumentSnapshot snapshot) Creates and returns a new Query that starts after the provided document (exclusive).startAfter(Object... fieldValues) Creates and returns a new Query that starts after the provided fields relative to the order of the query.startAt(DocumentSnapshot snapshot) Creates and returns a new Query that starts at the provided document (inclusive).Creates and returns a new Query that starts at the provided fields relative to the order of the query.voidstream(com.google.api.gax.rpc.ApiStreamObserver<DocumentSnapshot> responseObserver) Executes the query and streams the results as a StreamObserver of DocumentSnapshots.toProto()Returns theRunQueryRequestthat this Query instance represents.protected RunQueryRequest.BuildertoRunQueryRequestBuilder(com.google.protobuf.ByteString transactionId, com.google.cloud.Timestamp readTime, ExplainOptions explainOptions) Creates and returns a new Query with the additional filter.whereArrayContains(FieldPath fieldPath, Object value) Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain the provided value.whereArrayContains(String field, Object value) Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain the provided value.whereArrayContainsAny(FieldPath fieldPath, List<? extends Object> values) Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain at least one value from the provided list.whereArrayContainsAny(String field, List<? extends Object> values) Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain at least one value from the provided list.whereEqualTo(FieldPath fieldPath, Object value) Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be equal to the specified value.whereEqualTo(String field, Object value) Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be equal to the specified value.whereGreaterThan(FieldPath fieldPath, Object value) Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than the specified value.whereGreaterThan(String field, Object value) Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than the specified value.whereGreaterThanOrEqualTo(FieldPath fieldPath, Object value) Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than or equal to the specified value.whereGreaterThanOrEqualTo(String field, Object value) Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than or equal to the specified value.Creates and returns a new Query with the additional filter that documents must contain the specified field and the value must equal one of the values from the provided list.Creates and returns a new Query with the additional filter that documents must contain the specified field and the value must equal one of the values from the provided list.whereLessThan(FieldPath fieldPath, Object value) Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less than the specified value.whereLessThan(String field, Object value) Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less than the specified value.whereLessThanOrEqualTo(FieldPath fieldPath, Object value) Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less or equal to the specified value.whereLessThanOrEqualTo(String field, Object value) Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less or equal to the specified value.whereNotEqualTo(FieldPath fieldPath, Object value) Creates and returns a new Query with the additional filter that documents must contain the specified field and the value does not equal the specified value.whereNotEqualTo(String field, Object value) Creates and returns a new Query with the additional filter that documents must contain the specified field and its value does not equal the specified value.whereNotIn(FieldPath fieldPath, List<? extends Object> values) Creates and returns a new Query with the additional filter that documents must contain the specified field and the value does not equal any of the values from the provided list.whereNotIn(String field, List<? extends Object> values) Creates and returns a new Query with the additional filter that documents must contain the specified field and the value does not equal any of the values from the provided list.Methods inherited from class com.google.cloud.firestore.StreamableQuery
getFirestore, internalStream
-
Constructor Details
-
Query
protected Query(com.google.cloud.firestore.FirestoreRpcContext<?> rpcContext, com.google.cloud.firestore.Query.QueryOptions queryOptions)
-
-
Method Details
-
whereEqualTo
Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be equal to the specified value.- Parameters:
field- The name of the field to compare.value- The value for comparison.- Returns:
- The created Query.
-
whereEqualTo
Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be equal to the specified value.- Parameters:
fieldPath- The path of the field to compare.value- The value for comparison.- Returns:
- The created Query.
-
whereNotEqualTo
Creates and returns a new Query with the additional filter that documents must contain the specified field and its value does not equal the specified value.- Parameters:
field- The name of the field to compare.value- The value for comparison.- Returns:
- The created Query.
-
whereNotEqualTo
Creates and returns a new Query with the additional filter that documents must contain the specified field and the value does not equal the specified value.- Parameters:
fieldPath- The path of the field to compare.value- The value for comparison.- Returns:
- The created Query.
-
whereLessThan
Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less than the specified value.- Parameters:
field- The name of the field to compare.value- The value for comparison.- Returns:
- The created Query.
-
whereLessThan
Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less than the specified value.- Parameters:
fieldPath- The path of the field to compare.value- The value for comparison.- Returns:
- The created Query.
-
whereLessThanOrEqualTo
Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less or equal to the specified value.- Parameters:
field- The name of the field to compare.value- The value for comparison.- Returns:
- The created Query.
-
whereLessThanOrEqualTo
Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less or equal to the specified value.- Parameters:
fieldPath- The path of the field to compare.value- The value for comparison.- Returns:
- The created Query.
-
whereGreaterThan
Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than the specified value.- Parameters:
field- The name of the field to compare.value- The value for comparison.- Returns:
- The created Query.
-
whereGreaterThan
Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than the specified value.- Parameters:
fieldPath- The path of the field to compare.value- The value for comparison.- Returns:
- The created Query.
-
whereGreaterThanOrEqualTo
Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than or equal to the specified value.- Parameters:
field- The name of the field to compare.value- The value for comparison.- Returns:
- The created Query.
-
whereGreaterThanOrEqualTo
@Nonnull public Query whereGreaterThanOrEqualTo(@Nonnull FieldPath fieldPath, @Nonnull Object value) Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than or equal to the specified value.- Parameters:
fieldPath- The path of the field to compare.value- The value for comparison.- Returns:
- The created Query.
-
whereArrayContains
Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain the provided value.A Query can have only one whereArrayContains() filter and it cannot be combined with whereArrayContainsAny().
- Parameters:
field- The name of the field containing an array to searchvalue- The value that must be contained in the array- Returns:
- The created Query.
-
whereArrayContains
Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain the provided value.A Query can have only one whereArrayContains() filter and it cannot be combined with whereArrayContainsAny().
- Parameters:
fieldPath- The path of the field containing an array to searchvalue- The value that must be contained in the array- Returns:
- The created Query.
-
whereArrayContainsAny
@Nonnull public Query whereArrayContainsAny(@Nonnull String field, @Nonnull List<? extends Object> values) Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain at least one value from the provided list.A Query can have only one whereArrayContainsAny() filter and it cannot be combined with whereArrayContains() or whereIn().
- Parameters:
field- The name of the field containing an array to search.values- A list that contains the values to match.- Returns:
- The created Query.
-
whereArrayContainsAny
@Nonnull public Query whereArrayContainsAny(@Nonnull FieldPath fieldPath, @Nonnull List<? extends Object> values) Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain at least one value from the provided list.A Query can have only one whereArrayContainsAny() filter and it cannot be combined with whereArrayContains() or whereIn().
- Parameters:
fieldPath- The path of the field containing an array to search.values- A list that contains the values to match.- Returns:
- The created Query.
-
whereIn
Creates and returns a new Query with the additional filter that documents must contain the specified field and the value must equal one of the values from the provided list.A Query can have only one whereIn() filter, and it cannot be combined with whereArrayContainsAny().
- Parameters:
field- The name of the field to search.values- A list that contains the values to match.- Returns:
- The created Query.
-
whereIn
Creates and returns a new Query with the additional filter that documents must contain the specified field and the value must equal one of the values from the provided list.A Query can have only one whereIn() filter, and it cannot be combined with whereArrayContainsAny().
- Parameters:
fieldPath- The path of the field to search.values- A list that contains the values to match.- Returns:
- The created Query.
-
whereNotIn
Creates and returns a new Query with the additional filter that documents must contain the specified field and the value does not equal any of the values from the provided list.A Query can have only one whereNotIn() filter and it cannot be combined with whereArrayContains(), whereArrayContainsAny(), whereIn(), or whereNotEqualTo().
- Parameters:
field- The name of the field to search.values- The list that contains the values to match.- Returns:
- The created Query.
-
whereNotIn
@Nonnull public Query whereNotIn(@Nonnull FieldPath fieldPath, @Nonnull List<? extends Object> values) Creates and returns a new Query with the additional filter that documents must contain the specified field and the value does not equal any of the values from the provided list.A Query can have only one whereNotIn() filter, and it cannot be combined with whereArrayContains(), whereArrayContainsAny(), whereIn(), or whereNotEqualTo().
- Parameters:
fieldPath- The path of the field to search.values- The list that contains the values to match.- Returns:
- The created Query.
-
where
Creates and returns a new Query with the additional filter.- Parameters:
filter- The new filter to apply to the existing query.- Returns:
- The newly created Query.
-
orderBy
Creates and returns a new Query that's additionally sorted by the specified field.- Parameters:
field- The field to sort by.- Returns:
- The created Query.
-
orderBy
Creates and returns a new Query that's additionally sorted by the specified field.- Parameters:
fieldPath- The field to sort by.- Returns:
- The created Query.
-
orderBy
Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.- Parameters:
field- The field to sort by.direction- The direction to sort.- Returns:
- The created Query.
-
orderBy
Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.- Parameters:
fieldPath- The field to sort by.direction- The direction to sort.- Returns:
- The created Query.
-
limit
Creates and returns a new Query that only returns the first matching documents.- Parameters:
limit- The maximum number of items to return.- Returns:
- The created Query.
-
limitToLast
Creates and returns a new Query that only returns the last matching documents.You must specify at least one orderBy clause for limitToLast queries. Otherwise, an
IllegalStateExceptionis thrown during execution.Results for limitToLast() queries are only available once all documents are received. Hence, limitToLast() queries cannot be streamed via the
stream(ApiStreamObserver)API.- Parameters:
limit- the maximum number of items to return- Returns:
- the created Query
-
offset
Creates and returns a new Query that skips the first n results.- Parameters:
offset- The number of items to skip.- Returns:
- The created Query.
-
startAt
Creates and returns a new Query that starts at the provided document (inclusive). The starting position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of this query.- Parameters:
snapshot- The snapshot of the document to start at.- Returns:
- The created Query.
-
startAt
Creates and returns a new Query that starts at the provided fields relative to the order of the query. The order of the field values must match the order of the order by clauses of the query.- Parameters:
fieldValues- The field values to start this query at, in order of the query's order by.- Returns:
- The created Query.
-
select
Creates and returns a new Query instance that applies a field mask to the result and returns the specified subset of fields. You can specify a list of field paths to return, or use an empty list to only return the references of matching documents.- Parameters:
fields- The fields to include.- Returns:
- The created Query.
-
select
Creates and returns a new Query instance that applies a field mask to the result and returns the specified subset of fields. You can specify a list of field paths to return, or use an empty list to only return the references of matching documents.- Parameters:
fieldPaths- The field paths to include.- Returns:
- The created Query.
-
startAfter
Creates and returns a new Query that starts after the provided document (exclusive). The starting position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of this query.- Parameters:
snapshot- The snapshot of the document to start after.- Returns:
- The created Query.
-
startAfter
Creates and returns a new Query that starts after the provided fields relative to the order of the query. The order of the field values must match the order of the order by clauses of the query.- Parameters:
fieldValues- The field values to start this query after, in order of the query's order by.- Returns:
- The created Query.
-
endBefore
Creates and returns a new Query that ends before the provided document (exclusive). The end position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of this query.- Parameters:
snapshot- The snapshot of the document to end before.- Returns:
- The created Query.
-
endBefore
Creates and returns a new Query that ends before the provided fields relative to the order of the query. The order of the field values must match the order of the order by clauses of the query.- Parameters:
fieldValues- The field values to end this query before, in order of the query's order by.- Returns:
- The created Query.
-
endAt
Creates and returns a new Query that ends at the provided fields relative to the order of the query. The order of the field values must match the order of the order by clauses of the query.- Parameters:
fieldValues- The field values to end this query at, in order of the query's order by.- Returns:
- The created Query.
-
endAt
Creates and returns a new Query that ends at the provided document (inclusive). The end position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of this query.- Parameters:
snapshot- The snapshot of the document to end at.- Returns:
- The created Query.
-
stream
public void stream(@Nonnull com.google.api.gax.rpc.ApiStreamObserver<DocumentSnapshot> responseObserver) Executes the query and streams the results as a StreamObserver of DocumentSnapshots.- Parameters:
responseObserver- The observer to be notified when results arrive.
-
explainStream
@Nonnull public com.google.api.core.ApiFuture<ExplainMetrics> explainStream(@Nonnull ExplainOptions options, @Nonnull com.google.api.gax.rpc.ApiStreamObserver<DocumentSnapshot> documentObserver) Executes the query, streams the results as a StreamObserver of DocumentSnapshots, and returns an ApiFuture that will be resolved with the associatedExplainMetrics.- Parameters:
options- The options that configure the explain request.documentObserver- The observer to be notified every time a new document arrives.
-
toProto
Returns theRunQueryRequestthat this Query instance represents. The request contains the serialized form of all Query constraints.Runtime metadata (as required for `limitToLast()` queries) is not serialized and as such, the serialized request will return the results in the original backend order.
- Returns:
- the serialized RunQueryRequest
-
toRunQueryRequestBuilder
protected RunQueryRequest.Builder toRunQueryRequestBuilder(@Nullable com.google.protobuf.ByteString transactionId, @Nullable com.google.cloud.Timestamp readTime, @Nullable ExplainOptions explainOptions) -
fromProto
Returns a Query instance that can be used to execute the providedRunQueryRequest.Only RunQueryRequests that pertain to the same project as the Firestore instance can be deserialized.
Runtime metadata (as required for `limitToLast()` queries) is not restored and as such, the results for limitToLast() queries will be returned in the original backend order.
- Parameters:
firestore- a Firestore instance to apply the query toproto- the serialized RunQueryRequest- Returns:
- a Query instance that can be used to execute the RunQueryRequest
-
get
Executes the query and returns the results as QuerySnapshot.- Specified by:
getin classStreamableQuery<QuerySnapshot>- Returns:
- An ApiFuture that will be resolved with the results of the Query.
-
explain
@Nonnull public com.google.api.core.ApiFuture<ExplainResults<QuerySnapshot>> explain(ExplainOptions options) Plans and optionally executes this query. Returns an ApiFuture that will be resolved with the planner information, statistics from the query execution (if any), and the query results (if any).- Overrides:
explainin classStreamableQuery<QuerySnapshot>- Returns:
- An ApiFuture that will be resolved with the planner information, statistics from the query execution (if any), and the query results (if any).
-
addSnapshotListener
@Nonnull public ListenerRegistration addSnapshotListener(@Nonnull EventListener<QuerySnapshot> listener) Starts listening to this query.- Parameters:
listener- The event listener that will be called with the snapshots.- Returns:
- A registration object that can be used to remove the listener.
-
addSnapshotListener
@Nonnull public ListenerRegistration addSnapshotListener(@Nonnull Executor executor, @Nonnull EventListener<QuerySnapshot> listener) Starts listening to this query.- Parameters:
executor- The executor to use to call the listener.listener- The event listener that will be called with the snapshots.- Returns:
- A registration object that can be used to remove the listener.
-
count
Returns a query that counts the documents in the result set of this query.The returned query, when executed, counts the documents in the result set of this query without actually downloading the documents.
Using the returned query to count the documents is efficient because only the final count, not the documents' data, is downloaded. The returned query can count the documents in cases where the result set is prohibitively large to download entirely (thousands of documents).
- Returns:
- a query that counts the documents in the result set of this query.
-
aggregate
@Nonnull public AggregateQuery aggregate(@Nonnull AggregateField aggregateField1, @Nonnull AggregateField... aggregateFields) Calculates the specified aggregations over the documents in the result set of the given query without actually downloading the documents.Using the returned query to perform aggregations is efficient because only the final aggregation values, not the documents' data, is downloaded. The returned query can perform aggregations of the documents in cases where the result set is prohibitively large to download entirely (thousands of documents).
- Returns:
- an
AggregateQuerythat performs aggregations on the documents in the result set of this query.
-
findNearest
public VectorQuery findNearest(String vectorField, double[] queryVector, int limit, VectorQuery.DistanceMeasure distanceMeasure) Returns a VectorQuery that can perform vector distance (similarity) search with given parameters.The returned query, when executed, performs a distance (similarity) search on the specified `vectorField` against the given `queryVector` and returns the top documents that are closest to the `queryVector`.
Only documents whose `vectorField` field is a
VectorValueof the same dimension as `queryVector` participate in the query, all other documents are ignored.VectorQuery vectorQuery = col.findNearest("embedding", new double[] {41, 42}, 10, VectorQuery.DistanceMeasure.EUCLIDEAN); QuerySnapshot querySnapshot = await vectorQuery.get().get(); DocumentSnapshot mostSimilarDocument = querySnapshot.getDocuments().get(0);- Parameters:
vectorField- A string specifying the vector field to search on.queryVector- A representation of the vector used to measure the distance from `vectorField` values in the documents.limit- The upper bound of documents to return, must be a positive integer with a maximum value of 1000.distanceMeasure- What type of distance is calculated when performing the query. SeeVectorQuery.DistanceMeasure.- Returns:
- an
VectorQuerythat performs vector distance (similarity) search with the given parameters.
-
findNearest
public VectorQuery findNearest(String vectorField, double[] queryVector, int limit, VectorQuery.DistanceMeasure distanceMeasure, VectorQueryOptions vectorQueryOptions) Returns a VectorQuery that can perform vector distance (similarity) search with given parameters.The returned query, when executed, performs a distance (similarity) search on the specified `vectorField` against the given `queryVector` and returns the top documents that are closest to the `queryVector`.
Only documents whose `vectorField` field is a
VectorValueof the same dimension as `queryVector` participate in the query, all other documents are ignored.VectorQuery vectorQuery = col.findNearest( "embedding", new double[] {41, 42}, 10, VectorQuery.DistanceMeasure.EUCLIDEAN, FindNearestOptions.newBuilder().setDistanceThreshold(0.11).setDistanceResultField("foo").build()); QuerySnapshot querySnapshot = await vectorQuery.get().get(); DocumentSnapshot mostSimilarDocument = querySnapshot.getDocuments().get(0);- Parameters:
vectorField- A string specifying the vector field to search on.queryVector- A representation of the vector used to measure the distance from `vectorField` values in the documents.limit- The upper bound of documents to return, must be a positive integer with a maximum value of 1000.distanceMeasure- What type of distance is calculated when performing the query. SeeVectorQuery.DistanceMeasure.vectorQueryOptions- Optional arguments for VectorQueries, seeVectorQueryOptions.- Returns:
- an
VectorQuerythat performs vector distance (similarity) search with the given parameters.
-
findNearest
public VectorQuery findNearest(FieldPath vectorField, VectorValue queryVector, int limit, VectorQuery.DistanceMeasure distanceMeasure) Returns a VectorQuery that can perform vector distance (similarity) search with given parameters.The returned query, when executed, performs a distance (similarity) search on the specified `vectorField` against the given `queryVector` and returns the top documents that are closest to the `queryVector`.
Only documents whose `vectorField` field is a
VectorValueof the same dimension as `queryVector` participate in the query, all other documents are ignored.VectorValue queryVector = FieldValue.vector(new double[] {41, 42}); VectorQuery vectorQuery = col.findNearest( FieldPath.of("embedding"), queryVector, 10, VectorQuery.DistanceMeasure.EUCLIDEAN); QuerySnapshot querySnapshot = await vectorQuery.get().get(); DocumentSnapshot mostSimilarDocument = querySnapshot.getDocuments().get(0);- Parameters:
vectorField- AFieldPathspecifying the vector field to search on.queryVector- TheVectorValueused to measure the distance from `vectorField` values in the documents.limit- The upper bound of documents to return, must be a positive integer with a maximum value of 1000.distanceMeasure- What type of distance is calculated when performing the query. SeeVectorQuery.DistanceMeasure.- Returns:
- an
VectorQuerythat performs vector distance (similarity) search with the given parameters.
-
findNearest
public VectorQuery findNearest(FieldPath vectorField, VectorValue queryVector, int limit, VectorQuery.DistanceMeasure distanceMeasure, VectorQueryOptions vectorQueryOptions) Returns a VectorQuery that can perform vector distance (similarity) search with given parameters.The returned query, when executed, performs a distance (similarity) search on the specified `vectorField` against the given `queryVector` and returns the top documents that are closest to the `queryVector`.
Only documents whose `vectorField` field is a
VectorValueof the same dimension as `queryVector` participate in the query, all other documents are ignored.VectorQuery vectorQuery = col.findNearest( FieldPath.of("embedding"), queryVector, 10, VectorQuery.DistanceMeasure.EUCLIDEAN, FindNearestOptions.newBuilder().setDistanceThreshold(0.11).setDistanceResultField("foo").build()); QuerySnapshot querySnapshot = await vectorQuery.get().get(); DocumentSnapshot mostSimilarDocument = querySnapshot.getDocuments().get(0);- Parameters:
vectorField- AFieldPathspecifying the vector field to search on.queryVector- TheVectorValueused to measure the distance from `vectorField` values in the documents.limit- The upper bound of documents to return, must be a positive integer with a maximum value of 1000.distanceMeasure- What type of distance is calculated when performing the query. SeeVectorQuery.DistanceMeasure.vectorQueryOptions- Optional arguments for VectorQueries, seeVectorQueryOptions.- Returns:
- an
VectorQuerythat performs vector distance (similarity) search with the given parameters.
-
equals
Returns true if this Query is equal to the provided object. -
hashCode
public int hashCode()
-