Class: Mongo::SearchIndex::View

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

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)


36
37
38
39
40
41
42
43
44
45
# File 'lib/mongo/search_index/view.rb', line 36

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.



23
24
25
# File 'lib/mongo/search_index/view.rb', line 23

def aggregate_options
  @aggregate_options
end

#collectionMongo::Collection (readonly)

Returns the collection this view belongs to.

Returns:



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

def collection
  @collection
end

#requested_index_idnil | String (readonly)

Returns the index id to query.

Returns:

  • (nil | String)

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

Returns the index name to query.

Returns:

  • (nil | String)

    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.

Parameters:

  • indexes (Array<Hash>)

    The description of the indexes to create. Each element of the list must be a hash with a definition key, an optional name key, and an optional type key. The type key must be one of ‘search’ or ‘vectorSearch’. The default is ‘search’.

Returns:

  • (Array<String>)

    the names of the new search indexes.



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.

Parameters:

  • definition (Hash)

    The definition of the search index.

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

    The name to give the new search index.

  • type (String) (defaults to: 'search')

    The type of the search index. Possible values are ‘search’ and ‘vectorSearch’. The default is ‘search’.

Returns:

  • (String)

    the name of the new search index.



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.

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:



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.

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.



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



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.

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:



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