Class: Searchkick::Index
- Inherits:
-
Object
- Object
- Searchkick::Index
- Defined in:
- lib/searchkick/index.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #alias_exists? ⇒ Boolean
- #all_indices(unaliased: false) ⇒ Object
- #batches_left ⇒ Object
- #bulk_delete(records) ⇒ Object
- #bulk_index(records) ⇒ Object (also: #import)
- #bulk_update(records, method_name) ⇒ Object
-
#clean_indices ⇒ Object
remove old indices that start w/ index_name.
-
#conversions_fields ⇒ Object
private.
- #create(body = {}) ⇒ Object
- #create_index(index_options: nil) ⇒ Object
- #delete ⇒ Object
- #document_type(record) ⇒ Object
- #exists? ⇒ Boolean
- #import_scope(relation, **options) ⇒ Object
- #index_options ⇒ Object
-
#initialize(name, options = {}) ⇒ Index
constructor
A new instance of Index.
-
#klass_document_type(klass, ignore_type = false) ⇒ Object
private.
-
#locations_fields ⇒ Object
private.
-
#mapping ⇒ Object
call to_h for consistent results between elasticsearch gem 7 and 8 could do for all API calls, but just do for ones where return value is focus for now.
- #promote(new_name, update_refresh_interval: false) ⇒ Object (also: #swap)
- #refresh ⇒ Object
- #refresh_interval ⇒ Object
-
#reindex(object, method_name: nil, full: false, **options) ⇒ Object
note: this is designed to be used internally so it does not check object matches index class.
-
#reindex_queue ⇒ Object
queue.
- #reload_synonyms ⇒ Object
- #remove(record) ⇒ Object
- #retrieve(record) ⇒ Object
- #search_id(record) ⇒ Object
-
#settings ⇒ Object
call to_h for consistent results between elasticsearch gem 7 and 8.
- #similar_record(record, **options) ⇒ Object
- #store(record) ⇒ Object
-
#suggest_fields ⇒ Object
private.
- #tokens(text, options = {}) ⇒ Object
- #total_docs ⇒ Object
- #update_record(record, method_name) ⇒ Object
- #update_settings(settings) ⇒ Object
-
#uuid ⇒ Object
private.
Constructor Details
#initialize(name, options = {}) ⇒ Index
Returns a new instance of Index.
5 6 7 8 9 |
# File 'lib/searchkick/index.rb', line 5 def initialize(name, = {}) @name = name @options = @klass_document_type = {} # cache end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/searchkick/index.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/searchkick/index.rb', line 3 def @options end |
Instance Method Details
#alias_exists? ⇒ Boolean
37 38 39 |
# File 'lib/searchkick/index.rb', line 37 def alias_exists? client.indices.exists_alias name: name end |
#all_indices(unaliased: false) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/searchkick/index.rb', line 106 def all_indices(unaliased: false) indices = begin if client.indices.respond_to?(:get_alias) client.indices.get_alias(index: "#{name}*") else client.indices.get_aliases end rescue => e raise e unless Searchkick.not_found_error?(e) {} end indices = indices.select { |_k, v| v.empty? || v["aliases"].empty? } if unaliased indices.select { |k, _v| k =~ /\A#{Regexp.escape(name)}_\d{14,17}\z/ }.keys end |
#batches_left ⇒ Object
268 269 270 |
# File 'lib/searchkick/index.rb', line 268 def batches_left relation_indexer.batches_left end |
#bulk_delete(records) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/searchkick/index.rb', line 149 def bulk_delete(records) return if records.empty? notify_bulk(records, "Delete") do queue_delete(records) end end |
#bulk_index(records) ⇒ Object Also known as: import
157 158 159 160 161 162 163 |
# File 'lib/searchkick/index.rb', line 157 def bulk_index(records) return if records.empty? notify_bulk(records, "Import") do queue_index(records) end end |
#bulk_update(records, method_name) ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/searchkick/index.rb', line 166 def bulk_update(records, method_name) return if records.empty? notify_bulk(records, "Update") do queue_update(records, method_name) end end |
#clean_indices ⇒ Object
remove old indices that start w/ index_name
123 124 125 126 127 128 129 |
# File 'lib/searchkick/index.rb', line 123 def clean_indices indices = all_indices(unaliased: true) indices.each do |index| Index.new(index).delete end indices end |
#conversions_fields ⇒ Object
private
286 287 288 289 290 291 |
# File 'lib/searchkick/index.rb', line 286 def conversions_fields @conversions_fields ||= begin conversions = Array([:conversions]) conversions.map(&:to_s) + conversions.map(&:to_sym) end end |
#create(body = {}) ⇒ Object
15 16 17 |
# File 'lib/searchkick/index.rb', line 15 def create(body = {}) client.indices.create index: name, body: body end |
#create_index(index_options: nil) ⇒ Object
257 258 259 260 261 262 |
# File 'lib/searchkick/index.rb', line 257 def create_index(index_options: nil) ||= self. index = Index.new("#{name}_#{Time.now.strftime('%Y%m%d%H%M%S%L')}", @options) index.create() index end |
#delete ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/searchkick/index.rb', line 19 def delete if alias_exists? # can't call delete directly on aliases in ES 6 indices = client.indices.get_alias(name: name).keys client.indices.delete index: indices else client.indices.delete index: name end end |
#document_type(record) ⇒ Object
178 179 180 |
# File 'lib/searchkick/index.rb', line 178 def document_type(record) RecordData.new(self, record).document_type end |
#exists? ⇒ Boolean
29 30 31 |
# File 'lib/searchkick/index.rb', line 29 def exists? client.indices.exists index: name end |
#import_scope(relation, **options) ⇒ Object
264 265 266 |
# File 'lib/searchkick/index.rb', line 264 def import_scope(relation, **) relation_indexer.reindex(relation, **) end |
#index_options ⇒ Object
11 12 13 |
# File 'lib/searchkick/index.rb', line 11 def IndexOptions.new(self). end |
#klass_document_type(klass, ignore_type = false) ⇒ Object
private
273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/searchkick/index.rb', line 273 def klass_document_type(klass, ignore_type = false) @klass_document_type[[klass, ignore_type]] ||= begin if !ignore_type && klass.searchkick_klass.[:_type] type = klass.searchkick_klass.[:_type] type = type.call if type.respond_to?(:call) type else klass.model_name.to_s.underscore end end end |
#locations_fields ⇒ Object
private
299 300 301 302 303 304 |
# File 'lib/searchkick/index.rb', line 299 def locations_fields @locations_fields ||= begin locations = Array([:locations]) locations.map(&:to_s) + locations.map(&:to_sym) end end |
#mapping ⇒ Object
call to_h for consistent results between elasticsearch gem 7 and 8 could do for all API calls, but just do for ones where return value is focus for now
43 44 45 |
# File 'lib/searchkick/index.rb', line 43 def mapping client.indices.get_mapping(index: name).to_h end |
#promote(new_name, update_refresh_interval: false) ⇒ Object Also known as: swap
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/searchkick/index.rb', line 77 def promote(new_name, update_refresh_interval: false) if update_refresh_interval new_index = Index.new(new_name, @options) settings = [:settings] || {} refresh_interval = (settings[:index] && settings[:index][:refresh_interval]) || "1s" new_index.update_settings(index: {refresh_interval: refresh_interval}) end old_indices = begin client.indices.get_alias(name: name).keys rescue => e raise e unless Searchkick.not_found_error?(e) {} end actions = old_indices.map { |old_name| {remove: {index: old_name, alias: name}} } + [{add: {index: new_name, alias: name}}] client.indices.update_aliases body: {actions: actions} end |
#refresh ⇒ Object
33 34 35 |
# File 'lib/searchkick/index.rb', line 33 def refresh client.indices.refresh index: name end |
#refresh_interval ⇒ Object
52 53 54 |
# File 'lib/searchkick/index.rb', line 52 def refresh_interval index_settings["refresh_interval"] end |
#reindex(object, method_name: nil, full: false, **options) ⇒ Object
note: this is designed to be used internally so it does not check object matches index class
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/searchkick/index.rb', line 214 def reindex(object, method_name: nil, full: false, **) if object.is_a?(Array) # note: purposefully skip full return reindex_records(object, method_name: method_name, **) end if !object.respond_to?(:searchkick_klass) raise Error, "Cannot reindex object" end scoped = Searchkick.relation?(object) # call searchkick_klass for inheritance relation = scoped ? object.all : Searchkick.scope(object.searchkick_klass).all refresh = .fetch(:refresh, !scoped) .delete(:refresh) if method_name || (scoped && !full) mode = .delete(:mode) || :inline raise ArgumentError, "unsupported keywords: #{.keys.map(&:inspect).join(", ")}" if .any? # import only import_scope(relation, method_name: method_name, mode: mode) self.refresh if refresh true else async = .delete(:async) if async if async.is_a?(Hash) && async[:wait] # TODO warn in 5.1 # Searchkick.warn "async option is deprecated - use mode: :async, wait: true instead" [:wait] = true unless .key?(:wait) else # TODO warn in 5.1 # Searchkick.warn "async option is deprecated - use mode: :async instead" end [:mode] ||= :async end full_reindex(relation, **) end end |
#reindex_queue ⇒ Object
queue
206 207 208 |
# File 'lib/searchkick/index.rb', line 206 def reindex_queue ReindexQueue.new(name) end |
#reload_synonyms ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/searchkick/index.rb', line 190 def reload_synonyms if Searchkick.opensearch? client.transport.perform_request "POST", "_plugins/_refresh_search_analyzers/#{CGI.escape(name)}" else raise Error, "Requires Elasticsearch 7.3+" if Searchkick.server_below?("7.3.0") begin client.transport.perform_request("GET", "#{CGI.escape(name)}/_reload_search_analyzers") rescue => e raise Error, "Requires non-OSS version of Elasticsearch" if Searchkick.not_allowed_error?(e) raise e end end end |
#remove(record) ⇒ Object
137 138 139 140 141 |
# File 'lib/searchkick/index.rb', line 137 def remove(record) notify(record, "Remove") do queue_delete([record]) end end |
#retrieve(record) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/searchkick/index.rb', line 97 def retrieve(record) record_data = RecordData.new(self, record).record_data # remove underscore = record_data.to_h { |k, v| [k.to_s.sub(/\A_/, "").to_sym, v] } client.get()["_source"] end |
#search_id(record) ⇒ Object
174 175 176 |
# File 'lib/searchkick/index.rb', line 174 def search_id(record) RecordData.new(self, record).search_id end |
#settings ⇒ Object
call to_h for consistent results between elasticsearch gem 7 and 8
48 49 50 |
# File 'lib/searchkick/index.rb', line 48 def settings client.indices.get_settings(index: name).to_h end |
#similar_record(record, **options) ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/searchkick/index.rb', line 182 def similar_record(record, **) [:per_page] ||= 10 [:similar] = [RecordData.new(self, record).record_data] [:models] ||= [record.class] unless .key?(:model) Searchkick.search("*", **) end |
#store(record) ⇒ Object
131 132 133 134 135 |
# File 'lib/searchkick/index.rb', line 131 def store(record) notify(record, "Store") do queue_index([record]) end end |
#suggest_fields ⇒ Object
private
294 295 296 |
# File 'lib/searchkick/index.rb', line 294 def suggest_fields @suggest_fields ||= Array([:suggest]).map(&:to_s) end |
#tokens(text, options = {}) ⇒ Object
60 61 62 |
# File 'lib/searchkick/index.rb', line 60 def tokens(text, = {}) client.indices.analyze(body: {text: text}.merge(), index: name)["tokens"].map { |t| t["token"] } end |
#total_docs ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/searchkick/index.rb', line 64 def total_docs response = client.search( index: name, body: { query: {match_all: {}}, size: 0 } ) Results.new(nil, response).total_count end |
#update_record(record, method_name) ⇒ Object
143 144 145 146 147 |
# File 'lib/searchkick/index.rb', line 143 def update_record(record, method_name) notify(record, "Update") do queue_update([record], method_name) end end |
#update_settings(settings) ⇒ Object
56 57 58 |
# File 'lib/searchkick/index.rb', line 56 def update_settings(settings) client.indices.put_settings index: name, body: settings end |
#uuid ⇒ Object
private
307 308 309 |
# File 'lib/searchkick/index.rb', line 307 def uuid index_settings["uuid"] end |