Python 2.7 has reached end of supportand will bedeprecatedon January 31, 2026. After deprecation, you won't be able to deploy Python 2.7applications, even if your organization previously used an organization policy tore-enable deployments of legacy runtimes. Your existing Python2.7 applications will continue to run and receive traffic after theirdeprecation date. We recommend thatyoumigrate to the latest supported version of Python.

The Query Class

ClassQuery represents a request on the search service to query the index.

This API is supported for first-generation runtimes and can be used whenupgrading to corresponding second-generation runtimes. If you are updating to the App Engine Python 3 runtime, refer to themigration guide to learn about your migration options for legacy bundled services.

Query is defined in thegoogle.appengine.api.search module.

Introduction

TheQuery class allows you to specify a query string and other options, such as sort order, limit, and a cursor, for a search on an index. You set these options by instantiating theQueryOptions class to theQuery.options parameter.

For example, the following code fragment requests a search for documents wherefirst occurs in subject andgood occurs anywhere, returning at most 20 documents, returning a single document cursor for the results, sorting by subject in descending order, returning the author, subject, and summary fields as well as a snippeted field content.

fromgoogle.appengine.apiimportsearch...results=index.search(search.Query(# Specify the query string using the Search API'sQuery language.query_string='subject:first good',options=search.QueryOptions(limit=20,cursor=search.Cursor(),sort_options=search.SortOptions(expressions=[search.SortExpression(expression='subject',default_value='')],limit=1000),returned_fields=['author','subject','summary'],snippeted_fields=['content'])))...

You have the option to return a cursor with each set of search results. This cursor allows you to more easily page through search results. To get a Cursor, specify it inQueryOptions.cursor and extract the cursor for the next request fromSearchResults.cursor. This allows you to continue your search from the last found document, as shown below:

...results=index.search(search.Query(query_string='subject:first good',options=search.QueryOptions(cursor=results.cursor)))

Constructor

The constructor for classQuery is defined as follows:

class Query(query_string, options=None, enable_facet_discovery=False, return_facets=None, facet_options=None, facet_refinements=None)

Request the search service to query an index, specifying parameters for that query.

Arguments

query_string

The query to match against documents in the index. A query is a boolean expression containing terms. For example, the queryjob tag:"very important" sent < 2011-02-28 finds documents with the termjob in any field, and also contain the phrasevery important in atag field, and asent date prior to February 28, 2011.

options

Instantiation of theQueryOptions class with instructions for post-processing search results.

enable_facet_discovery

Enable the discovery of the most relevant facets used by the documents that satisfy this search query and return them.

return_facets

An iterable of FacetRequest or basestring as facet name to return specific facet with the result.

facet_options

A FacetOption describing processing of facets.

facet_refinements

An iterable of FacetRefinement objects or refinement token strings used to filter out search results based on a facet value. refinements for different facets will be conjunction and refinements for the same facet will be disjunction.

Result value

A new instance of classQuery.

Exceptions

TypeError

Raised whenquery_string is not a string oroptions is not aQueryOptions object.

ValueError

Raised when thequery_string could not be parsed.

Properties

An instance of classQuery has the following properties:

query_string

Returns the query string to search in this request.

options

Returns theQueryOptions defining post-processing of the search results.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.