Cursor 類別提供目前此組搜尋結果的游標,可讓您依據您指定的條件擷取下一組搜尋結果。隨著索引的更新,使用游標可以提高分頁的效能與一致性。
以下說明如何使用游標取得下一個頁面的結果:
# 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)))
如果您想從 SearchResults 中的任何一個 ScoredDocuments 繼續搜尋,請將 Cursor.per_result 設為 True:
# 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 search per_document_cursor = None for scored_document in results: per_document_cursor = scored_document.cursor # get the next set of results results = index.search(search.Query(query_string='some stuff', options=search.QueryOptions(cursor=per_document_cursor)))
游標可以被快取為網頁安全字串,用於重新建構游標。例如,
next_cursor = results.cursor next_cursor_url_safe = next_cursor.web_safe_string // save next_cursor_url_safe ... // extract next_cursor_url_safe results = index.search( search.Query(query_string, cursor=search.Cursor(web_safe_string=next_cursor_url_safe)))
建構函式
Cursor 類別的建構函式定義如下:
- 
      class Cursor(web_safe_string=None, per_result=False) 
- 建構 - Cursor類別的例項。- 引數 - per_result
- 若為 true,則會在 SearchResults 中的每個 ScoredDocument 傳回一個游標。若為 false,則會傳回所有 SearchResults 的單一游標。如使用 - QueryOptions.offset則請忽略,因為使用者負責計算下一個偏移量 (如果有)。
- web_safe_string
- 從搜尋服務傳回游標字串,由搜尋服務解釋以取得下一組結果。 
 - 結果值 - Cursor類別的新例項。
 - 例外狀況 - ValueError
- 如果 API 提供的 - web_safe_string非所需的格式。
 
屬性
Cursor 類別的執行個體具有下列屬性:
- per_result
- 傳回結果中的每個 ScoredDocument 是否要傳回游標。 
- web_safe_string
- 傳回由搜尋服務產生的游標字串。