# Cloud Firestore API - Class Google::Cloud::Firestore::AggregateQueryExplainResult (v3.2.0)

3.2.0 (latest) 3.1.1 3.0.0 2.16.1 2.15.1 2.14.0 2.13.1 2.12.0 2.11.0 2.10.1 2.9.1 2.8.0 2.7.2 2.6.6 Reference documentation and code samples for the Cloud Firestore API class Google::Cloud::Firestore::AggregateQueryExplainResult.

## AggregateQueryExplainResult

Represents the result of a Firestore aggregate query explanation.
This class provides access to the
V1::ExplainMetrics, which contain details
about the query plan and execution statistics. If the explanation was
run with `analyze: true`, it also provides access to the
[AggregateQuerySnapshot](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-AggregateQuerySnapshot "Google::Cloud::Firestore::AggregateQuerySnapshot (class)").

The metrics and snapshot (if applicable) are fetched and cached upon
the first call to either [#explain_metrics](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-AggregateQueryExplainResult#Google__Cloud__Firestore__AggregateQueryExplainResult_explain_metrics_instance_ "Google::Cloud::Firestore::AggregateQueryExplainResult#explain_metrics (method)") or [#snapshot](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-AggregateQueryExplainResult#Google__Cloud__Firestore__AggregateQueryExplainResult_snapshot_instance_ "Google::Cloud::Firestore::AggregateQueryExplainResult#snapshot (method)").

## Inherits

- Object

## Examples

Getting planning and execution metrics with the snapshot

```ruby
require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new
aggregate_query = firestore.col(:cities).aggregate_query.add_count

explain_result = aggregate_query.explain analyze: true

metrics = explain_result.explain_metrics
if metrics
  puts "Plan summary: #{metrics.plan_summary&.to_json}"
  puts "Execution stats: #{metrics.execution_stats&.to_json}"
end

snapshot = explain_result.snapshot
puts "Count: #{snapshot.get}" if snapshot
```

Getting only planning metrics

```ruby
require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new
aggregate_query = firestore.col(:cities).aggregate_query.add_count

explain_result = aggregate_query.explain analyze: false # Default

metrics = explain_result.explain_metrics
puts "Plan summary: #{metrics.plan_summary&.to_json}" if metrics

# Snapshot will be nil because analyze was false
puts "Snapshot is nil: #{explain_result.snapshot.nil?}"
```

## Methods

### #explain_metrics

    def explain_metrics() -> Google::Cloud::Firestore::V1::ExplainMetrics, nil

The metrics from planning and potentially execution stages of the
aggregate query.


<br />

Calling this method for the first time will process the server
responses to extract and cache the metrics (and snapshot if
`analyze: true` was used). Subsequent calls return the cached metrics.
**Returns**

- (Google::Cloud::Firestore::V1::ExplainMetrics, nil) --- The query explanation metrics, or `nil` if no metrics were returned by the server.

### #metrics_fetched

    def metrics_fetched() -> Boolean

**Aliases**

- [#metrics_fetched?](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-AggregateQueryExplainResult#Google__Cloud__Firestore__AggregateQueryExplainResult_metrics_fetched?_instance_)
Indicates whether the metrics and snapshot (if applicable) have been
fetched from the server response and cached.
This becomes `true` after the first call to [#explain_metrics](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-AggregateQueryExplainResult#Google__Cloud__Firestore__AggregateQueryExplainResult_explain_metrics_instance_ "Google::Cloud::Firestore::AggregateQueryExplainResult#explain_metrics (method)") or
[#snapshot](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-AggregateQueryExplainResult#Google__Cloud__Firestore__AggregateQueryExplainResult_snapshot_instance_ "Google::Cloud::Firestore::AggregateQueryExplainResult#snapshot (method)").
**Returns**

- (Boolean) --- `true` if data has been fetched, `false` otherwise.

### #metrics_fetched?

    def metrics_fetched?() -> Boolean

**Alias Of** : [#metrics_fetched](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-AggregateQueryExplainResult#Google__Cloud__Firestore__AggregateQueryExplainResult_metrics_fetched_instance_) Indicates whether the metrics and snapshot (if applicable) have been
fetched from the server response and cached.
This becomes `true` after the first call to [#explain_metrics](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-AggregateQueryExplainResult#Google__Cloud__Firestore__AggregateQueryExplainResult_explain_metrics_instance_ "Google::Cloud::Firestore::AggregateQueryExplainResult#explain_metrics (method)") or
[#snapshot](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-AggregateQueryExplainResult#Google__Cloud__Firestore__AggregateQueryExplainResult_snapshot_instance_ "Google::Cloud::Firestore::AggregateQueryExplainResult#snapshot (method)").
**Returns**

- (Boolean) --- `true` if data has been fetched, `false` otherwise.

### #snapshot

    def snapshot() -> AggregateQuerySnapshot, nil

The [AggregateQuerySnapshot](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-AggregateQuerySnapshot "Google::Cloud::Firestore::AggregateQuerySnapshot (class)") containing the aggregation results.


This is only available if the explanation was run with `analyze: true`.
If `analyze: false` was used, or if the query yielded no results
even with `analyze: true`, this method returns `nil`.

<br />

Calling this method for the first time will process the server
responses to extract and cache the snapshot (and metrics).
Subsequent calls return the cached snapshot.
**Returns**

- ([AggregateQuerySnapshot](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-AggregateQuerySnapshot), nil) --- The aggregate query snapshot if `analyze: true` was used and results are available, otherwise `nil`.