Method: Litesearch::Model::ClassMethods#search_all

Defined in:
lib/litestack/litesearch/model.rb

#search_all(term, options = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/litestack/litesearch/model.rb', line 92

def search_all(term, options = {})
  options[:offset] ||= 0
  options[:limit] ||= 25
  options[:term] = term
  selects = []
  if (models = options[:models])
    models_hash = {}
    models.each do |model|
      models_hash[model.name] = model
    end
  else
    models_hash = search_models
  end
  # remove the models from the options hash before passing it to the query
  options.delete(:models)
  models_hash.each do |name, klass|
    selects << "SELECT '#{name}' AS model, rowid, -rank AS search_rank FROM #{index_name_for_table(klass.table_name)}(:term)"
  end
  conn = get_connection
  sql = selects.join(" UNION ") << " ORDER BY search_rank DESC LIMIT :limit OFFSET :offset"
  result = []
  rs = conn.query(sql, options) # , options[:limit], options[:offset])
  rs.each_hash do |row|
    obj = models_hash[row["model"]].fetch_row(row["rowid"])
    obj.search_rank = row["search_rank"]
    result << obj
  end
  rs.close
  result
end