Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Die Klasse QueryOptions bietet Optionen für die Nachbearbeitung von Abfrageergebnissen anhand der Anwendungsanforderungen. Sie erstellen die Klasse als options im Argument Query.options.
Query ist im Modul google.appengine.api.search definiert.
Einführung
Die Klasse QueryOptions bietet Optionen für die Nachbearbeitung der Ergebnisse einer bestimmten Abfrage. Optional können Sie die Ergebnisse sortieren, festlegen, welche Dokumentfelder zurückgegeben werden, Snippets von Feldern erzeugen sowie nach komplexen Bewertungsausdrücken berechnen und sortieren.
Wenn Sie nach dem Zufallsprinzip auf Seiten mit Suchergebnissen zugreifen möchten, können Sie einen Offset verwenden:
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))
Zum Beispiel fordert das folgende Codefragment eine Suche nach Dokumenten an, bei denen im Feld subject das Element first und in einem beliebigen Feld good vorkommt, wobei höchstens 20 Dokumente zurückgegeben werden. Der Cursor wird für die nächste Seite der Ergebnisse angefordert und ein anderer Cursor für die nächste Ergebnismenge. Die Sortierung erfolgt nach Thema in absteigender Reihenfolge. Es werden der Autor, das Thema, Zusammenfassungsfelder sowie ein Snippet-Feldinhalt zurückgegeben:
Geben Sie Optionen an, welche die Ergebnisse der Suchanfrage definieren.
Argumente
limit
Das Limit für die Anzahl der Dokumente, die in Ergebnissen zurückgegeben werden sollen.
number_found_accuracy
Die Mindestanforderung an die Genauigkeit für SearchResults.number_found. Wenn festgelegt, ist die Rückgabe mindestens bis zu dieser Zahl genau. Wenn Sie beispielsweise 100 festlegen, gilt jedes SearchResults-Objekt mit number_found_accuracy <= 100 als genau.
cursor
Ein Cursor, der beschreibt, wo die nächste Ergebnismenge zu finden ist oder wo die nächsten Cursors in den SearchResults bereitgestellt werden.
offset
Der Offset gibt die Anzahl der Dokumente an, die in den Suchergebnissen übersprungen werden. Das bietet eine Alternative zum Abfrage-Cursor und ermöglicht den zufälligen Zugriff auf die Ergebnisse. Offsets sind (in Bezug auf die Instanzstunden) kostspieliger als Cursor. Sie können entweder einen Cursor oder ein Offset, aber nicht beides verwenden. Bei Verwendung eines Offsets wird in ScoredDocument.cursor oder ScoredDocument.cursor kein Cursor zurückgegeben.
sort_options
Ein SortOptions-Objekt für die mehrdimensionale Sortierung von Suchergebnissen.
returned_fields
Iterierbare Namen der Felder, die in den Suchergebnissen zurückgegeben werden.
ids_only
Gibt nur Dokument-IDs zurück, keine Felder.
snippeted_fields
Iterierbare Namen der Felder, von denen in Suchergebnisausdrücken Snippets zurückgegeben werden.
returned_expressions
Eine Iteration von FieldExpression zum Auswerten und Zurückgeben von Suchergebnissen.
Ergebniswert
Eine neue Instanz der Klasse QueryOptions.
Ausnahmen
TypeError
Wenn unbekannte iterator_options oder sort_options weitergegeben werden.
ValueError
Wenn ids_only und returned_fields zusammen verwendet werden.
Eigenschaften
Eine Instanz der Klasse Query hat die folgenden Attribute:
limit
Gibt ein Limit für die Anzahl der Dokumente an, die in Ergebnissen zurückgegeben werden sollen
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[[["\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."]]