Class: Mongo::SearchIndex::View

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • collection (Collection)

    The collection.

  • options (Hash) (defaults to: {})

    The options that configure the behavior of the view.

Options Hash (options):

  • :id (String)

    The specific index id to query (optional)

  • :name (String)

    The name of the specific index to query (optional)

  • :aggregate (Hash)

    The options hash to send to the aggregate command when querying the available indexes.

Raises:

  • (ArgumentError)


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_optionsHash (readonly)

Returns the options hash to use for the aggregate command when querying the available indexes.

Returns:

  • (Hash)

    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 (readonly)

Returns the collection this view belongs to.

Returns:



12
13
14
# File 'lib/mongo/search_index/view.rb', line 12

def collection
  @collection
end

#requested_index_idnil | String (readonly)

Returns the index id to query.

Returns:

  • (nil | String)

    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_namenil | String (readonly)

Returns the index name to query.

Returns:

  • (nil | String)

    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.

Parameters:

  • indexes (Array<Hash>)

    The description of the indexes to create. Each element of the list must be a hash with a definition key, and an optional name key.

Returns:

  • (Array<String>)

    the names of the new search indexes.



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.

Parameters:

  • definition (Hash)

    The definition of the search index.

  • name (nil | String) (defaults to: nil)

    The name to give the new search index.

Returns:

  • (String)

    the name of the new search index.



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.

Parameters:

  • id (String) (defaults to: nil)

    the id of the index to drop

  • name (String) (defaults to: nil)

    the name of the index to drop

Returns:



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.

Parameters:

  • block (Proc)

    if given, each search index will be yieleded to the block.

Returns:

  • (self | Enumerator)

    if a block is given, self is returned. Otherwise, an enumerator will be returned.



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

Returns:

  • (true | false)

    whether the enumerable is empty or not.



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.

Parameters:

  • definition (Hash)

    the definition to replace the given search index with.

  • id (nil | String) (defaults to: nil)

    the id of the search index to update

  • name (nil | String) (defaults to: nil)

    the name of the search index to update

Returns:



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