Module: Litesearch::Model::ClassMethods
- Defined in:
- lib/litestack/litesearch/model.rb
Instance Method Summary collapse
-
#create_instance(row) ⇒ Object
create a new instance of self with the row as an argument.
- #drop_index! ⇒ Object
- #index_name ⇒ Object
- #index_name_for_table(table) ⇒ Object
- #litesearch ⇒ Object
- #rebuild_index! ⇒ Object
- #search_all(term, options = {}) ⇒ Object
Instance Method Details
#create_instance(row) ⇒ Object
create a new instance of self with the row as an argument
107 108 109 |
# File 'lib/litestack/litesearch/model.rb', line 107 def create_instance(row) new(row) end |
#drop_index! ⇒ Object
63 64 65 |
# File 'lib/litestack/litesearch/model.rb', line 63 def drop_index! get_connection.search_index(index_name).drop! end |
#index_name ⇒ Object
98 99 100 |
# File 'lib/litestack/litesearch/model.rb', line 98 def index_name "#{table_name}_search_idx" end |
#index_name_for_table(table) ⇒ Object
102 103 104 |
# File 'lib/litestack/litesearch/model.rb', line 102 def index_name_for_table(table) "#{table}_search_idx" end |
#litesearch ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/litestack/litesearch/model.rb', line 43 def litesearch idx = get_connection.search_index(index_name) do |schema| schema.type :backed schema.table table_name.to_sym yield schema schema.post_init @schema = schema # save the schema end if !defined?(Sequel::Model).nil? && ancestors.include?(Sequel::Model) Sequel::Model.search_models[name] = self elsif !defined?(ActiveRecord::Base).nil? && ancestors.include?(ActiveRecord::Base) ActiveRecord::Base.search_models[name] = self end idx end |
#rebuild_index! ⇒ Object
59 60 61 |
# File 'lib/litestack/litesearch/model.rb', line 59 def rebuild_index! get_connection.search_index(index_name).rebuild! end |
#search_all(term, options = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/litestack/litesearch/model.rb', line 67 def search_all(term, = {}) [:offset] ||= 0 [:limit] ||= 25 [:term] = term selects = [] if (models = [: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 ot the query .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[: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 |