Method: Couchbase::Scope#analytics_query

Defined in:
lib/couchbase/scope.rb

#analytics_query(statement, options = Options::Analytics::DEFAULT) ⇒ AnalyticsResult

Performs an analytics query

The query will be implicitly scoped using current bucket and scope names.

Examples:

Select name of the given user

scope.analytics_query("SELECT u.name AS uname FROM GleambookUsers u WHERE u.id = $user_id ",
                       Options::Analytics(named_parameters: {user_id: 2}))

Parameters:

  • statement (String)

    the N1QL query statement

  • options (Options::Analytics) (defaults to: Options::Analytics::DEFAULT)

    the custom options for this query

Returns:

  • (AnalyticsResult)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/couchbase/scope.rb', line 102

def analytics_query(statement, options = Options::Analytics::DEFAULT)
  resp = @backend.document_analytics(statement, options.to_backend(scope_name: @name, bucket_name: @bucket_name))

  Cluster::AnalyticsResult.new do |res|
    res.transcoder = options.transcoder
    res. = Cluster::.new do |meta|
      meta.status = resp[:meta][:status]
      meta.request_id = resp[:meta][:request_id]
      meta.client_context_id = resp[:meta][:client_context_id]
      meta.signature = JSON.parse(resp[:meta][:signature]) if resp[:meta][:signature]
      meta.profile = JSON.parse(resp[:meta][:profile]) if resp[:meta][:profile]
      meta.metrics = Cluster::AnalyticsMetrics.new do |metrics|
        if resp[:meta][:metrics]
          metrics.elapsed_time = resp[:meta][:metrics][:elapsed_time]
          metrics.execution_time = resp[:meta][:metrics][:execution_time]
          metrics.result_count = resp[:meta][:metrics][:result_count]
          metrics.result_size = resp[:meta][:metrics][:result_size]
          metrics.error_count = resp[:meta][:metrics][:error_count]
          metrics.warning_count = resp[:meta][:metrics][:warning_count]
          metrics.processed_objects = resp[:meta][:metrics][:processed_objects]
        end
      end
      res[:warnings] = resp[:warnings].map { |warn| Cluster::AnalyticsWarning.new(warn[:code], warn[:message]) } if resp[:warnings]
    end
    res.instance_variable_set(:@rows, resp[:rows])
  end
end