Python 2.7 has reached end of supportand will bedeprecatedon January 31, 2026. After deprecation, you won't be able to deploy Python 2.7applications, even if your organization previously used an organization policy tore-enable deployments of legacy runtimes. Your existing Python2.7 applications will continue to run and receive traffic after theirdeprecation date. We recommend thatyoumigrate to the latest supported version of Python.

The QueryOptions Class

ClassQueryOptions provides options for post-processing query results based on the needs of your application. You construct the class as theoptions in theQuery.options argument.

This API is supported for first-generation runtimes and can be used whenupgrading to corresponding second-generation runtimes. If you are updating to the App Engine Python 3 runtime, refer to themigration guide to learn about your migration options for legacy bundled services.

Query is defined in thegoogle.appengine.api.search module.

Introduction

ClassQueryOptions 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 wherefirst occurs in thesubject field andgood 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:

...results=index.search(search.Query(query='subject:first good',options=search.QueryOptions(limit=20,cursor=search.Cursor(),sort_options=search.SortOptions(expressions=[search.SortExpression(expression='subject',default_value='')],limit=1000),returned_fields=['author','subject','summary'],snippeted_fields=['content'])))

Constructor

The constructor for classQueryOptions is defined as follows:

class 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)

Specify options defining search query results..

Arguments

limit

The limit on number of documents to return in results.

number_found_accuracy

The minimum accuracy requirement forSearchResults.number_found. If set, remains accurate up to at least that number. For example, when set to 100, anySearchResults object withnumber_found_accuracy<= 100 is accurate.

Caution! This option may add considerable latency/expense, especially when used withreturned_fields.
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 ofinstance hours) than cursors. You can use either cursor or offset, but not both. Using an offset means that no cursor is returned inScoredDocument.cursor orScoredDocument.cursor.

sort_options

ASortOptions 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 classQueryOptions.

Exceptions

TypeError

If an unknown iterator_options or sort_options is passed.

ValueError

Ifids_only andreturned_fields are used together.

Properties

An instance of classQuery has the following properties:

limit

Returns a limit on number of documents to return in results.

number_found_accuracy

Returns minimum accuracy requirement forSearchResults.number_found.

cursor

Returns the cursor for the query.

offset

Returns the number of documents in search results to skip.

sort_options

Returns aSortOptions object.

returned_fields

Returns an iterable of names of fields to return in search results.

ids_only

Returns only in search results.

snippeted_fields

Returns iterable of field names to snippet and return in results.

returned_expressions

Returns iterable ofFieldExpression to return in results.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.