Python 2.7 ha raggiunto la fine del supporto
e verrà ritirato
il 31 gennaio 2026. Dopo il ritiro, non potrai eseguire il deployment di applicazioni Python 2.7, anche se la tua organizzazione ha utilizzato in precedenza un criterio dell'organizzazione per riattivare i deployment di runtime legacy. Le tue applicazioni Python 2.7 esistenti continueranno a essere eseguite e a ricevere traffico dopo la
data di ritiro. Ti consigliamo di eseguire la migrazione all'ultima versione supportata di Python.
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
La classe QueryOptions fornisce opzioni per la post-elaborazione dei risultati delle query in base alle esigenze della tua applicazione. Costruisci la classe come options nell'argomento Query.options.
Query è definito nel modulo google.appengine.api.search.
Introduzione
La classe QueryOptions fornisce opzioni per il post-trattamento dei risultati di una query specifica. Le opzioni includono la possibilità di ordinare i risultati, controllare i campi del documento da restituire, produrre snippet di campi e calcolare e ordinare in base a espressioni di punteggio complesse.
Se vuoi accedere in modo casuale alle pagine dei risultati di ricerca, puoi utilizzare un offset:
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))
Ad esempio, il seguente frammento di codice richiede la ricerca di documenti in cui first si verifica nel campo subject e good si verifica in qualsiasi campo, restituisce al massimo 20 documenti, richiede il cursore per la pagina successiva dei risultati, restituisce un altro cursore per il set di risultati successivo, ordina in ordine decrescente per argomento, restituisce i campi autore, argomento e riepilogo, nonché i contenuti del campo con snippet:
Specifica le opzioni che definiscono i risultati della query di ricerca.
Argomenti
limit
Il limite di documenti da restituire nei risultati.
number_found_accuracy
Il requisito di precisione minima per SearchResults.number_found. Se impostato, rimane accurato fino ad almeno quel numero. Ad esempio, se impostato su 100, qualsiasi oggetto SearchResults con number_found_accuracy <= 100 è accurato.
cursor
Un cursore che descrive dove recuperare l'insieme di risultati successivo o per fornire i cursori successivi in SearchResults.
offset
L'offset rappresenta il numero di documenti da saltare nei risultati di ricerca. Si tratta di un'alternativa all'utilizzo di un cursore di query. Consente l'accesso casuale ai risultati. Gli offset sono più costosi (in termini di ore di istanza) rispetto ai cursori. Puoi utilizzare cursor o offset, ma non entrambi. L'utilizzo di un offset significa che non viene restituito alcun cursore in ScoredDocument.cursor o ScoredDocument.cursor.
sort_options
Un oggetto SortOptions che specifica un'ordinamento multi-dimensionale dei risultati di ricerca.
returned_fields
Un insieme di nomi di campi da restituire nei risultati di ricerca.
ids_only
Restituisce solo gli ID documento, non restituisce alcun campo.
snippeted_fields
Un iterable di nomi di campi da snippet e da restituire
nelle espressioni dei risultati di ricerca.
returned_expressions
Un iterable di FieldExpression da valutare e
restituire nei risultati di ricerca.
Valore del risultato
Una nuova istanza della classe QueryOptions.
Eccezioni
TypeError
Se viene passato un iterator_options o sort_options sconosciuto.
ValueError
Se ids_only e returned_fields vengono utilizzati insieme.
Proprietà
Un'istanza della classe Query ha le seguenti proprietà:
limit
Restituisce un limite al numero di documenti da restituire nei risultati.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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."]]