The Cursor Class Stay organized with collections Save and categorize content based on your preferences.
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.
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 class
Cursor.Arguments
- per_result
When true, returns a cursor per ScoredDocument in SearchResults. When false, returns a single cursor for all of SearchResults. Ignored if using
QueryOptions.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 class
Cursor.
Exceptions
- ValueError
If the
web_safe_stringprovided 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.