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 Cursor Class

TheCursor class provides a cursor in the current set search results, allowing you to retrieve the next set based on criteria that you specify. Using cursors improves the performance and consistency of pagination as indexes are updated.

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.

The following shows how to use the cursor to get the next page of results:

# Get the first set of results, and return a cursor with that result set.# The first cursor tells the API to return cursors in the SearchResults object.results=index.search(search.Query(query_string='some stuff',options=search.QueryOptions(cursor=search.Cursor()))# Get the next set of results with a cursor.results=index.search(search.Query(query_string='some stuff',options=search.QueryOptions(cursor=results.cursor)))

If you want to continue search from any one of theScoredDocuments inSearchResults, setCursor.per_result toTrue:

# get the first set of results, the first cursor is used to specify# that cursors are to be returned in the SearchResults.results=index.search(search.Query(query_string='some stuff',options=search.QueryOptions(cursor=Cursor(per_result=True)))# this shows how to access the per_document cursors returned from a searchper_document_cursor=Noneforscored_documentinresults:per_document_cursor=scored_document.cursor# get the next set of resultsresults=index.search(search.Query(query_string='some stuff',options=search.QueryOptions(cursor=per_document_cursor)))

The cursor can be cached as a web safe string that can be used to reconstruct the Cursor. For example,

next_cursor=results.cursornext_cursor_url_safe=next_cursor.web_safe_string//savenext_cursor_url_safe...//extractnext_cursor_url_saferesults=index.search(search.Query(query_string,cursor=search.Cursor(web_safe_string=next_cursor_url_safe)))

Constructor

The constructor for classCursor is defined as follows:

class Cursor(web_safe_string=None,per_result=False)

Construct an instance of classCursor.

Arguments

per_result

When true, returns a cursor per ScoredDocument in SearchResults. When false, returns a single cursor for all of SearchResults. Ignored if usingQueryOptions.offset, as the user is responsible for calculating a next offset (if any).

web_safe_string

The cursor string returned from the search service to be interpreted by the search service to get the next set of results.

Result value

A new instance of classCursor.

Exceptions

ValueError

If theweb_safe_string provided by the API is not of required format.

Properties

An instance of classCursor has the following properties:

per_result

Returns whether to return a cursor for each ScoredDocument in results.

web_safe_string

Returns the cursor string generated by the search service.

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.