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)
保存和恢复游标
您可以将游标序列化为一个 Web 安全字符串,然后进行保护和恢复以供日后使用:
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"]],["最后更新时间 (UTC):2025-03-06。"],[[["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."]]],[]]