Python 2.7 has reached end of support
and will be deprecated
on January 31, 2026. After deprecation, you won't be able to deploy Python 2.7
applications, even if your organization previously used an organization policy to
re-enable deployments of legacy runtimes. Your existing Python
2.7 applications will continue to run and receive traffic after their
deprecation date. We recommend that
you migrate to the latest supported version of Python.
Stay organized with collections
Save and categorize content based on your preferences.
Class QueryOptions provides options for post-processing query results based on the needs of your application. You construct the class as the options in the Query.options argument.
Query is defined in the google.appengine.api.search module.
Introduction
Class QueryOptions provides options for post-processing the results for a specific query. Options include the ability to sort results, control which document fields to return, produce snippets of fields and compute and sort by complex scoring expressions.
If you wish to randomly access pages of search results, you can use an offset:
fromgoogle.appengine.apiimportsearch...# get the first set of resultspage_size=10results=index.search(search.Query(query_string='some stuff',options=search.QueryOptions(limit=page_size))# calculate pagespages=results.number_found/page_size# user chooses page and hence an offset into resultsnext_page=ith*page_size# get the search results for that pageresults=index.search(search.Query(query_string='some stuff',options=search.QueryOptions(limit=page_size,offset=next_page))
For example, the following code fragment requests a search for documents where first occurs in the subject field and good occurs in any field, returning at most 20 documents, requesting the cursor for the next page of results, returning another cursor for the next set of results, sorting by subject in descending order, returning the author, subject, and summary fields as well as a snippeted field content:
The limit on number of documents to return in results.
number_found_accuracy
The minimum accuracy requirement for SearchResults.number_found. If set, remains accurate up to at least that number. For example, when set to 100, any SearchResults object with number_found_accuracy <= 100 is accurate.
cursor
A Cursor describing where to get the next set of results,
or to provide next cursors in SearchResults.
offset
The offset represents the number of documents to skip in search results. This is an alternative to using a query cursor. It allows random access to the results. Offsets are more expensive (in terms of instance hours) than cursors. You can use either cursor or offset, but not both. Using an offset means that no cursor is returned in ScoredDocument.cursor or ScoredDocument.cursor.
sort_options
A SortOptions object specifying a multi-dimensional sort over search results.
returned_fields
An iterable of names of fields to return in search
results.
ids_only
Only return document ids, do not return any fields.
snippeted_fields
An iterable of names of fields to snippet and return
in search result expressions.
returned_expressions
An iterable of FieldExpression to evaluate and
return in search results.
Result value
A new instance of class QueryOptions.
Exceptions
TypeError
If an unknown iterator_options or sort_options is passed.
ValueError
If ids_only and returned_fields are used together.
Properties
An instance of class Query has the following properties:
limit
Returns a limit on number of documents to return in results.
[[["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-08-29 UTC."],[[["\u003cp\u003e\u003ccode\u003eQueryOptions\u003c/code\u003e class allows for post-processing of search query results, including sorting, field selection, snippet generation, and complex scoring expressions.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003elimit\u003c/code\u003e parameter in \u003ccode\u003eQueryOptions\u003c/code\u003e controls the maximum number of documents returned in the search results.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eoffset\u003c/code\u003e parameter enables random access to pages of search results by skipping a specified number of documents, offering an alternative to using query cursors.\u003c/p\u003e\n"],["\u003cp\u003eYou can specify which document fields to return using the \u003ccode\u003ereturned_fields\u003c/code\u003e parameter and which fields to snippet using \u003ccode\u003esnippeted_fields\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003enumber_found_accuracy\u003c/code\u003e parameter can be used to specify the minimum accuracy for \u003ccode\u003eSearchResults.number_found\u003c/code\u003e, however it can introduce significant latency.\u003c/p\u003e\n"]]],[],null,["# The QueryOptions Class\n\nClass `QueryOptions` provides options for post-processing query results based on the needs of your application. You construct the class as the `options` in the [Query.options](/appengine/docs/legacy/standard/python/search/queryclass) argument.\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| python3\n|\n| /services/access). If you are updating to the App Engine Python 3 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/python-differences) to learn about your migration options for legacy bundled services.\n\n`Query` is defined in the `google.appengine.api.search` module.\n\nIntroduction\n------------\n\nClass `QueryOptions` provides options for post-processing the results for a specific query. Options include the ability to sort results, control which document fields to return, produce snippets of fields and compute and sort by complex scoring expressions.\n\nIf you wish to randomly access pages of search results, you can use an offset: \n\n```python\nfrom google.appengine.api import search\n...\n# get the first set of results\npage_size = 10\nresults = index.search(search.Query(query_string='some stuff',\n options=search.QueryOptions(limit=page_size))\n\n# calculate pages\npages = results.number_found / page_size\n\n# user chooses page and hence an offset into results\nnext_page = ith * page_size\n\n# get the search results for that page\nresults = index.search(search.Query(query_string='some stuff',\n options=search.QueryOptions(limit=page_size, offset=next_page))\n```\n\nFor example, the following code fragment requests a search for documents where `first` occurs in the `subject` field and `good` occurs in any field, returning at most 20 documents, requesting the cursor for the next page of results, returning another cursor for the next set of results, sorting by subject in descending order, returning the author, subject, and summary fields as well as a snippeted field content: \n\n```python\n...\nresults = index.search(search.Query(\n query='subject:first good',\n options=search.QueryOptions(\n limit=20,\n cursor=search.Cursor(),\n sort_options=search.SortOptions(\n expressions=[\n search.SortExpression(expression='subject', default_value='')],\n limit=1000),\n returned_fields=['author', 'subject', 'summary'],\n snippeted_fields=['content'])))\n```\n\nConstructor\n-----------\n\nThe constructor for class `QueryOptions` is defined as follows:\n\n\nclass QueryOptions(limit=20, number_found_accuracy=None, cursor=None, offset=None, sort_options=None, returned_fields=None, ids_only=False, snippeted_fields=None, returned_expressions=None)\n\n: Specify options defining search query results..\n\n: Arguments\n\n limit\n\n : The limit on number of documents to return in results.\n\n number_found_accuracy\n\n : The minimum accuracy requirement for `SearchResults.number_found`. If set, remains accurate up to at least that number. For example, when set to 100, any `SearchResults` object with `number_found_accuracy` \\\u003c= 100 is accurate.\n\n | **Caution!** This option may add considerable latency/expense, especially when used with `returned_fields`.\n\n cursor\n\n : A Cursor describing where to get the next set of results,\n or to provide next cursors in SearchResults.\n\n offset\n\n : The offset represents the number of documents to skip in search results. This is an alternative to using a query cursor. It allows random access to the results. Offsets are more expensive (in terms of [instance hours](/appengine/docs/pricing)) than cursors. You can use either cursor or offset, but not both. Using an offset means that no cursor is returned in [`ScoredDocument.cursor`](/appengine/docs/legacy/standard/python/search/searchresultsclass) or `ScoredDocument.cursor`.\n\n sort_options\n\n : A [SortOptions](/appengine/docs/legacy/standard/python/search/sortoptionsclass) object specifying a multi-dimensional sort over search results.\n\n returned_fields\n\n : An iterable of names of fields to return in search\n results.\n\n ids_only\n\n : Only return document ids, do not return any fields.\n\n snippeted_fields\n\n : An iterable of names of fields to snippet and return\n in search result expressions.\n\n returned_expressions\n\n : An iterable of FieldExpression to evaluate and\n return in search results.\n\n Result value\n\n : A new instance of class `QueryOptions`.\n\n Exceptions\n\n TypeError\n\n : If an unknown iterator_options or sort_options is passed.\n\n ValueError\n\n : If `ids_only` and `returned_fields` are used together.\n\n \u003cbr /\u003e\n\nProperties\n----------\n\nAn instance of class `Query` has the following properties:\n\nlimit\n\n: Returns a limit on number of documents to return in results.\n\nnumber_found_accuracy\n\n: Returns minimum accuracy requirement for [SearchResults.number_found](/appengine/docs/legacy/standard/python/search/searchresultsclass).\n\ncursor\n\n: Returns the cursor for the query.\n\noffset\n\n: Returns the number of documents in search results to skip.\n\nsort_options\n\n: Returns a [SortOptions](/appengine/docs/legacy/standard/python/search/sortoptionsclass) object.\n\nreturned_fields\n\n: Returns an iterable of names of fields to return in search results.\n\nids_only\n\n: Returns only ` ` in search results.\n\nsnippeted_fields\n\n: Returns iterable of field names to snippet and return in results.\n\nreturned_expressions\n\n: Returns iterable of [FieldExpression](/appengine/docs/legacy/standard/python/search/fieldexpressionclass) to return in results."]]