Class: Couchbase::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/scope.rb

Overview

The scope identifies a group of collections and allows high application density as a result.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend, bucket_name, scope_name) ⇒ Scope

Returns a new instance of Scope.

Parameters:

  • backend (Couchbase::Backend)
  • bucket_name (String)

    name of the bucket

  • scope_name (String)

    name of the scope



32
33
34
35
36
# File 'lib/couchbase/scope.rb', line 32

def initialize(backend, bucket_name, scope_name)
  @backend = backend
  @bucket_name = bucket_name
  @name = scope_name
end

Instance Attribute Details

#bucket_nameObject (readonly)

Returns the value of attribute bucket_name.



24
25
26
# File 'lib/couchbase/scope.rb', line 24

def bucket_name
  @bucket_name
end

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/couchbase/scope.rb', line 25

def name
  @name
end

Instance Method Details

#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::AnalyticsMetaData.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

#collection(collection_name) ⇒ Collection

Opens the default collection for this scope

Parameters:

  • collection_name (String)

    name of the collection

Returns:



43
44
45
# File 'lib/couchbase/scope.rb', line 43

def collection(collection_name)
  Collection.new(@backend, @bucket_name, @name, collection_name)
end

#query(statement, options = Options::Query::DEFAULT) ⇒ QueryResult

Performs a query against the query (N1QL) services.

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

Examples:

Select first ten hotels from travel sample dataset

scope.query("SELECT * FROM `travel-sample` WHERE type = $type LIMIT 10",
             Options::Query(named_parameters: {type: "hotel"}, metrics: true))

Parameters:

  • statement (String)

    the N1QL query statement

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

    the custom options for this query

Returns:

  • (QueryResult)

See Also:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/couchbase/scope.rb', line 62

def query(statement, options = Options::Query::DEFAULT)
  resp = @backend.document_query(statement, options.to_backend(scope_name: @name, bucket_name: @bucket_name))

  Cluster::QueryResult.new do |res|
    res. = Cluster::QueryMetaData.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::QueryMetrics.new do |metrics|
        if resp[:meta][:metrics]
          metrics.elapsed_time = resp[:meta][:metrics][:elapsed_time]
          metrics.execution_time = resp[:meta][:metrics][:execution_time]
          metrics.sort_count = resp[:meta][:metrics][:sort_count]
          metrics.result_count = resp[:meta][:metrics][:result_count]
          metrics.result_size = resp[:meta][:metrics][:result_size]
          metrics.mutation_count = resp[:meta][:metrics][:mutation_count]
          metrics.error_count = resp[:meta][:metrics][:error_count]
          metrics.warning_count = resp[:meta][:metrics][:warning_count]
        end
      end
      meta.warnings = resp[:warnings].map { |warn| Cluster::QueryWarning.new(warn[:code], warn[:message]) } if resp[:warnings]
    end
    res.instance_variable_set(:@rows, resp[:rows])
  end
end

#search(index_name, search_request, options = Options::Search::DEFAULT) ⇒ SearchResult

Performs a request against the Full Text Search (FTS) service.

Parameters:

  • index_name (String)

    the name of the search index

  • search_request (SearchRequest)

    the request

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

    the custom options for this search request

Returns:



159
160
161
162
163
# File 'lib/couchbase/scope.rb', line 159

def search(index_name, search_request, options = Options::Search::DEFAULT)
  encoded_query, encoded_req = search_request.to_backend
  resp = @backend.document_search(@bucket_name, @name, index_name, encoded_query, encoded_req, options.to_backend(show_request: false))
  convert_search_result(resp, options)
end

#search_indexesManagement::ScopeSearchIndexManager



166
167
168
# File 'lib/couchbase/scope.rb', line 166

def search_indexes
  Management::ScopeSearchIndexManager.new(@backend, @bucket_name, @name)
end

#search_query(index_name, query, options = Options::Search::DEFAULT) ⇒ SearchResult

Performs a Full Text Search (FTS) query

Examples:

Return first 10 results of “hop beer” query and request highlighting

cluster.search_query("travel_index", Cluster::SearchQuery.match_phrase("green"),
                     Options::Search(
                       limit: 10,
                       collections: ["landmark", "hotel"]
                       fields: %w[name],
                       highlight_style: :html,
                       highlight_fields: %w[name description]
                     ))

Parameters:

  • index_name (String)

    the name of the search index

  • query (SearchQuery)

    the query tree

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

    the query tree

Returns:



147
148
149
150
# File 'lib/couchbase/scope.rb', line 147

def search_query(index_name, query, options = Options::Search::DEFAULT)
  resp = @backend.document_search(@bucket_name, @name, index_name, JSON.generate(query), {}, options.to_backend)
  convert_search_result(resp, options)
end