Java 8 has reached end of support
and will be
deprecated
on January 31, 2026. After deprecation, you won't be able to deploy Java 8
applications, even if your organization previously used an organization policy to
re-enable deployments of legacy runtimes. Your existing Java
8 applications will continue to run and receive traffic after their
deprecation date. We recommend that
you
migrate to the latest supported version of Java.
QueryOptions.Builder (Google App Engine API for Java)
Stay organized with collections
Save and categorize content based on your preferences.
- java.lang.Object
-
- com.google.appengine.api.search.QueryOptions.Builder
-
-
Method Summary
-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
setLimit
public QueryOptions.Builder setLimit(int limit)
Sets the limit on the number of documents to return in
Results
.
- Parameters:
limit
- the number of documents to return
- Returns:
- this Builder
- Throws:
java.lang.IllegalArgumentException
- if numDocumentsToReturn is
not within acceptable range
-
setCursor
public QueryOptions.Builder setCursor(Cursor cursor)
Sets the cursor. The cursor is obtained from either a
Results
or one of the individual
ScoredDocuments
.
This is illustrated from the following code fragment:
Cursor cursor = Cursor.newBuilder().build();
SearchResults results = index.search(
Query.newBuilder()
.setOptions(QueryOptions.newBuilder()
.setLimit(20)
.setCursor(cursor)
.build())
.build("some query"));
// If the Cursor is built without setPerResult(true), then
// by default a single Cursor
is returned with the
// Results
.
cursor = results.getCursor();
for (ScoredDocument result : results) {
// If you set Cursor.newBuilder().setPerResult(true)
// then a cursor is returned with each result.
result.getCursor();
- Parameters:
cursor
- use a cursor returned from a
previous set of search results as a starting point to retrieve
the next set of results. This can get you better performance, and
also improves the consistency of pagination through index updates
- Returns:
- this Builder
-
setNumberFoundAccuracy
public QueryOptions.Builder setNumberFoundAccuracy(int numberFoundAccuracy)
Sets the accuracy requirement for
Results.getNumberFound()
. If set,
getNumberFound()
will be accurate up to at least that number.
For example, when set to 100, any
getNumberFound() <= 100
is
accurate. This option may add considerable latency / expense, especially
when used with
setFieldsToReturn(String...)
.
- Parameters:
numberFoundAccuracy
- the minimum accuracy requirement
- Returns:
- this Builder
- Throws:
java.lang.IllegalArgumentException
- if the accuracy is not within
acceptable range
-
setFieldsToSnippet
public QueryOptions.Builder setFieldsToSnippet(java.lang.String... fieldsToSnippet)
- Parameters:
fieldsToSnippet
- the names of fields to snippet in results
- Returns:
- this Builder
- Throws:
java.lang.IllegalArgumentException
- if any of the field names is invalid
-
addExpressionToReturn
public QueryOptions.Builder addExpressionToReturn(FieldExpression.Builder expressionBuilder)
- Parameters:
expressionBuilder
- a builder of named expressions to
evaluate and return in results
- Returns:
- this Builder
-
setReturningIdsOnly
public QueryOptions.Builder setReturningIdsOnly(boolean idsOnly)
- Parameters:
idsOnly
- whether or not only IDs of documents are returned by search request
- Returns:
- this Builder
-
addExpressionToReturn
public QueryOptions.Builder addExpressionToReturn(FieldExpression expression)
- Parameters:
expression
- a named expression to compute and return in results
- Returns:
- this Builder
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-06-16 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-16 UTC."],[[["\u003cp\u003e\u003ccode\u003eQueryOptions.Builder\u003c/code\u003e is a class for constructing \u003ccode\u003eQueryOptions\u003c/code\u003e objects to refine search result behavior.\u003c/p\u003e\n"],["\u003cp\u003eYou can specify the number of documents to return in results using \u003ccode\u003esetLimit()\u003c/code\u003e, with an offset set by \u003ccode\u003esetOffset()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eCursors can be set for pagination and performance using \u003ccode\u003esetCursor()\u003c/code\u003e to resume from a previous search result.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eQueryOptions.Builder\u003c/code\u003e allows for precise control over returned data, such as specific field selection with \u003ccode\u003esetFieldsToReturn()\u003c/code\u003e, snippets with \u003ccode\u003esetFieldsToSnippet()\u003c/code\u003e, or returning only document IDs with \u003ccode\u003esetReturningIdsOnly()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eYou can manipulate and set the accuracy requirement for \u003ccode\u003eResults.getNumberFound()\u003c/code\u003e with \u003ccode\u003esetNumberFoundAccuracy()\u003c/code\u003e and \u003ccode\u003eclearNumberFoundAccuracy()\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# QueryOptions.Builder (Google App Engine API for Java)\n\ncom.google.appengine.api.search\n\nClass QueryOptions.Builder\n--------------------------\n\n- java.lang.Object\n-\n - com.google.appengine.api.search.QueryOptions.Builder\n\n-\n\n Enclosing class:\n : [QueryOptions](../../../../../com/google/appengine/api/search/QueryOptions.html \"class in com.google.appengine.api.search\")\n\n *** ** * ** ***\n\n \u003cbr /\u003e\n\n ```\n public static final class QueryOptions.Builder\n extends java.lang.Object\n ``` \nA builder which constructs QueryOptions objects. \n-\n -\n\n ### Method Summary\n\n -\n\n ### Methods inherited from class java.lang.Object\n\n`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait` \n-\n -\n\n ### Method Detail\n\n\n -\n\n #### setLimit\n\n ```\n public QueryOptions.Builder setLimit(int limit)\n ``` \n Sets the limit on the number of documents to return in [`Results`](../../../../../com/google/appengine/api/search/Results.html \"class in com.google.appengine.api.search\").\n\n Parameters:\n : `limit` - the number of documents to return\n\n Returns:\n : this Builder\n\n Throws:\n : `java.lang.IllegalArgumentException` - if numDocumentsToReturn is\n not within acceptable range\n\n\n -\n\n #### setCursor\n\n ```\n public QueryOptions.Builder setCursor(Cursor cursor)\n ``` \n Sets the cursor. The cursor is obtained from either a [`Results`](../../../../../com/google/appengine/api/search/Results.html \"class in com.google.appengine.api.search\") or one of the individual [`ScoredDocuments`](../../../../../com/google/appengine/api/search/ScoredDocument.html \"class in com.google.appengine.api.search\"). This is illustrated from the following code fragment:\n\n\n ```\n Cursor cursor = Cursor.newBuilder().build();\n\n SearchResults results = index.search(\n Query.newBuilder()\n .setOptions(QueryOptions.newBuilder()\n .setLimit(20)\n .setCursor(cursor)\n .build())\n .build(\"some query\"));\n\n // If the Cursor is built without setPerResult(true), then\n // by default a single `Cursor` is returned with the\n // `Results`.\n cursor = results.getCursor();\n\n for (ScoredDocument result : results) {\n // If you set Cursor.newBuilder().setPerResult(true)\n // then a cursor is returned with each result.\n result.getCursor();\n \n ```\n\n \u003cbr /\u003e\n\n Parameters:\n : `cursor` - use a cursor returned from a\n previous set of search results as a starting point to retrieve\n the next set of results. This can get you better performance, and\n also improves the consistency of pagination through index updates\n\n Returns:\n : this Builder\n\n\n -\n\n #### setCursor\n\n ```\n public QueryOptions.Builder setCursor(Cursor.Builder cursorBuilder)\n ``` \n Sets a cursor built from the builder.\n\n Parameters:\n : `cursorBuilder` - a [`Cursor.Builder`](../../../../../com/google/appengine/api/search/Cursor.Builder.html \"class in com.google.appengine.api.search\") that is used to build\n a [`Cursor`](../../../../../com/google/appengine/api/search/Cursor.html \"class in com.google.appengine.api.search\").\n\n Returns:\n : this Builder\n\n See Also:\n : [`setCursor(Cursor)`](../../../../../com/google/appengine/api/search/QueryOptions.Builder.html#setCursor-com.google.appengine.api.search.Cursor-)\n\n\n -\n\n #### setOffset\n\n ```\n public QueryOptions.Builder setOffset(int offset)\n ``` \n Sets the offset of the first result to return.\n\n Parameters:\n : `offset` - the offset into all search results to return the limit\n amount of results\n\n Returns:\n : this Builder\n\n Throws:\n : `java.lang.IllegalArgumentException` - if the offset is negative or is larger\n than [`SearchApiLimits.SEARCH_MAXIMUM_OFFSET`](../../../../../com/google/appengine/api/search/checkers/SearchApiLimits.html#SEARCH_MAXIMUM_OFFSET)\n\n\n -\n\n #### setNumberFoundAccuracy\n\n ```\n public QueryOptions.Builder setNumberFoundAccuracy(int numberFoundAccuracy)\n ``` \n Sets the accuracy requirement for [`Results.getNumberFound()`](../../../../../com/google/appengine/api/search/Results.html#getNumberFound--). If set, `getNumberFound()` will be accurate up to at least that number. For example, when set to 100, any `getNumberFound() \u003c= 100` is accurate. This option may add considerable latency / expense, especially when used with [`setFieldsToReturn(String...)`](../../../../../com/google/appengine/api/search/QueryOptions.Builder.html#setFieldsToReturn-java.lang.String...-).\n\n Parameters:\n : `numberFoundAccuracy` - the minimum accuracy requirement\n\n Returns:\n : this Builder\n\n Throws:\n : `java.lang.IllegalArgumentException` - if the accuracy is not within\n acceptable range\n\n\n -\n\n #### clearNumberFoundAccuracy\n\n ```\n public QueryOptions.Builder clearNumberFoundAccuracy()\n ``` \n Clears any accuracy requirement for [`Results.getNumberFound()`](../../../../../com/google/appengine/api/search/Results.html#getNumberFound--).\n\n\n -\n\n #### setFieldsToReturn\n\n ```\n public QueryOptions.Builder setFieldsToReturn(java.lang.String... fields)\n ``` \n Specifies one or more fields to return in results.\n\n Parameters:\n : `fields` - the names of fields to return in results\n\n Returns:\n : this Builder\n\n Throws:\n : `java.lang.IllegalArgumentException` - if any of the field names is invalid\n\n\n -\n\n #### setFieldsToSnippet\n\n ```\n public QueryOptions.Builder setFieldsToSnippet(java.lang.String... fieldsToSnippet)\n ``` \n Specifies one or more fields to snippet in results. Snippets will be returned as fields with the same names in [`ScoredDocument.getExpressions()`](../../../../../com/google/appengine/api/search/ScoredDocument.html#getExpressions--).\n\n Parameters:\n : `fieldsToSnippet` - the names of fields to snippet in results\n\n Returns:\n : this Builder\n\n Throws:\n : `java.lang.IllegalArgumentException` - if any of the field names is invalid\n\n\n -\n\n #### addExpressionToReturn\n\n ```\n public QueryOptions.Builder addExpressionToReturn(FieldExpression.Builder expressionBuilder)\n ``` \n Adds a [`FieldExpression`](../../../../../com/google/appengine/api/search/FieldExpression.html \"class in com.google.appengine.api.search\") build from the given `expressionBuilder` to return in search results. Snippets will be returned as fields with the same names in [`ScoredDocument.getExpressions()`](../../../../../com/google/appengine/api/search/ScoredDocument.html#getExpressions--).\n\n Parameters:\n : `expressionBuilder` - a builder of named expressions to\n evaluate and return in results\n\n Returns:\n : this Builder\n\n\n -\n\n #### setReturningIdsOnly\n\n ```\n public QueryOptions.Builder setReturningIdsOnly(boolean idsOnly)\n ``` \n Sets whether or not the search should return documents or document IDs only. This setting is incompatible with [`addExpressionToReturn(FieldExpression)`](../../../../../com/google/appengine/api/search/QueryOptions.Builder.html#addExpressionToReturn-com.google.appengine.api.search.FieldExpression-) and with [`setFieldsToReturn(String...)`](../../../../../com/google/appengine/api/search/QueryOptions.Builder.html#setFieldsToReturn-java.lang.String...-) methods.\n\n Parameters:\n : `idsOnly` - whether or not only IDs of documents are returned by search request\n\n Returns:\n : this Builder\n\n\n -\n\n #### addExpressionToReturn\n\n ```\n public QueryOptions.Builder addExpressionToReturn(FieldExpression expression)\n ``` \n Adds a [`FieldExpression`](../../../../../com/google/appengine/api/search/FieldExpression.html \"class in com.google.appengine.api.search\") to return in search results.\n\n Parameters:\n : `expression` - a named expression to compute and return in results\n\n Returns:\n : this Builder\n\n\n -\n\n #### setSortOptions\n\n ```\n public QueryOptions.Builder setSortOptions(SortOptions sortOptions)\n ``` \n Sets a [`SortOptions`](../../../../../com/google/appengine/api/search/SortOptions.html \"class in com.google.appengine.api.search\") to sort documents with.\n\n Parameters:\n : `sortOptions` - specifies how to sort the documents in [`Results`](../../../../../com/google/appengine/api/search/Results.html \"class in com.google.appengine.api.search\")\n\n Returns:\n : this Builder\n\n\n -\n\n #### setSortOptions\n\n ```\n public QueryOptions.Builder setSortOptions(SortOptions.Builder builder)\n ``` \n Sets a [`SortOptions`](../../../../../com/google/appengine/api/search/SortOptions.html \"class in com.google.appengine.api.search\") using a builder.\n\n Parameters:\n : `builder` - a builder of a [`SortOptions`](../../../../../com/google/appengine/api/search/SortOptions.html \"class in com.google.appengine.api.search\")\n\n Returns:\n : this Builder\n\n\n -\n\n #### build\n\n ```\n public QueryOptions build()\n ``` \n Construct the final message.\n\n Returns:\n : the QueryOptions built from the parameters entered on this\n Builder\n\n Throws:\n : `java.lang.IllegalArgumentException` - if the search request is invalid"]]