defquery_offset(index,query_string):offset=0whileTrue:# Build the query using the current offset.options=search.QueryOptions(offset=offset)query=search.Query(query_string=query_string,options=options)# Get the resultsresults=index.search(query)number_retrieved=len(results.results)ifnumber_retrieved==0:break# Add the number of documents found to the offset, so that the next# iteration will grab the next page of documents.offset+=number_retrieved# Process the matched documentsfordocumentinresults:print(document)
defquery_cursor(index,query_string):cursor=search.Cursor()whilecursor:# Build the query using the cursor.options=search.QueryOptions(cursor=cursor)query=search.Query(query_string=query_string,options=options)# Get the results and the next cursorresults=index.search(query)cursor=results.cursorfordocumentinresults:print(document)
defquery_per_document_cursor(index,query_string):cursor=search.Cursor(per_result=True)# Build the query using the cursor.options=search.QueryOptions(cursor=cursor)query=search.Query(query_string=query_string,options=options)# Get the results.results=index.search(query)document_cursor=Nonefordocumentinresults:# discover some document of interest and grab its cursor, for this# sample we'll just use the first document.document_cursor=document.cursorbreak# Start the next search from the document of interest.ifdocument_cursorisNone:returnoptions=search.QueryOptions(cursor=document_cursor)query=search.Query(query_string=query_string,options=options)results=index.search(query)fordocumentinresults:print(document)
儲存和還原游標
您可將游標序列化為網頁安全字串並加以儲存,然後還原以供日後使用:
defsaving_and_restoring_cursor(cursor):# Convert the cursor to a web-safe string.cursor_string=cursor.web_safe_string# Restore the cursor from a web-safe string.cursor=search.Cursor(web_safe_string=cursor_string)
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-14 (世界標準時間)。"],[[["A completed query returns a `SearchResults` object, detailing the total matching documents and the number returned, including a list of `ScoredDocuments`."],["The number of documents returned might be less than the total found, as searches return a limited number, but all can be retrieved using offsets or cursors."],["`ScoredDocuments` include fields from the original indexed document and any specified computed fields from the query options."],["Offsets allow iterating through matching documents by specifying a starting point, while cursors offer a more efficient way to retrieve results across consecutive pages without missing documents."],["Cursors can be per-query or per-result, with per-query holding the position of the last returned document, and per-result associating a cursor with each document; these cursors can be serialized and restored using web-safe strings."]]],[]]