fromgoogle.appengine.apiimportsearch...# get the first set of resultspage_size=10results=index.search(search.Query(query_string='some stuff',options=search.QueryOptions(limit=page_size))# calculate pagespages=results.number_found/page_size# user chooses page and hence an offset into resultsnext_page=ith*page_size# get the search results for that pageresults=index.search(search.Query(query_string='some stuff',options=search.QueryOptions(limit=page_size,offset=next_page))
舉例來說,下列程式碼片段會要求搜尋 subject 欄位中出現 first 且任何欄位中出現 good 的文件,並傳回最多 20 個文件、要求下一個結果頁面的游標、傳回下一個結果集的另一個游標、依主題以降冪排序,傳回作者、主題和摘要欄位,以及程式碼片段欄位內容:
[[["容易理解","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-09-04 (世界標準時間)。"],[[["\u003cp\u003e\u003ccode\u003eQueryOptions\u003c/code\u003e class allows for post-processing of search query results, including sorting, field selection, snippet generation, and complex scoring expressions.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003elimit\u003c/code\u003e parameter in \u003ccode\u003eQueryOptions\u003c/code\u003e controls the maximum number of documents returned in the search results.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eoffset\u003c/code\u003e parameter enables random access to pages of search results by skipping a specified number of documents, offering an alternative to using query cursors.\u003c/p\u003e\n"],["\u003cp\u003eYou can specify which document fields to return using the \u003ccode\u003ereturned_fields\u003c/code\u003e parameter and which fields to snippet using \u003ccode\u003esnippeted_fields\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003enumber_found_accuracy\u003c/code\u003e parameter can be used to specify the minimum accuracy for \u003ccode\u003eSearchResults.number_found\u003c/code\u003e, however it can introduce significant latency.\u003c/p\u003e\n"]]],[],null,["# The QueryOptions Class\n\nClass `QueryOptions` provides options for post-processing query results based on the needs of your application. You construct the class as the `options` in the [Query.options](/appengine/docs/legacy/standard/python/search/queryclass) argument.\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| python3\n|\n| /services/access). If you are updating to the App Engine Python 3 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/python-differences) to learn about your migration options for legacy bundled services.\n\n`Query` is defined in the `google.appengine.api.search` module.\n\nIntroduction\n------------\n\nClass `QueryOptions` provides options for post-processing the results for a specific query. Options include the ability to sort results, control which document fields to return, produce snippets of fields and compute and sort by complex scoring expressions.\n\nIf you wish to randomly access pages of search results, you can use an offset: \n\n```python\nfrom google.appengine.api import search\n...\n# get the first set of results\npage_size = 10\nresults = index.search(search.Query(query_string='some stuff',\n options=search.QueryOptions(limit=page_size))\n\n# calculate pages\npages = results.number_found / page_size\n\n# user chooses page and hence an offset into results\nnext_page = ith * page_size\n\n# get the search results for that page\nresults = index.search(search.Query(query_string='some stuff',\n options=search.QueryOptions(limit=page_size, offset=next_page))\n```\n\nFor example, the following code fragment requests a search for documents where `first` occurs in the `subject` field and `good` occurs in any field, returning at most 20 documents, requesting the cursor for the next page of results, returning another cursor for the next set of results, sorting by subject in descending order, returning the author, subject, and summary fields as well as a snippeted field content: \n\n```python\n...\nresults = index.search(search.Query(\n query='subject:first good',\n options=search.QueryOptions(\n limit=20,\n cursor=search.Cursor(),\n sort_options=search.SortOptions(\n expressions=[\n search.SortExpression(expression='subject', default_value='')],\n limit=1000),\n returned_fields=['author', 'subject', 'summary'],\n snippeted_fields=['content'])))\n```\n\nConstructor\n-----------\n\nThe constructor for class `QueryOptions` is defined as follows:\n\n\nclass QueryOptions(limit=20, number_found_accuracy=None, cursor=None, offset=None, sort_options=None, returned_fields=None, ids_only=False, snippeted_fields=None, returned_expressions=None)\n\n: Specify options defining search query results..\n\n: Arguments\n\n limit\n\n : The limit on number of documents to return in results.\n\n number_found_accuracy\n\n : The minimum accuracy requirement for `SearchResults.number_found`. If set, remains accurate up to at least that number. For example, when set to 100, any `SearchResults` object with `number_found_accuracy` \\\u003c= 100 is accurate.\n\n | **Caution!** This option may add considerable latency/expense, especially when used with `returned_fields`.\n\n cursor\n\n : A Cursor describing where to get the next set of results,\n or to provide next cursors in SearchResults.\n\n offset\n\n : The offset represents the number of documents to skip in search results. This is an alternative to using a query cursor. It allows random access to the results. Offsets are more expensive (in terms of [instance hours](/appengine/docs/pricing)) than cursors. You can use either cursor or offset, but not both. Using an offset means that no cursor is returned in [`ScoredDocument.cursor`](/appengine/docs/legacy/standard/python/search/searchresultsclass) or `ScoredDocument.cursor`.\n\n sort_options\n\n : A [SortOptions](/appengine/docs/legacy/standard/python/search/sortoptionsclass) object specifying a multi-dimensional sort over search results.\n\n returned_fields\n\n : An iterable of names of fields to return in search\n results.\n\n ids_only\n\n : Only return document ids, do not return any fields.\n\n snippeted_fields\n\n : An iterable of names of fields to snippet and return\n in search result expressions.\n\n returned_expressions\n\n : An iterable of FieldExpression to evaluate and\n return in search results.\n\n Result value\n\n : A new instance of class `QueryOptions`.\n\n Exceptions\n\n TypeError\n\n : If an unknown iterator_options or sort_options is passed.\n\n ValueError\n\n : If `ids_only` and `returned_fields` are used together.\n\n \u003cbr /\u003e\n\nProperties\n----------\n\nAn instance of class `Query` has the following properties:\n\nlimit\n\n: Returns a limit on number of documents to return in results.\n\nnumber_found_accuracy\n\n: Returns minimum accuracy requirement for [SearchResults.number_found](/appengine/docs/legacy/standard/python/search/searchresultsclass).\n\ncursor\n\n: Returns the cursor for the query.\n\noffset\n\n: Returns the number of documents in search results to skip.\n\nsort_options\n\n: Returns a [SortOptions](/appengine/docs/legacy/standard/python/search/sortoptionsclass) object.\n\nreturned_fields\n\n: Returns an iterable of names of fields to return in search results.\n\nids_only\n\n: Returns only ` ` in search results.\n\nsnippeted_fields\n\n: Returns iterable of field names to snippet and return in results.\n\nreturned_expressions\n\n: Returns iterable of [FieldExpression](/appengine/docs/legacy/standard/python/search/fieldexpressionclass) to return in results."]]