Reference documentation and code samples for the Cloud Firestore API class Google::Cloud::Firestore::QueryExplainResult.
QueryExplainResult
Represents the result of a Firestore query explanation. This class
provides an enumerable interface to iterate over the DocumentSnapshot
results (if the explanation was run with analyze: true) and allows
access to the V1::ExplainMetrics which
contain details about the query plan and execution statistics.
Unlike the Enumerator object that is returned from the Query#get,
iterating over QueryExplainResult multiple times will not result in
multiple requests to the server. The first set of results will be saved
and re-used instead.
This is to avoid the situations where the metrics do not correspond to the results
if results are partially re-enumerated
Inherits
Object
Includes
Enumerable
Examples
Iterating over results and accessing metrics
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.newquery=firestore.col(:cities).where(:population,:>,100000)# Run the query and return metrics from the planning and execution stagesexplanation_result=query.explainanalyze:trueexplanation_result.eachdo|city_snapshot|puts"City: #{city_snapshot.document_id}, Population: #{city_snapshot[:population]}"endmetrics=explanation_result.explain_metricsputs"Results returned: #{metrics.execution_stats.results_returned}"ifmetrics&.execution_stats
Fetching metrics directly (which also iterates internally if needed)
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.newquery=firestore.col(:cities).where(:population,:>,100000)# Get the execution plan without running the query (or with analyze: true)explanation_result=query.explainanalyze:false# or truemetrics=explanation_result.explain_metricsputs"Plan summary: #{metrics.plan_summary}"ifmetrics&.plan_summaryputs"Results returned: #{metrics.execution_stats.results_returned}"ifmetrics&.execution_stats
Iterating over results multiple times
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.newquery=firestore.col(:cities).where(:population,:>,100000)explanation_result=query.explainanalyze:trueresults=explanation_result.to_aresults_2=explanation_result.to_a# same results, no re-query
Methods
#each
defeach(){|snapshot|...}->Enumerator
Iterates over the document snapshots returned by the query explanation
if analyze: true was used. If analyze: false was used, this
method will still iterate but will not yield any documents, though it
will populate the query explanation metrics.
Yield Parameter
snapshot (DocumentSnapshot) — A document snapshot from the query results.
The metrics from planning and execution stages of the query.
Calling this the first time will enumerate and cache all results as well as cache the metrics.
Subsequent calls will return the cached value.
Returns
(Google::Cloud::Firestore::V1::ExplainMetrics) — The query explanation metrics.
Indicates whether the #explain_metrics have been populated.
This becomes true after iterating through the results (e.g., via #each)
or by explicitly calling #explain_metrics.
Returns
(Boolean) — true if metrics are populated, false otherwise.
Indicates whether the #explain_metrics have been populated.
This becomes true after iterating through the results (e.g., via #each)
or by explicitly calling #explain_metrics.
Returns
(Boolean) — true if metrics are populated, false otherwise.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-09 UTC."],[],[],null,["# Cloud Firestore API - Class Google::Cloud::Firestore::QueryExplainResult (v3.1.0)\n\nVersion latestkeyboard_arrow_down\n\n- [3.1.0 (latest)](/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-QueryExplainResult)\n- [3.0.0](/ruby/docs/reference/google-cloud-firestore/3.0.0/Google-Cloud-Firestore-QueryExplainResult)\n- [2.16.1](/ruby/docs/reference/google-cloud-firestore/2.16.1/Google-Cloud-Firestore-QueryExplainResult)\n- [2.15.1](/ruby/docs/reference/google-cloud-firestore/2.15.1/Google-Cloud-Firestore-QueryExplainResult)\n- [2.14.0](/ruby/docs/reference/google-cloud-firestore/2.14.0/Google-Cloud-Firestore-QueryExplainResult)\n- [2.13.1](/ruby/docs/reference/google-cloud-firestore/2.13.1/Google-Cloud-Firestore-QueryExplainResult)\n- [2.12.0](/ruby/docs/reference/google-cloud-firestore/2.12.0/Google-Cloud-Firestore-QueryExplainResult)\n- [2.11.0](/ruby/docs/reference/google-cloud-firestore/2.11.0/Google-Cloud-Firestore-QueryExplainResult)\n- [2.10.1](/ruby/docs/reference/google-cloud-firestore/2.10.1/Google-Cloud-Firestore-QueryExplainResult)\n- [2.9.1](/ruby/docs/reference/google-cloud-firestore/2.9.1/Google-Cloud-Firestore-QueryExplainResult)\n- [2.8.0](/ruby/docs/reference/google-cloud-firestore/2.8.0/Google-Cloud-Firestore-QueryExplainResult)\n- [2.7.2](/ruby/docs/reference/google-cloud-firestore/2.7.2/Google-Cloud-Firestore-QueryExplainResult)\n- [2.6.6](/ruby/docs/reference/google-cloud-firestore/2.6.6/Google-Cloud-Firestore-QueryExplainResult) \nReference documentation and code samples for the Cloud Firestore API class Google::Cloud::Firestore::QueryExplainResult.\n\nQueryExplainResult\n------------------\n\nRepresents the result of a Firestore query explanation. This class\nprovides an enumerable interface to iterate over the [DocumentSnapshot](/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-DocumentSnapshot \"Google::Cloud::Firestore::DocumentSnapshot (class)\")\nresults (if the explanation was run with `analyze: true`) and allows\naccess to the V1::ExplainMetrics which\ncontain details about the query plan and execution statistics.\n\nUnlike the Enumerator object that is returned from the `Query#get`,\niterating over QueryExplainResult multiple times will not result in\nmultiple requests to the server. The first set of results will be saved\nand re-used instead.\n\nThis is to avoid the situations where the metrics do not correspond to the results\nif results are partially re-enumerated \n\nInherits\n--------\n\n- Object \n\nIncludes\n--------\n\n- Enumerable\n\nExamples\n--------\n\nIterating over results and accessing metrics \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\nquery = firestore.col(:cities).where(:population, :\u003e, 100000)\n\n# Run the query and return metrics from the planning and execution stages\nexplanation_result = query.explain analyze: true\n\nexplanation_result.each do |city_snapshot|\n puts \"City: #{city_snapshot.document_id}, Population: #{city_snapshot[:population]}\"\nend\n\nmetrics = explanation_result.explain_metrics\nputs \"Results returned: #{metrics.execution_stats.results_returned}\" if metrics&.execution_stats\n```\n\nFetching metrics directly (which also iterates internally if needed) \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\nquery = firestore.col(:cities).where(:population, :\u003e, 100000)\n\n# Get the execution plan without running the query (or with analyze: true)\nexplanation_result = query.explain analyze: false # or true\n\nmetrics = explanation_result.explain_metrics\nputs \"Plan summary: #{metrics.plan_summary}\" if metrics&.plan_summary\nputs \"Results returned: #{metrics.execution_stats.results_returned}\" if metrics&.execution_stats\n```\n\nIterating over results multiple times \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\nquery = firestore.col(:cities).where(:population, :\u003e, 100000)\nexplanation_result = query.explain analyze: true\nresults = explanation_result.to_a\nresults_2 = explanation_result.to_a # same results, no re-query\n```\n\nMethods\n-------\n\n### #each\n\n def each() { |snapshot| ... } -\u003e Enumerator\n\nIterates over the document snapshots returned by the query explanation\nif `analyze: true` was used. If `analyze: false` was used, this\nmethod will still iterate but will not yield any documents, though it\nwill populate the query explanation metrics. \n**Yield Parameter**\n\n- **snapshot** ([DocumentSnapshot](./Google-Cloud-Firestore-DocumentSnapshot)) --- A document snapshot from the query results. \n**Returns**\n\n- (Enumerator) --- If no block is given.\n\n### #explain_metrics\n\n def explain_metrics() -\u003e Google::Cloud::Firestore::V1::ExplainMetrics\n\nThe metrics from planning and execution stages of the query.\nCalling this the first time will enumerate and cache all results as well as cache the metrics.\n\n\n\u003cbr /\u003e\n\nSubsequent calls will return the cached value. \n**Returns**\n\n- (Google::Cloud::Firestore::V1::ExplainMetrics) --- The query explanation metrics.\n\n### #metrics_fetched\n\n def metrics_fetched() -\u003e Boolean\n\n**Aliases**\n\n- [#metrics_fetched?](./Google-Cloud-Firestore-QueryExplainResult#Google__Cloud__Firestore__QueryExplainResult_metrics_fetched?_instance_) \nIndicates whether the [#explain_metrics](/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-QueryExplainResult#Google__Cloud__Firestore__QueryExplainResult_explain_metrics_instance_ \"Google::Cloud::Firestore::QueryExplainResult#explain_metrics (method)\") have been populated.\nThis becomes `true` after iterating through the results (e.g., via [#each](/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-QueryExplainResult#Google__Cloud__Firestore__QueryExplainResult_each_instance_ \"Google::Cloud::Firestore::QueryExplainResult#each (method)\"))\nor by explicitly calling [#explain_metrics](/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-QueryExplainResult#Google__Cloud__Firestore__QueryExplainResult_explain_metrics_instance_ \"Google::Cloud::Firestore::QueryExplainResult#explain_metrics (method)\"). \n**Returns**\n\n- (Boolean) --- `true` if metrics are populated, `false` otherwise.\n\n### #metrics_fetched?\n\n def metrics_fetched?() -\u003e Boolean\n\n**Alias Of** : [#metrics_fetched](./Google-Cloud-Firestore-QueryExplainResult#Google__Cloud__Firestore__QueryExplainResult_metrics_fetched_instance_) \nIndicates whether the [#explain_metrics](/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-QueryExplainResult#Google__Cloud__Firestore__QueryExplainResult_explain_metrics_instance_ \"Google::Cloud::Firestore::QueryExplainResult#explain_metrics (method)\") have been populated.\nThis becomes `true` after iterating through the results (e.g., via [#each](/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-QueryExplainResult#Google__Cloud__Firestore__QueryExplainResult_each_instance_ \"Google::Cloud::Firestore::QueryExplainResult#each (method)\"))\nor by explicitly calling [#explain_metrics](/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-QueryExplainResult#Google__Cloud__Firestore__QueryExplainResult_explain_metrics_instance_ \"Google::Cloud::Firestore::QueryExplainResult#explain_metrics (method)\"). \n**Returns**\n\n- (Boolean) --- `true` if metrics are populated, `false` otherwise."]]