Module: ElasticSearchable::ActiveRecordExtensions::LocalMethods

Extended by:
ActiveSupport::Concern
Defined in:
lib/elastic_searchable/active_record_extensions.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#as_json_for_indexObject

document to index in elasticsearch can be overridden by implementing class to customize the content



216
217
218
219
220
221
222
# File 'lib/elastic_searchable/active_record_extensions.rb', line 216

def as_json_for_index
  original_include_root_in_json = self.class.include_root_in_json
  self.class.include_root_in_json = false
  return self.as_json self.class.elastic_options[:json]
ensure
  self.class.include_root_in_json = original_include_root_in_json
end

#percolate(percolator_query = nil) ⇒ Object

percolate this object to see what registered searches match can be done on transient/non-persisted objects! can be done automatically when indexing using :percolate => true config option www.elasticsearch.org/blog/2011/02/08/percolator.html



234
235
236
237
238
239
# File 'lib/elastic_searchable/active_record_extensions.rb', line 234

def percolate(percolator_query = nil)
  body = {:doc => self.as_json_for_index}
  body[:query] = percolator_query if percolator_query
  response = ElasticSearchable.request :get, self.class.index_mapping_path('_percolate'), :json_body => body
  @percolations = response['matches'] || []
end

#percolationsObject

retuns list of percolation matches found during indexing usable when the model is configured with an :after_index callback



195
196
197
# File 'lib/elastic_searchable/active_record_extensions.rb', line 195

def percolations
  @percolations || []
end

#reindex(lifecycle = nil) ⇒ Object

reindex the object in elasticsearch fires after_index callbacks after operation is complete see www.elasticsearch.org/guide/reference/api/index_.html



202
203
204
205
206
207
208
209
210
211
212
# File 'lib/elastic_searchable/active_record_extensions.rb', line 202

def reindex(lifecycle = nil)
  query = {}
  query[:percolate] = "*" if _percolate_callbacks.any?
  response = ElasticSearchable.request :put, self.class.index_mapping_path(self.id), :query => query, :json_body => self.as_json_for_index

  self.index_lifecycle = lifecycle ? lifecycle.to_sym : nil
  run_callbacks :index

  @percolations = response['matches'] || []
  run_callbacks :percolate if @percolations.any?
end

#should_index?Boolean

flag to tell if this instance should be indexed

Returns:

  • (Boolean)


225
226
227
228
# File 'lib/elastic_searchable/active_record_extensions.rb', line 225

def should_index?
  [self.class.elastic_options[:if]].flatten.compact.all? { |m| evaluate_elastic_condition(m) } &&
  ![self.class.elastic_options[:unless]].flatten.compact.any? { |m| evaluate_elastic_condition(m) }
end