Module: Redis::Search::ClassMethods

Defined in:
lib/redis-search/base.rb

Instance Method Summary collapse

Instance Method Details

#prefix_match(q, opts = {}) ⇒ Object



146
147
148
# File 'lib/redis-search/base.rb', line 146

def prefix_match(q, opts = {})
  Redis::Search.complete(self.name, q, opts)
end

#redis_search(opts = {}) ⇒ Object

Config redis-search index for Model

Params:

title_field   Query field for Search
alias_field   Alias field for search, can accept multi field (String or Array type) it type is String, redis-search will split by comma
ext_fields    What kind fields do you need inlucde to search indexes
score_field   Give a score for search sort, need Integer value, default is `created_at`


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/redis-search/base.rb', line 120

def redis_search(opts = {})
  opts[:title_field] ||= :title
  opts[:alias_field] ||= nil
  opts[:ext_fields] ||= []
  opts[:score_field] ||= :created_at
  opts[:condition_fields] ||= []
  opts[:class_name] ||= nil

  # Add score field to ext_fields
  opts[:ext_fields] += [opts[:score_field]]

  # Add condition fields to ext_fields
  opts[:ext_fields] += opts[:condition_fields] if opts[:condition_fields].is_a?(Array)

  # store Model name to indexed_models for Rake tasks
  Search.indexed_models = [] if Search.indexed_models.nil?
  Search.indexed_models << self

  class_variable_set('@@redis_search_options'.freeze, opts)
end

#redis_search_index(opts = {}) ⇒ Object



141
142
143
144
# File 'lib/redis-search/base.rb', line 141

def redis_search_index(opts = {})
  Kernel.warn 'DEPRECATION WARNING: redis_search_index is deprecated, use redis_search instead. '
  redis_search(opts)
end

#redis_search_index_batch_create(batch_size = 1000) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/redis-search/base.rb', line 150

def redis_search_index_batch_create(batch_size = 1000)
  count = 0
  if ancestors.collect(&:to_s).include?('ActiveRecord::Base'.freeze)
    find_in_batches(batch_size: batch_size) do |items|
      _redis_search_reindex_items(items)
      count += items.count
    end
  elsif included_modules.collect(&:to_s).include?('Mongoid::Document'.freeze)
    self.all.each_slice(batch_size) do |items|
      _redis_search_reindex_items(items)
      count += items.count
    end
  else
    puts 'skiped, not support this ORM in current.'
  end

  count
end