Module: Litesearch::Model::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#create_instance(row) ⇒ Object

create a new instance of self with the row as an argument



132
133
134
# File 'lib/litestack/litesearch/model.rb', line 132

def create_instance(row)
  new(row)
end

#drop_index!Object



88
89
90
# File 'lib/litestack/litesearch/model.rb', line 88

def drop_index!
  get_connection.search_index(index_name).drop!
end

#index_nameObject



123
124
125
# File 'lib/litestack/litesearch/model.rb', line 123

def index_name
  "#{table_name}_search_idx"
end

#index_name_for_table(table) ⇒ Object



127
128
129
# File 'lib/litestack/litesearch/model.rb', line 127

def index_name_for_table(table)
  "#{table}_search_idx"
end

#litesearchObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/litestack/litesearch/model.rb', line 44

def litesearch
  # it is possible that this code is running when there is no table created yet
  if !defined?(ActiveRecord::Base).nil? && ancestors.include?(ActiveRecord::Base)
    unless table_exists?
      # capture the schema block
      @schema = ::Litesearch::Schema.new
      @schema.model_class = self if @schema.respond_to? :model_class
      @schema.type :backed 
      @schema.table table_name.to_sym
      yield @schema
      @schema.post_init
      @schema_not_created = true
      after_initialize do
        if self.class.instance_variable_get(:@schema_not_created)
          self.class.get_connection.search_index(self.class.index_name) do |schema|
            @schema.model_class = self.class if @schema.respond_to? :model_class
            schema.merge(self.class.instance_variable_get(:@schema))
          end
          self.class.instance_variable_set(:@schema_not_created, false)
        end
      end
      return nil
    end
  end
  idx = get_connection.search_index(index_name) do |schema|
    schema.type :backed
    schema.table table_name.to_sym
    schema.model_class = self if schema.respond_to? :model_class
    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



84
85
86
# File 'lib/litestack/litesearch/model.rb', line 84

def rebuild_index!
  get_connection.search_index(index_name).rebuild!
end

#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