Class: Google::Cloud::Datastore::AggregateQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/datastore/aggregate_query.rb

Overview

AggregateQuery

An aggregate query can be used to fetch aggregate values (ex: count) for a query

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task")
     .where("done", "=", false)

Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_count

aggregate_query_results = dataset.run_aggregation aggregate_query
puts aggregate_query_results.get

Alias an aggregate query

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task")
     .where("done", "=", false)

Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_count aggregate_alias: 'total'

aggregate_query_results = dataset.run_aggregation aggregate_query
puts aggregate_query_results.get('total')

Instance Method Summary collapse

Instance Method Details

#add_avg(name, aggregate_alias: nil) ⇒ AggregateQuery

Adds an average aggregate.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task")
     .where("done", "=", false)

Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_avg("score")

aggregate_query_results = dataset.run_aggregation aggregate_query
puts aggregate_query_results.get

Alias an aggregate AVG query

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task")
     .where("done", "=", false)

# Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_avg("score", aggregate_alias: 'avg_score')

aggregate_query_results = dataset.run_aggregation aggregate_query
puts aggregate_query_results.get('avg_score')

Parameters:

  • name (String)

    The property to apply average on.

  • aggregate_alias (String) (defaults to: nil)

    Alias to refer to the aggregate. Optional

Returns:

  • (AggregateQuery)

    The modified aggregate query object with the added AVG aggregate.



228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/google/cloud/datastore/aggregate_query.rb', line 228

def add_avg name, aggregate_alias: nil
  aggregate_alias ||= DEFAULT_AVG_AGGREGATE_ALIAS
  @grpc.aggregations << Google::Cloud::Datastore::V1::AggregationQuery::Aggregation.new(
    avg: Google::Cloud::Datastore::V1::AggregationQuery::Aggregation::Avg.new(
      property: Google::Cloud::Datastore::V1::PropertyReference.new(
        name: name
      )
    ),
    alias: aggregate_alias
  )

  self
end

#add_count(aggregate_alias: nil) ⇒ AggregateQuery

Adds a count aggregate.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task")
     .where("done", "=", false)

Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_count

aggregate_query_results = dataset.run_aggregation aggregate_query
puts aggregate_query_results.get

Alias an aggregate query

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task")
     .where("done", "=", false)

Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_count aggregate_alias: 'total'

aggregate_query_results = dataset.run_aggregation aggregate_query
puts aggregate_query_results.get('total')

Parameters:

  • aggregate_alias (String) (defaults to: nil)

    Alias to refer to the aggregate. Optional

Returns:

  • (AggregateQuery)

    The modified aggregate query object with the added count aggregate.



124
125
126
127
128
129
130
131
132
# File 'lib/google/cloud/datastore/aggregate_query.rb', line 124

def add_count aggregate_alias: nil
  aggregate_alias ||= DEFAULT_COUNT_AGGREGATE_ALIAS
  @grpc.aggregations << Google::Cloud::Datastore::V1::AggregationQuery::Aggregation.new(
    count: Google::Cloud::Datastore::V1::AggregationQuery::Aggregation::Count.new,
    alias: aggregate_alias
  )

  self
end

#add_sum(name, aggregate_alias: nil) ⇒ AggregateQuery

Adds a sum aggregate.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task")
     .where("done", "=", false)

Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_sum("score")

aggregate_query_results = dataset.run_aggregation aggregate_query
puts aggregate_query_results.get

Alias an aggregate SUM query

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task")
     .where("done", "=", false)

# Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_sum("score", aggregate_alias: 'total_score')

aggregate_query_results = dataset.run_aggregation aggregate_query
puts aggregate_query_results.get('total_score')

Parameters:

  • name (String)

    The property to sum by.

  • aggregate_alias (String) (defaults to: nil)

    Alias to refer to the aggregate. Optional

Returns:

  • (AggregateQuery)

    The modified aggregate query object with the added SUM aggregate.



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/google/cloud/datastore/aggregate_query.rb', line 174

def add_sum name, aggregate_alias: nil
  aggregate_alias ||= DEFAULT_SUM_AGGREGATE_ALIAS
  @grpc.aggregations << Google::Cloud::Datastore::V1::AggregationQuery::Aggregation.new(
    sum: Google::Cloud::Datastore::V1::AggregationQuery::Aggregation::Sum.new(
      property: Google::Cloud::Datastore::V1::PropertyReference.new(
        name: name
      )
    ),
    alias: aggregate_alias
  )

  self
end