Module: IndexedSearch::Index::InstanceMethods

Defined in:
lib/indexed_search/index.rb

Overview

ClassMethods

Instance Method Summary collapse

Instance Method Details

#collect_search_ranksObject



266
267
268
269
270
271
272
273
# File 'lib/indexed_search/index.rb', line 266

def collect_search_ranks
  wrd_rnk_map = Hash.new(0)
  search_index_info.each { |txt, amnt| IndexedSearch::Query.split_into_words(txt).each { |w| wrd_rnk_map[w] += amnt } }
  wrd_id_map = IndexedSearch::Word.word_id_map(wrd_rnk_map.keys)
  srch_rnks = {}
  wrd_rnk_map.each { |wrd, rnk| srch_rnks[wrd_id_map[wrd]] = rnk }
  srch_rnks
end

#create_search_index(do_quickly = false) ⇒ Object



221
222
223
224
225
226
227
228
229
230
# File 'lib/indexed_search/index.rb', line 221

def create_search_index(do_quickly=false)
  IndexedSearch::Entry.transaction do
    srch_rnks = collect_search_ranks
    IndexedSearch::Entry.import(self.class.search_insertion_headings, make_search_insertion_data(srch_rnks), :validate => false)
    unless do_quickly
      IndexedSearch::Word.incr_counts_by_ids(srch_rnks.keys)
      IndexedSearch::Word.update_ranks_by_ids(srch_rnks.keys)
    end
  end
end

#delete_search_indexObject



253
254
255
256
257
258
# File 'lib/indexed_search/index.rb', line 253

def delete_search_index
  IndexedSearch::Entry.transaction do
    search_entries.delete_all
    #IndexedSearch::Word.fix_counts_orphans_and_ranks
  end
end

#id_for_indexObject



278
279
280
# File 'lib/indexed_search/index.rb', line 278

def id_for_index
  send self.class.id_for_index_attr
end

#make_search_insertion_data(ranks) ⇒ Object



281
282
283
284
285
286
287
# File 'lib/indexed_search/index.rb', line 281

def make_search_insertion_data(ranks)
  id = id_for_index
  idx = ((id << 8) | model_id)
  mid = model_id
  pri = search_priority.round(15)
  ranks.collect { |wid, rnk| [wid, idx, mid, id, rnk, pri] }
end

#make_search_update_data(ranks, search_entries_data = nil) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/indexed_search/index.rb', line 288

def make_search_update_data(ranks, search_entries_data=nil)
  updates = {}
  deletions = []
  count_decrs = []
  rank_changes = []
  sp = search_priority.round(15)
  (search_entries_data || self.search_entries_data).each do |hit|
    if ranks.has_key?(hit[1])
      upd = {}
      if hit[2] != ranks[hit[1]]
        upd[:rank]        = ranks[hit[1]]
        rank_changes      << hit[1]
      end
      upd[:row_priority]  = sp     if hit[3]  != sp
      updates[hit[0]]     = upd    unless upd.empty?
      ranks.delete(hit[1])
    else
      deletions << hit[0]
      count_decrs << hit[1]
      rank_changes << hit[1]
    end
  end
  # at this point, whatever is left in the ranks variable should be inserted as new entries
  rank_changes += ranks.keys
  [ranks, updates, deletions, count_decrs, rank_changes]
end

#model_idObject



263
264
265
# File 'lib/indexed_search/index.rb', line 263

def model_id
  self.class.model_id
end

#search_entriesObject



260
261
262
# File 'lib/indexed_search/index.rb', line 260

def search_entries
  self.class.search_entries.where(:modelrowid => id_for_index)
end

#search_entries_dataObject



275
276
277
# File 'lib/indexed_search/index.rb', line 275

def search_entries_data
  search_entries.values_of(*self.class.search_entry_data_headings)
end

#update_search_index(do_quickly = false) ⇒ Object

updates and deletes are often called in a loop to manipulate multiple rows at once



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/indexed_search/index.rb', line 232

def update_search_index(do_quickly=false)
  (inserts, updates, deletions, count_decrs, rank_changes) = make_search_update_data(collect_search_ranks)
  unless inserts.blank? && updates.blank? && deletions.blank?
    IndexedSearch::Entry.transaction do
      IndexedSearch::Entry.import(self.class.search_insertion_headings, make_search_insertion_data(inserts), :validate => false) unless inserts.blank?
      updates.invert_multi.each { |vals, ids| IndexedSearch::Entry.where(:id => ids).update_all(vals) }
      IndexedSearch::Entry.where(:id => deletions).delete_all unless deletions.blank?
      unless do_quickly
        IndexedSearch::Word.incr_counts_by_ids(inserts.keys)  unless inserts.blank?
        IndexedSearch::Word.decr_counts_by_ids(count_decrs)   unless count_decrs.blank?
        IndexedSearch::Word.delete_empty                      unless count_decrs.blank?
        IndexedSearch::Word.update_ranks_by_ids(rank_changes) unless rank_changes.blank?
      end
    end
  end
end

#update_search_priorityObject



248
249
250
251
252
# File 'lib/indexed_search/index.rb', line 248

def update_search_priority
  IndexedSearch::Entry.transaction do
    search_entries.update_all(:row_priority => search_priority.round(15))
  end
end