View — Documentation by YARD 0.9.37 (original) (raw)

Class: Mongo::SearchIndex::View

Overview

A class representing a view of search indexes.

Instance Attribute Summary collapse

Instance Method Summarycollapse

Methods included from Collection::Helpers

#do_drop

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, options = {}) @collection = collection @requested_index_id = options[:id] @requested_index_name = options[:name] @aggregate_options = 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

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 @aggregate_options end

#collectionMongo::Collection

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

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

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

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, 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.

51 52 53 # File 'lib/mongo/search_index/view.rb', line 51 def create_one(definition, name: nil, type: 'search') create_many([ { name: name, definition: definition, type: type } ]).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) 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.with(read_concern: {}).aggregate( [ { '$listSearchIndexes' => spec } ], aggregate_options ) 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