Module: SmartSearch::InstanceMethods

Defined in:
lib/smart_search.rb

Overview

Instance Methods for ActiveRecord

Instance Method Summary collapse

Instance Method Details

#clear_search_tagsObject

Remove search data for the instance from the index



247
248
249
250
251
# File 'lib/smart_search.rb', line 247

def clear_search_tags
  if !self.id.nil?
    SmartSearchTag.connection.execute("DELETE from #{SmartSearchTag.table_name} where `table_name` = '#{self.class.table_name}' and entry_id = #{self.id}") rescue nil
  end
end

#create_search_tagsObject

create search tags for this very record based on the attributes defined in ‘:on’ option passed to the ‘Class.smart_search’ method



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/smart_search.rb', line 176

def create_search_tags
  # storing tags must never fail the systems
  begin
    tags      = []
       
    self.class.tags.each do |tag|

      if !tag.is_a?(Hash)
        tag = {:field_name => tag, :boost => 1, :search_tags => ""}
      else
        tag[:search_tags] = ""
        tag[:boost] ||= 1
      end

      if tag[:field_name].is_a?(Symbol)
        tag[:search_tags] << self.send(tag[:field_name]).to_s
      elsif tag[:field_name].is_a?(String)
        tag_methods = tag[:field_name].split(".")
        tagx = self.send(tag_methods[0])
        tag_methods[1..-1].each do |x|
          tagx = tagx.send(x) rescue ""
        end
        tag[:search_tags] << tagx.to_s
      end

      tag[:search_tags] = tag[:search_tags].split(" ").uniq.join(" ").downcase.clear_html
      tags << tag
    end


    self.clear_search_tags

    # Merge search tags with same boost
    @merged_tags = {}

    tags.each do |t|
      boost = t[:boost]

      if @merged_tags[boost]

        @merged_tags[boost][:field_name] << ",#{t[:field_name]}"
        @merged_tags[boost][:search_tags] << " #{t[:search_tags]}"
      else
        @merged_tags[boost] = {:field_name => "#{t[:field_name]}", :search_tags => t[:search_tags], :boost => boost }
      end

    end

    @merged_tags.values.each do |t|
      if !t[:search_tags].blank? && t[:search_tags].size > 1
        begin
          SmartSearchTag.create(t.merge!(:table_name => self.class.table_name, :entry_id => self.id, :search_tags => t[:search_tags].strip.split(" ").uniq.join(" ")))
        rescue Exception => e
          
        end
      end
    end
      
  rescue Exception => e
    Rails.logger.error "SMART SEARCH FAILED TO TO STORE SEARCH TAGS #{self.class.name} #{self.id}"
    Rails.logger.error e.message
    Rails.logger.error puts e.backtrace
  end
    
    

    

end

#dont_update_search_tags!Object



167
168
169
# File 'lib/smart_search.rb', line 167

def dont_update_search_tags!
  self.dont_update_search_tags = true
end

#result_template_pathObject

Load the result template path for this instance



163
164
165
# File 'lib/smart_search.rb', line 163

def result_template_path
  self.class.result_template_path
end

#update_search_tags?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/smart_search.rb', line 171

def update_search_tags?
  !self.dont_update_search_tags
end