Class: IndexedSearch::Entry

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
ResetTable
Defined in:
app/models/indexed_search/entry.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ResetTable

reset_auto_increment, truncate_table

Class Method Details

.count_distinct_rowsObject

count distinct rows found (it’s a function because count ignores any select/group from scope, but not conditions)



72
73
74
# File 'app/models/indexed_search/entry.rb', line 72

def self.count_distinct_rows
  count(:select => 'DISTINCT rowidx')
end

.count_results(query) ⇒ Object

count the total results (with no sorting by ranking, pagination, etc)



25
26
27
# File 'app/models/indexed_search/entry.rb', line 25

def self.count_results(query)
  query.nil? || query.empty? || query.results.empty? ? 0 : matching_query(query.results).count_distinct_rows
end

.delete_orphanedObject

cleanup after deleting from main index extra entries hanging around gives bogus nil results, so best to clean em up also resets auto increment if the entire database is purged



92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/indexed_search/entry.rb', line 92

def self.delete_orphaned
  # delete where models no longer exist
  cnt = where(arel_table[:modelid].not_in(IndexedSearch::Index.models_by_id.keys)).delete_all
  # delete where row ids have been set to NULL
  cnt += where(:modelrowid => nil).delete_all
  # delete for existing models where rows have been deleted
  IndexedSearch::Index.models_by_id.each do |mdlid, mdl|
    cnt += where("`entries`.`modelid`=#{mdlid} AND NOT EXISTS (SELECT * FROM `#{mdl.table_name}` WHERE `#{mdl.table_name}`.`#{mdl.id_for_index_attr}`=`entries`.`modelrowid`)").delete_all
  end
  reset_auto_increment
  cnt
end

.find_results(query, page_size, page_number = 1) ⇒ Object

main entry points for searching database (either whole thing, or scope a limit first) returns results, sorted in ranking order, with pagination support note it actually returns a scope that can be lazily loaded! use #models to convert to actual models



21
22
23
# File 'app/models/indexed_search/entry.rb', line 21

def self.find_results(query, page_size, page_number=1)
  query.nil? || query.empty? || query.results.empty? ? [] : matching_query(query.results).ranked_rows(query.results).paged(page_size, page_number)
end

.modelsObject

get the instantiated results model class from the hit(s) we represent



79
80
81
# File 'app/models/indexed_search/entry.rb', line 79

def self.models
  all.collect { |hit| hit.model }
end

Instance Method Details

#modelObject



82
83
84
# File 'app/models/indexed_search/entry.rb', line 82

def model
  @model ||= model_type.where(model_type.id_for_index_attr => modelrowid).first
end

#model_typeObject



85
86
87
# File 'app/models/indexed_search/entry.rb', line 85

def model_type
  @model_type ||= IndexedSearch::Index.models_by_id[modelid]
end

#to_sObject



105
106
107
# File 'app/models/indexed_search/entry.rb', line 105

def to_s
  model.to_s
end