Class: Mongo::SearchIndex::View
- Inherits:
-
Object
- Object
- Mongo::SearchIndex::View
- 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) ⇒ 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.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mongo/search_index/view.rb', line 33 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.
22 23 24 |
# File 'lib/mongo/search_index/view.rb', line 22 def @aggregate_options end |
#collection ⇒ Mongo::Collection (readonly)
Returns the collection this view belongs to.
12 13 14 |
# File 'lib/mongo/search_index/view.rb', line 12 def collection @collection end |
#requested_index_id ⇒ nil | String (readonly)
Returns the index id to query.
15 16 17 |
# File 'lib/mongo/search_index/view.rb', line 15 def requested_index_id @requested_index_id end |
#requested_index_name ⇒ nil | String (readonly)
Returns the index name to query.
18 19 20 |
# File 'lib/mongo/search_index/view.rb', line 18 def requested_index_name @requested_index_name end |
Instance Method Details
#create_many(indexes) ⇒ Array<String>
Create multiple search indexes with a single command.
62 63 64 65 66 |
# File 'lib/mongo/search_index/view.rb', line 62 def create_many(indexes) spec = spec_with(indexes: indexes.map { |v| validate_search_index!(v) }) result = Operation::CreateSearchIndexes.new(spec).execute(next_primary, context: execution_context) result.first['indexesCreated'].map { |idx| idx['name'] } end |
#create_one(definition, name: nil) ⇒ String
Create a single search index with the given definition. If the name is provided, the new index will be given that name.
51 52 53 |
# File 'lib/mongo/search_index/view.rb', line 51 def create_one(definition, name: nil) create_many([ { name: name, definition: definition } ]).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.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/mongo/search_index/view.rb', line 76 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) # per the spec: # Drivers MUST suppress NamespaceNotFound errors for the # ``dropSearchIndex`` helper. Drop operations should be idempotent. do_drop(op, nil, execution_context) end |
#each(&block) ⇒ self | Enumerator
Iterate over the search indexes.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/mongo/search_index/view.rb', line 95 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.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.
136 137 138 |
# File 'lib/mongo/search_index/view.rb', line 136 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.
123 124 125 126 127 128 |
# File 'lib/mongo/search_index/view.rb', line 123 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) Operation::UpdateSearchIndex.new(spec).execute(next_primary, context: execution_context) end |