Class: Waistband::Index
- Inherits:
-
Object
- Object
- Waistband::Index
- Defined in:
- lib/waistband/index.rb
Constant Summary collapse
- BODY_SIZE_LIMIT =
100_000
Instance Method Summary collapse
- #alias(alias_name) ⇒ Object
- #alias_exists?(alias_name) ⇒ Boolean
- #client ⇒ Object
- #config ⇒ Object
- #create ⇒ Object
- #create! ⇒ Object
- #delete ⇒ Object
- #delete! ⇒ Object
- #delete_by_query(query) ⇒ Object
- #destroy(id, options = {}) ⇒ Object
- #destroy!(id, options = {}) ⇒ Object
- #exists? ⇒ Boolean
- #find(id, options = {}) ⇒ Object
- #find!(id, options = {}) ⇒ Object
-
#initialize(index_name, options = {}) ⇒ Index
constructor
A new instance of Index.
- #read(id, options = {}) ⇒ Object
- #read!(id, options = {}) ⇒ Object
- #read_result(id, options = {}) ⇒ Object
- #read_result!(id, options = {}) ⇒ Object
- #refresh ⇒ Object
- #save(*args) ⇒ Object
- #save!(*args) ⇒ Object
- #search(body_hash) ⇒ Object
- #update(*args) ⇒ Object
- #update!(*args) ⇒ Object
- #update_all_mappings ⇒ Object
- #update_mapping(type) ⇒ Object
- #update_settings ⇒ Object
Constructor Details
#initialize(index_name, options = {}) ⇒ Index
Returns a new instance of Index.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/waistband/index.rb', line 14 def initialize(index_name, = {}) = .stringify_keys @index_name = index_name @stringify = config['stringify'] @log_level = config['log_level'] # subindexes checks if ['version'].present? # version @version = ['version'] @subs = ['version', @version] elsif ['subs'].present? # subs @subs = [['subs']] if ['subs'].present? @subs = @subs.flatten if @subs.is_a?(Array) end end |
Instance Method Details
#alias(alias_name) ⇒ Object
248 249 250 251 252 253 254 |
# File 'lib/waistband/index.rb', line 248 def alias(alias_name) alias_name = full_alias_name alias_name client.indices.put_alias( index: config_name, name: alias_name ) end |
#alias_exists?(alias_name) ⇒ Boolean
256 257 258 259 260 261 262 |
# File 'lib/waistband/index.rb', line 256 def alias_exists?(alias_name) alias_name = full_alias_name alias_name client.indices.exists_alias( index: config_name, name: alias_name ) end |
#client ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/waistband/index.rb', line 268 def client @client ||= begin _client = if client_config_hash ::Waistband::Client.from_config(client_config_hash) else ::Waistband.config.client end if @log_level && Waistband.config.logger _client.transport.logger = Waistband.config.logger _client.transport.logger.level = @log_level end _client end end |
#config ⇒ Object
264 265 266 |
# File 'lib/waistband/index.rb', line 264 def config ::Waistband.config.index @index_name end |
#create ⇒ Object
73 74 75 76 77 |
# File 'lib/waistband/index.rb', line 73 def create create! rescue ::Waistband::Errors::IndexExists => ex true end |
#create! ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/waistband/index.rb', line 79 def create! ('create') client.indices.create index: config_name, body: config.except('name', 'permissions', 'stringify', 'log_level') rescue Elasticsearch::Transport::Transport::Errors::BadRequest => ex raise ex unless ex..to_s =~ /already_exists_exception/ raise ::Waistband::Errors::IndexExists.new("Index already exists") end |
#delete ⇒ Object
88 89 90 91 92 |
# File 'lib/waistband/index.rb', line 88 def delete delete! rescue ::Waistband::Errors::IndexNotFound => ex true end |
#delete! ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/waistband/index.rb', line 94 def delete! ('delete_index') client.indices.delete index: config_name rescue Elasticsearch::Transport::Transport::Errors::NotFound => ex raise ex unless ex..to_s =~ /index_not_found_exception/ raise ::Waistband::Errors::IndexNotFound.new("Index not found") end |
#delete_by_query(query) ⇒ Object
244 245 246 |
# File 'lib/waistband/index.rb', line 244 def delete_by_query(query) ::Waistband::DeleteByQueryResult.new(client.delete_by_query(index: config_name, body: query)) end |
#destroy(id, options = {}) ⇒ Object
210 211 212 213 214 |
# File 'lib/waistband/index.rb', line 210 def destroy(id, = {}) destroy!(id, ) rescue Elasticsearch::Transport::Transport::Errors::NotFound nil end |
#destroy!(id, options = {}) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/waistband/index.rb', line 216 def destroy!(id, = {}) ('destroy') = .with_indifferent_access type = [:_type] || default_type_name client.delete( index: config_name, id: id, type: type ) end |
#exists? ⇒ Boolean
34 35 36 |
# File 'lib/waistband/index.rb', line 34 def exists? client.indices.exists index: config_name end |
#find(id, options = {}) ⇒ Object
169 170 171 172 173 |
# File 'lib/waistband/index.rb', line 169 def find(id, = {}) find!(id, ) rescue Elasticsearch::Transport::Transport::Errors::NotFound nil end |
#find!(id, options = {}) ⇒ Object
175 176 177 178 |
# File 'lib/waistband/index.rb', line 175 def find!(id, = {}) doc = read!(id, ) doc['_source'] end |
#read(id, options = {}) ⇒ Object
191 192 193 194 195 |
# File 'lib/waistband/index.rb', line 191 def read(id, = {}) read!(id, ) rescue Elasticsearch::Transport::Transport::Errors::NotFound nil end |
#read!(id, options = {}) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/waistband/index.rb', line 197 def read!(id, = {}) ('read') = .with_indifferent_access type = [:_type] || default_type_name client.get( index: config_name, type: type, id: id ).with_indifferent_access end |
#read_result(id, options = {}) ⇒ Object
180 181 182 183 184 |
# File 'lib/waistband/index.rb', line 180 def read_result(id, = {}) read_result!(id, ) rescue Elasticsearch::Transport::Transport::Errors::NotFound nil end |
#read_result!(id, options = {}) ⇒ Object
186 187 188 189 |
# File 'lib/waistband/index.rb', line 186 def read_result!(id, = {}) hit = read!(id, ) ::Waistband::Result.new(hit) end |
#refresh ⇒ Object
38 39 40 |
# File 'lib/waistband/index.rb', line 38 def refresh client.indices.refresh index: config_name end |
#save(*args) ⇒ Object
129 130 131 132 133 |
# File 'lib/waistband/index.rb', line 129 def save(*args) save!(*args) rescue ::Waistband::Errors::UnableToSave => ex false end |
#save!(*args) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/waistband/index.rb', line 103 def save!(*args) ('write') body_hash = args. id = args.first _type = body_hash.delete(:_type) || body_hash.delete('_type') || default_type_name # map everything to strings if need be body_hash = stringify_all(body_hash) if @stringify verify_body_size(config_name, _type, id, body_hash) saved = client.index( index: config_name, type: _type, id: id, body: body_hash ) unless saved['_id'].present? raise ::Waistband::Errors::UnableToSave.new("Unable to save to index: #{config_name}, type: #{_type}, id: #{id}: result: #{saved}") end saved end |
#search(body_hash) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/waistband/index.rb', line 229 def search(body_hash) page, page_size = get_page_info body_hash body_hash = parse_search_body(body_hash) _type = body_hash.delete(:_type) search_hash = {index: config_name, body: body_hash} search_hash[:type] = _type if _type search_hash[:from] = body_hash[:from] if body_hash[:from] search_hash[:size] = body_hash[:size] if body_hash[:size] search_hash = client.search(search_hash) ::Waistband::SearchResults.new(search_hash, page: page, page_size: page_size) end |
#update(*args) ⇒ Object
163 164 165 166 167 |
# File 'lib/waistband/index.rb', line 163 def update(*args) update!(*args) rescue ::Waistband::Errors::UnableToUpdate false end |
#update!(*args) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/waistband/index.rb', line 135 def update!(*args) ('write') body_hash = args. id = args.first _type = body_hash.delete(:_type) || body_hash.delete('_type') || default_type_name # map everything to strings if need be body_hash = stringify_all(body_hash) if @stringify verify_body_size(config_name, _type, id, body_hash) body_hash = { doc: body_hash } saved = client.update( index: config_name, type: _type, id: id, body: body_hash ) unless saved['_id'].present? raise ::Waistband::Errors::UnableToUpdate.new("Unable to update to index: #{config_name}, type: #{_type}, id: #{id}: result: #{saved}") end saved end |
#update_all_mappings ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/waistband/index.rb', line 42 def update_all_mappings ('create') responses = types.map do |type| update_mapping(type).merge('_type' => type) end end |
#update_mapping(type) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/waistband/index.rb', line 50 def update_mapping(type) ('create') properties = config['mappings'][type]['properties'] || {} mapping_hash = {type => {properties: properties}} client.indices.put_mapping( index: config_name, type: type, body: mapping_hash ) end |
#update_settings ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/waistband/index.rb', line 64 def update_settings ('create') client.indices.put_settings( index: config_name, body: settings ) end |