Module: Elasticsearch::Model::Indexing::ClassMethods
- Defined in:
- lib/elasticsearch/model/indexing.rb
Instance Method Summary collapse
-
#create_index!(options = {}) ⇒ Object
Creates an index with correct name, automatically passing ‘settings` and `mappings` defined in the model.
-
#delete_index!(options = {}) ⇒ Object
Deletes the index with corresponding name.
-
#mapping(options = {}, &block) ⇒ Object
(also: #mappings)
Defines mappings for the index.
-
#refresh_index!(options = {}) ⇒ Object
Performs the “refresh” operation for the index (useful e.g. in tests).
-
#settings(settings = {}, &block) ⇒ Object
Define settings for the index.
Instance Method Details
#create_index!(options = {}) ⇒ Object
Creates an index with correct name, automatically passing ‘settings` and `mappings` defined in the model
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/elasticsearch/model/indexing.rb', line 182 def create_index!(={}) target_index = .delete(:index) || self.index_name delete_index!(.merge index: target_index) if [:force] unless ( self.client.indices.exists(index: target_index) rescue false ) begin self.client.indices.create index: target_index, body: { settings: self.settings.to_hash, mappings: self.mappings.to_hash } rescue Exception => e unless e.class.to_s =~ /NotFound/ && [:force] STDERR.puts "[!!!] Error when creating the index: #{e.class}", "#{e.message}" end end else end end |
#delete_index!(options = {}) ⇒ Object
Deletes the index with corresponding name
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/elasticsearch/model/indexing.rb', line 212 def delete_index!(={}) target_index = .delete(:index) || self.index_name begin self.client.indices.delete index: target_index rescue Exception => e unless e.class.to_s =~ /NotFound/ && [:force] STDERR.puts "[!!!] Error when deleting the index: #{e.class}", "#{e.message}" end end end |
#mapping(options = {}, &block) ⇒ Object Also known as: mappings
Defines mappings for the index
The ‘mappings` and `settings` methods are accessible directly on the model class, when it doesn’t already defines them. Use the ‘__elasticsearch__` proxy otherwise.
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/elasticsearch/model/indexing.rb', line 131 def mapping(={}, &block) @mapping ||= Mappings.new(document_type, ) if block_given? @mapping..update() @mapping.instance_eval(&block) return self else @mapping end end |
#refresh_index!(options = {}) ⇒ Object
Performs the “refresh” operation for the index (useful e.g. in tests)
236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/elasticsearch/model/indexing.rb', line 236 def refresh_index!(={}) target_index = .delete(:index) || self.index_name begin self.client.indices.refresh index: target_index rescue Exception => e unless e.class.to_s =~ /NotFound/ && [:force] STDERR.puts "[!!!] Error when refreshing the index: #{e.class}", "#{e.message}" end end end |
#settings(settings = {}, &block) ⇒ Object
Define settings for the index
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/elasticsearch/model/indexing.rb', line 154 def settings(settings={}, &block) @settings ||= Settings.new(settings) @settings.settings.update(settings) unless settings.empty? if block_given? self.instance_eval(&block) return self else @settings end end |