Class: Mongo::SearchIndex::View
- Inherits:
-
Object
- Object
- Mongo::SearchIndex::View
- Extended by:
- Forwardable
- Includes:
- Enumerable, Collection::Helpers, Retryable
- Defined in:
- lib/mongo/search_index/view.rb
Overview
A class representing a view of search indexes.
Instance Attribute Summary collapse
-
#aggregate_options ⇒ Hash
readonly
The options hash to use for the aggregate command when querying the available indexes.
-
#collection ⇒ Mongo::Collection
readonly
The collection this view belongs to.
-
#requested_index_id ⇒ nil | String
readonly
The index id to query.
-
#requested_index_name ⇒ nil | String
readonly
The index name to query.
Instance Method Summary collapse
-
#create_many(indexes) ⇒ Array<String>
Create multiple search indexes with a single command.
-
#create_one(definition, name: nil, type: 'search') ⇒ String
Create a single search index with the given definition.
-
#drop_one(id: nil, name: nil) ⇒ Mongo::Operation::Result | false
Drop the search index with the given id, or name.
-
#each(&block) ⇒ self | Enumerator
Iterate over the search indexes.
-
#empty? ⇒ true | false
Queries whether the search index enumerable is empty.
-
#initialize(collection, options = {}) ⇒ View
constructor
Create the new search index view.
-
#update_one(definition, id: nil, name: nil) ⇒ Mongo::Operation::Result
Update the search index with the given id or name.
Methods included from Collection::Helpers
Methods included from Retryable
#read_worker, #select_server, #write_worker
Constructor Details
#initialize(collection, options = {}) ⇒ View
Create the new search index view.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mongo/search_index/view.rb', line 36 def initialize(collection, = {}) @collection = collection @requested_index_id = [:id] @requested_index_name = [:name] @aggregate_options = [:aggregate] || {} return if @aggregate_options.is_a?(Hash) raise ArgumentError, "The :aggregate option must be a Hash (got a #{@aggregate_options.class})" end |
Instance Attribute Details
#aggregate_options ⇒ Hash (readonly)
Returns the options hash to use for the aggregate command when querying the available indexes.
23 24 25 |
# File 'lib/mongo/search_index/view.rb', line 23 def @aggregate_options end |
#collection ⇒ Mongo::Collection (readonly)
Returns the collection this view belongs to.
13 14 15 |
# File 'lib/mongo/search_index/view.rb', line 13 def collection @collection end |
#requested_index_id ⇒ nil | String (readonly)
Returns the index id to query.
16 17 18 |
# File 'lib/mongo/search_index/view.rb', line 16 def requested_index_id @requested_index_id end |
#requested_index_name ⇒ nil | String (readonly)
Returns the index name to query.
19 20 21 |
# File 'lib/mongo/search_index/view.rb', line 19 def requested_index_name @requested_index_name end |
Instance Method Details
#create_many(indexes) ⇒ Array<String>
Create multiple search indexes with a single command.
71 72 73 74 75 76 77 78 79 |
# File 'lib/mongo/search_index/view.rb', line 71 def create_many(indexes) spec = spec_with(indexes: indexes.map { |v| validate_search_index!(v) }) operation = Operation::CreateSearchIndexes.new(spec) context = execution_context tracer.trace_operation(operation, context, op_name: 'createSearchIndexes') do result = operation.execute(next_primary, context: context) result.first['indexesCreated'].map { |idx| idx['name'] } end end |
#create_one(definition, name: nil, type: 'search') ⇒ String
Create a single search index with the given definition. If the name is provided, the new index will be given that name.
56 57 58 59 60 61 |
# File 'lib/mongo/search_index/view.rb', line 56 def create_one(definition, name: nil, type: 'search') spec = { definition: definition, type: type }.tap do |sp| sp[:name] = name unless name.nil? end create_many([ spec ]).first end |
#drop_one(id: nil, name: nil) ⇒ Mongo::Operation::Result | false
Drop the search index with the given id, or name. One or the other must be specified, but not both.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mongo/search_index/view.rb', line 89 def drop_one(id: nil, name: nil) validate_id_or_name!(id, name) spec = spec_with(index_id: id, index_name: name) op = Operation::DropSearchIndex.new(spec) context = execution_context tracer.trace_operation(op, context, op_name: 'dropSearchIndex') do # per the spec: # Drivers MUST suppress NamespaceNotFound errors for the # ``dropSearchIndex`` helper. Drop operations should be idempotent. do_drop(op, nil, context) end end |
#each(&block) ⇒ self | Enumerator
Iterate over the search indexes.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/mongo/search_index/view.rb', line 111 def each(&block) @result ||= begin spec = {}.tap do |s| s[:id] = requested_index_id if requested_index_id s[:name] = requested_index_name if requested_index_name end collection.with(read_concern: {}).aggregate( [ { '$listSearchIndexes' => spec } ], ) end return @result.to_enum unless block @result.each(&block) self end |
#empty? ⇒ true | false
Queries whether the search index enumerable is empty.
156 157 158 |
# File 'lib/mongo/search_index/view.rb', line 156 def empty? count.zero? end |
#update_one(definition, id: nil, name: nil) ⇒ Mongo::Operation::Result
Update the search index with the given id or name. One or the other must be provided, but not both.
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/mongo/search_index/view.rb', line 139 def update_one(definition, id: nil, name: nil) validate_id_or_name!(id, name) spec = spec_with(index_id: id, index_name: name, index: definition) op = Operation::UpdateSearchIndex.new(spec) context = execution_context tracer.trace_operation(op, context, op_name: 'updateSearchIndex') do op.execute(next_primary, context: context) end end |