Python 2.7 はサポートが終了しており、2026 年 1 月 31 日に
非推奨になります。非推奨になると、過去に組織のポリシーを使用して以前のランタイムのデプロイを再度有効にしていた場合でも、Python 2.7 アプリケーションをデプロイできなくなります。既存の Python 2.7 アプリケーションは、
非推奨日以降も引き続き実行され、トラフィックを受信します。
サポートされている最新バージョンの Python に移行することをおすすめします。
Query クラス
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Query
クラスは、検索サービスに対するインデックスのクエリのリクエストを表します。
Query
は、google.appengine.api.search
モジュールで定義されます。
概要
Query
クラスを使用すると、インデックスでの検索について、クエリ文字列のほか、並べ替え順、制限、カーソルなどのオプションを指定できます。これらのオプションを設定するには、Query.options
パラメータに対して QueryOptions
クラスをインスタンス化します。
たとえば、次のコードでは件名に first
、いずれかの部分に good
を含むドキュメントの検索を要求しています。この例の場合、最大で 20 のドキュメントが返され、その結果について 1 つのドキュメント カーソルが返され、件名で降順に並べ替えて、作成者、件名、概要の各フィールドの他にスニペット フィールドのコンテンツが返されます。
from google.appengine.api import search
...
results = index.search(search.Query(
# Specify the query string using the Search API's Query 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'])))
...
検索結果のセットごとにカーソルを返すことが可能です。このカーソルを使用すると、検索結果のページ間を移動しやすくなります。カーソルを取得するには、QueryOptions.cursor
に指定し、SearchResults.cursor
から次のリクエストのカーソルを抽出します。これにより、以下に示すとおり、最後に見つかったドキュメントから検索を続行できます。
...
results = index.search(
search.Query(query_string='subject:first good',
options=search.QueryOptions(cursor=results.cursor)))
コンストラクタ
Query
クラスのコンストラクタは、次のように定義されます。
-
class Query(query_string, options=None, enable_facet_discovery=False, return_facets=None, facet_options=None, facet_refinements=None)
検索サービスに対してインデックスのクエリを要求し、該当のクエリについてパラメータを指定します。
引数
- query_string
インデックス内のドキュメントと照合するクエリ。 クエリはキーワードを含むブール式です。たとえば、job tag:"very important" sent < 2011-02-28
というクエリでは、いずれかのフィールドにキーワード job
、tag
フィールドにフレーズ very important
が含まれ、sent
の日付が 2011 年 2 月 28 日より前であるドキュメントが見つかります。
- options
検索結果の後処理の指示を含む QueryOptions
クラスのインスタンス化。
- enable_facet_discovery
この検索クエリを満たし、返すドキュメントによって使用される最も関連性の高いファセットの検出を有効にします。
- return_facets
結果とともに特定のファセットを返されるファセット名として、反復可能な FacetRequest または basestring。
- facet_options
ファセットの処理を記述する FacetOption。
- facet_refinements
ファセット値に基づいて検索結果を除外するファセット絞り込みに使用される反復可能な FacetRefinement オブジェクトまたは絞り込みトークン文字列。異なるファセットの絞り込みは結合し、同じファセットの絞り込みは分離します。
結果値
Query
クラスの新しいインスタンス。
例外
- TypeError
query_string
が文字列ではないか、options
が QueryOptions
オブジェクトではない場合に発生します。
- ValueError
query_string
を解析できなかった場合に発生します。
プロパティ
Query
クラスのインスタンスには次のプロパティがあります。
- query_string
このリクエストで検索するクエリ文字列を返します。
- options
検索結果の後処理を定義する QueryOptions
を返します。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-19 UTC。
[[["わかりやすい","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-08-19 UTC。"],[[["\u003cp\u003eThe \u003ccode\u003eQuery\u003c/code\u003e class is used to define and send search requests to an index in the search service, specifying the query string and other search parameters.\u003c/p\u003e\n"],["\u003cp\u003eYou can customize search parameters like sort order, result limits, and cursors for pagination by using the \u003ccode\u003eQueryOptions\u003c/code\u003e class in conjunction with the \u003ccode\u003eQuery\u003c/code\u003e class.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003equery_string\u003c/code\u003e within the \u003ccode\u003eQuery\u003c/code\u003e object represents a boolean expression used to match documents within the index, enabling complex searches.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eQuery\u003c/code\u003e class allows for the retrieval of a cursor from \u003ccode\u003eSearchResults\u003c/code\u003e to enable efficient pagination through large result sets, allowing for continued searches.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eQuery\u003c/code\u003e constructor can handle options related to facet discovery and refinements, providing advanced filtering and analysis of search results.\u003c/p\u003e\n"]]],[],null,["# The Query Class\n\nClass `Query` represents a request on the search service to query the index.\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\nThe `Query` class allows you to specify a query string and other options, such as sort\norder, limit, and a cursor, for a search on an index. You set these options by instantiating the\n[QueryOptions](/appengine/docs/legacy/standard/python/search/queryoptionsclass) class to the\n`Query.options` parameter.\n\nFor example, the following code fragment requests a search for documents where `first`\noccurs in subject and `good` occurs anywhere, returning at most 20 documents, returning\na single document cursor for the results, sorting by subject in descending order, returning the\nauthor, subject, and summary fields as well as a snippeted field content. \n\n```python\nfrom google.appengine.api import search\n\n...\nresults = index.search(search.Query(\n # Specify the query string using the Search API's /appengine/docs/legacy/standard/python/search/query_strings.\n query_string='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```\n\nYou have the option to return a cursor with each set of search results. This cursor allows you to\nmore easily page through search results. To get a Cursor, specify it in\n[QueryOptions.cursor](/appengine/docs/legacy/standard/python/search/queryoptionsclass) and extract the cursor for\nthe next request from [SearchResults.cursor](/appengine/docs/legacy/standard/python/search/searchresultsclass).\nThis allows you to continue your search from the last found document, as shown below:\n\n```python\n...\nresults = index.search(\n search.Query(query_string='subject:first good',\n options=search.QueryOptions(cursor=results.cursor)))\n```\n\nConstructor\n-----------\n\nThe constructor for class `Query` is defined as follows:\n\n\nclass Query(query_string, options=None, enable_facet_discovery=False, return_facets=None, facet_options=None, facet_refinements=None)\n\n:\n\n Request the search service to query an index, specifying parameters for that query.\n\n \u003cbr /\u003e\n\n:\n\n Arguments\n\n query_string\n\n : The query to match against documents in the index. A query is a boolean expression containing terms. For example, the query `job tag:\"very important\" sent \u003c 2011-02-28 ` finds documents with the term `job` in any field, and also contain the phrase `very important` in a `tag` field, and a `sent` date prior to February 28, 2011.\n\n options\n\n : Instantiation of the [QueryOptions](/appengine/docs/legacy/standard/python/search/queryoptionsclass) class with instructions for post-processing search results.\n\n enable_facet_discovery\n\n : Enable the discovery of the most relevant facets used by the documents that satisfy this search query\n and return them.\n\n return_facets\n\n : An iterable of FacetRequest or basestring as facet name to\n return specific facet with the result.\n\n facet_options\n\n : A FacetOption describing processing of facets.\n\n facet_refinements\n\n : An iterable of FacetRefinement objects or refinement\n token strings used to filter out search results based on a facet value.\n refinements for different facets will be conjunction and refinements for\n the same facet will be disjunction.\n\n Result value\n\n : A new instance of class `Query`.\n\n Exceptions\n\n TypeError\n\n : Raised when `query_string` is not a string or `options` is not a `QueryOptions` object.\n\n ValueError\n\n : Raised when the `query_string` could not be parsed.\n\n Properties\n ----------\n\n An instance of class `Query` has the following properties:\n\n query_string\n\n : Returns the query string to search in this request.\n\n options\n\n : Returns the [QueryOptions](/appengine/docs/legacy/standard/python/search/queryoptionsclass) defining post-processing of the search results."]]