public static Query fromProto(Firestore firestore, RunQueryRequest proto)
Returns a Query instance that can be used to execute the provided RunQueryRequest.
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
Name
Description
firestore
Firestorea Firestore instance to apply the query to
public AggregateQuery 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_.
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).
an AggregateQuery that performs aggregations on the documents in the result set of this query.
count()
public AggregateQuery 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).
a query that counts the documents in the result set of this query.
endAt(DocumentSnapshot snapshot)
public Query endAt(DocumentSnapshot snapshot)
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.
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.
Parameter
Name
Description
fieldValues
Object[]The field values to end this query at, in order of the query's order by.
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.
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.
Parameter
Name
Description
fieldValues
Object[]The field values to end this query before, in order of the query's order by.
Whether this Query is equal to the provided object.
Overrides
explain(ExplainOptions options)
public 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).
public ApiFuture<ExplainMetrics> explainStream(ExplainOptions options, ApiStreamObserver<DocumentSnapshot> documentObserver)
Executes the query, streams the results as a StreamObserver of DocumentSnapshots, and returns an ApiFuture that will be resolved with the associated ExplainMetrics.
Parameters
Name
Description
options
ExplainOptionsThe options that configure the explain request.
findNearest(FieldPath vectorField, VectorValue queryVector, int limit, VectorQuery.DistanceMeasure distanceMeasure)
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 specifiedvectorField against the given queryVector and returns the top documents that are closest to the queryVector.
Only documents whose vectorField field is a VectorValue of the same dimension asqueryVector participate in the query, all other documents are ignored.
an VectorQuery that performs vector distance (similarity) search with the given parameters.
findNearest(FieldPath vectorField, VectorValue queryVector, int limit, VectorQuery.DistanceMeasure distanceMeasure, VectorQueryOptions vectorQueryOptions)
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 specifiedvectorField against the given queryVector and returns the top documents that are closest to the queryVector.
Only documents whose vectorField field is a VectorValue of the same dimension asqueryVector participate in the query, all other documents are ignored.
an VectorQuery that performs vector distance (similarity) search with the given parameters.
findNearest(String vectorField, double[] queryVector, int limit, VectorQuery.DistanceMeasure distanceMeasure)
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 specifiedvectorField against the given queryVector and returns the top documents that are closest to the queryVector.
Only documents whose vectorField field is a VectorValue of the same dimension asqueryVector participate in the query, all other documents are ignored.
an VectorQuery that performs vector distance (similarity) search with the given parameters.
findNearest(String vectorField, double[] queryVector, int limit, VectorQuery.DistanceMeasure distanceMeasure, VectorQueryOptions vectorQueryOptions)
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 specifiedvectorField against the given queryVector and returns the top documents that are closest to the queryVector.
Only documents whose vectorField field is a VectorValue of the same dimension asqueryVector participate in the query, all other documents are ignored.
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 java.lang.IllegalStateException is 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.
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.
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.
public Query startAfter(DocumentSnapshot snapshot)
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.
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.
Parameter
Name
Description
fieldValues
Object[]The field values to start this query after, in order of the query's order by.
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.
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.
Parameter
Name
Description
fieldValues
Object[]The field values to start this query at, in order of the query's order by.
Returns the RunQueryRequest that 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.
public Query 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.
A Query can have only one whereArrayContains() filter and it cannot be combined with whereArrayContainsAny().
Parameters
Name
Description
fieldPath
FieldPathThe path of the field containing an array to search
value
ObjectThe value that must be contained in the array
public Query 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.
A Query can have only one whereArrayContains() filter and it cannot be combined with whereArrayContainsAny().
Parameters
Name
Description
field
StringThe name of the field containing an array to search
value
ObjectThe value that must be contained in the array
public Query 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.
A Query can have only one whereArrayContainsAny() filter and it cannot be combined with whereArrayContains() or whereIn().
Parameters
Name
Description
fieldPath
FieldPathThe path of the field containing an array to search.
values
List<? extends java.lang.Object>A list that contains the values to match.
public Query 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.
A Query can have only one whereArrayContainsAny() filter and it cannot be combined with whereArrayContains() or whereIn().
Parameters
Name
Description
field
StringThe name of the field containing an array to search.
values
List<? extends java.lang.Object>A list that contains the values to match.
public Query 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.
public Query 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.
public Query 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.
public Query 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.
public Query 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.
public Query 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.
public Query whereIn(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 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().
public Query whereIn(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 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().
public Query 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.
public Query 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.
public Query 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.
public Query 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.
public Query 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.
public Query 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.
public Query 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.
A Query can have only one whereNotIn() filter, and it cannot be combined with whereArrayContains(), whereArrayContainsAny(), whereIn(), or whereNotEqualTo().
public Query 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.
A Query can have only one whereNotIn() filter and it cannot be combined with whereArrayContains(), whereArrayContainsAny(), whereIn(), or whereNotEqualTo().