Module: Escargot

Defined in:
lib/escargot.rb,
lib/escargot/version.rb,
lib/escargot/local_indexing.rb,
lib/escargot/activerecord_ex.rb,
lib/escargot/elasticsearch_ex.rb,
lib/escargot/queue_backend/base.rb,
lib/escargot/distributed_indexing.rb,
lib/escargot/queue_backend/resque.rb

Defined Under Namespace

Modules: ActiveRecordExtensions, AdminIndexVersions, DistributedIndexing, HitExtensions, LocalIndexing, QueueBackend

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.flush_all_indexed_modelsObject



27
28
29
# File 'lib/escargot.rb', line 27

def self.flush_all_indexed_models
  @indexed_models = []
end

.indexed_modelsObject



19
20
21
# File 'lib/escargot.rb', line 19

def self.indexed_models
  @indexed_models || []
end

.queue_backendObject



23
24
25
# File 'lib/escargot.rb', line 23

def self.queue_backend
  @queue ||= Escargot::QueueBackend::Rescue.new
end

.register_model(model) ⇒ Object



12
13
14
15
16
17
# File 'lib/escargot.rb', line 12

def self.register_model(model)
  return unless model.table_exists?
  @indexed_models ||= []
  @indexed_models.delete(model) if @indexed_models.include?(model)
  @indexed_models << model
end

.search(query, options = {}, call_by_instance_method = false) ⇒ Object

search returns a will_paginate collection of ActiveRecord objects for the search results

see ElasticSearch::Api::Index#search for the full list of valid options

note that the collection may include nils if ElasticSearch returns a result hit for a record that has been deleted on the database



57
58
59
60
61
62
63
# File 'lib/escargot.rb', line 57

def self.search(query, options = {}, call_by_instance_method = false)
  hits = Escargot.search_hits(query, options, call_by_instance_method)
  hits_ar = hits.map{|hit| hit.to_activerecord}
  results = WillPaginate::Collection.new(hits.current_page, hits.per_page, hits.total_entries)
  results.replace(hits_ar)
  results
end

.search_count(query = "*", options = {}, call_by_instance_method = false) ⇒ Object

counts the number of results for this query.



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/escargot.rb', line 66

def self.search_count(query = "*", options = {}, call_by_instance_method = false)
  unless call_by_instance_method
    if (options[:classes])
      models = Array(options[:classes])
    else
      register_all_models
      models = @indexed_models
    end
    options = options.merge({:index => models.map(&:index_name).join(',')})
  end
  $elastic_search_client.count(query, options)
end

.search_hits(query, options = {}, call_by_instance_method = false) ⇒ Object

search_hits returns a raw ElasticSearch::Api::Hits object for the search results see #search for the valid options



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/escargot.rb', line 33

def self.search_hits(query, options = {}, call_by_instance_method = false)
  unless call_by_instance_method
    if (options[:classes])
      models = Array(options[:classes])
    else
      register_all_models
      models = @indexed_models
    end
    options = options.merge({:index => models.map(&:index_name).join(',')})
  end
  
  if query.kind_of?(Hash)
    query_dsl = query.delete(:query_dsl)
    query = {:query => query} if (query_dsl.nil? || query_dsl)
  end
  $elastic_search_client.search(query, options)
end