Module: IndexedSearch::Index::InstanceMethods

Defined in:
lib/indexed_search/index.rb

Overview

ClassMethods

Instance Method Summary collapse

Instance Method Details

#collect_search_ranksObject



278
279
280
281
282
283
284
285
# File 'lib/indexed_search/index.rb', line 278

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



233
234
235
236
237
238
239
240
241
242
# File 'lib/indexed_search/index.rb', line 233

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



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

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

#id_for_indexObject



287
288
289
# File 'lib/indexed_search/index.rb', line 287

def id_for_index
  send self.class.id_for_index_attr
end

#make_search_insertion_data(ranks) ⇒ Object



290
291
292
293
294
295
296
# File 'lib/indexed_search/index.rb', line 290

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 = nil) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/indexed_search/index.rb', line 297

def make_search_update_data(ranks, search_entries=nil)
  updates = {}
  deletions = []
  count_decrs = []
  rank_changes = []
  sp = search_priority.round(15)
  (search_entries || self.search_entries).each do |hit|
    if ranks.has_key?(hit.word_id)
      upd = {}
      if hit.rank != ranks[hit.word_id]
        upd[:rank]        = ranks[hit.word_id]
        rank_changes      << hit.word_id
      end
      upd[:row_priority]  = sp     if hit.row_priority  != sp
      updates[hit.id]     = upd    unless upd.empty?
      ranks.delete(hit.word_id)
    else
      deletions << hit.id
      count_decrs << hit.word_id
      rank_changes << hit.word_id
    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



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

def model_id
  self.class.model_id
end

#search_entriesObject



272
273
274
# File 'lib/indexed_search/index.rb', line 272

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

#update_search_index(do_quickly = false) ⇒ Object

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



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/indexed_search/index.rb', line 244

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



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

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