Module: Mongoid::SearchIndexable::ClassMethods
- Defined in:
- lib/mongoid/search_indexable.rb
Overview
Implementations for the feature’s class-level methods.
Instance Method Summary collapse
-
#create_search_indexes ⇒ Array<String>
Request the creation of all registered search indices.
-
#remove_search_index(name: nil, id: nil) ⇒ Object
Removes the search index specified by the given name or id.
-
#remove_search_indexes ⇒ Object
Request the removal of all registered search indexes.
-
#search_index(name_or_defn, defn = nil) ⇒ Object
Adds an index definition for the provided single or compound keys.
-
#search_indexes(options = {}) ⇒ Object
A convenience method for querying the search indexes available on the current model’s collection.
-
#wait_for_search_indexes(names, interval: 5) {|SearchIndexable::Status| ... } ⇒ Object
Waits for the named search indexes to be created.
Instance Method Details
#create_search_indexes ⇒ Array<String>
Request the creation of all registered search indices. Note that the search indexes are created asynchronously, and may take several minutes to be fully available.
65 66 67 68 69 |
# File 'lib/mongoid/search_indexable.rb', line 65 def create_search_indexes return if search_index_specs.empty? collection.search_indexes.create_many(search_index_specs) end |
#remove_search_index(name: nil, id: nil) ⇒ Object
Removes the search index specified by the given name or id. Either name OR id must be given, but not both.
107 108 109 110 111 112 113 114 |
# File 'lib/mongoid/search_indexable.rb', line 107 def remove_search_index(name: nil, id: nil) logger.info( "MONGOID: Removing search index '#{name || id}' " \ "on collection '#{collection.name}'." ) collection.search_indexes.drop_one(name: name, id: id) end |
#remove_search_indexes ⇒ Object
It would be nice if this could remove ONLY the search indexes
Request the removal of all registered search indexes. Note that the search indexes are removed asynchronously, and may take several minutes to be fully deleted.
that have been declared on the model, but because the model may not name the index, we can’t guarantee that we’ll know the name or id of the corresponding indexes. It is not unreasonable to assume, though, that the intention is for the model to declare, one-to-one, all desired search indexes, so removing all search indexes ought to suffice. If a specific index or set of indexes needs to be removed instead, consider using search_indexes.each with remove_search_index.
128 129 130 131 132 |
# File 'lib/mongoid/search_indexable.rb', line 128 def remove_search_indexes search_indexes.each do |spec| remove_search_index id: spec['id'] end end |
#search_index(name_or_defn, defn = nil) ⇒ Object
Adds an index definition for the provided single or compound keys.
147 148 149 150 151 152 153 |
# File 'lib/mongoid/search_indexable.rb', line 147 def search_index(name_or_defn, defn = nil) name = name_or_defn name, defn = nil, name if name.is_a?(Hash) spec = { definition: defn }.tap { |s| s[:name] = name.to_s if name } search_index_specs.push(spec) end |
#search_indexes(options = {}) ⇒ Object
A convenience method for querying the search indexes available on the current model’s collection.
98 99 100 |
# File 'lib/mongoid/search_indexable.rb', line 98 def search_indexes( = {}) collection.search_indexes() end |
#wait_for_search_indexes(names, interval: 5) {|SearchIndexable::Status| ... } ⇒ Object
Waits for the named search indexes to be created.
78 79 80 81 82 83 84 85 86 |
# File 'lib/mongoid/search_indexable.rb', line 78 def wait_for_search_indexes(names, interval: 5) loop do status = Status.new(get_indexes(names)) yield status if block_given? break if status.ready? sleep interval end end |