Module: Tanker

Extended by:
Configuration
Defined in:
lib/tanker.rb,
lib/tanker/railtie.rb,
lib/tanker/utilities.rb,
lib/tanker/pagination.rb,
lib/tanker/configuration.rb,
lib/tanker/pagination/kaminari.rb,
lib/tanker/pagination/will_paginate.rb

Defined Under Namespace

Modules: ClassMethods, Configuration, InstanceMethods, Pagination, Utilities Classes: BadConfiguration, ModelConfig, NoBlockGiven, NoIndexName, NotConfigured, Railtie, SearchState

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Configuration

configuration, configuration=

Class Attribute Details

.included_inObject (readonly)

Returns the value of attribute included_in.



33
34
35
# File 'lib/tanker.rb', line 33

def included_in
  @included_in
end

Class Method Details

.apiObject



35
36
37
# File 'lib/tanker.rb', line 35

def api
  @api ||= IndexTank::ApiClient.new(Tanker.configuration[:url])
end

.batch_update(records) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/tanker.rb', line 54

def batch_update(records)
  return false if records.empty?
  data = records.map do |record|
    options = record.tanker_index_options
    options.merge!( :docid => record.it_doc_id, :fields => record.tanker_index_data )
    options
  end
  records.first.class.tanker_index.add_documents(data)
end

.included(klass) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tanker.rb', line 39

def included(klass)
  configuration # raises error if not defined

  @included_in ||= []
  @included_in << klass
  @included_in.uniq!

  klass.send :include, InstanceMethods
  klass.extend ClassMethods

  class << klass
    define_method(:per_page) { 10 } unless respond_to?(:per_page)
  end
end

.instantiate(search_state) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/tanker.rb', line 143

def instantiate(search_state)
  results = search_state.results
  fetch = search_state.fetch
  snippets = search_state.snippets
  paginate = search_state.paginate

  categories = results['facets'] if results.has_key?('facets')

  instantiated_results = if (fetch || snippets)
    instantiate_results_from_results(results, fetch, snippets)
  else
    instantiate_results_from_db(results)
  end
  paginate === false ? instantiated_results : Pagination.create(instantiated_results, results['matches'], paginate, categories)
end

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



138
139
140
141
# File 'lib/tanker.rb', line 138

def search(models, query, options = {})
  search_state = search_results(models, query, options)
  instantiate(search_state)
end

.search_results(models, query, options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/tanker.rb', line 64

def search_results(models, query, options = {})
  ids      = []
  models   = [models].flatten.uniq
  index    = models.first.tanker_index
  query    = query.join(' ') if Array === query
  snippets = options.delete(:snippets)
  fetch    = options.delete(:fetch)
  paginate = extract_setup_paginate_options(options, :page => 1, :per_page => models.first.per_page)

  if (index_names = models.map(&:tanker_config).map(&:index_name).uniq).size > 1
    raise "You can't search across multiple indexes in one call (#{index_names.inspect})"
  end

  # move conditions into the query body
  if conditions = options.delete(:conditions)
    conditions.each do |field, value|
      value = [value].flatten.compact
      value.each do |item|
        query += " #{field}:(#{item})"
      end
    end
  end

  # rephrase filter_functions
  if filter_functions = options.delete(:filter_functions)
    filter_functions.each do |function_number, ranges|
      options[:"filter_function#{function_number}"] = ranges.map{|r|r.join(':')}.join(',')
    end
  end

  # rephrase filter_docvars
  if filter_docvars = options.delete(:filter_docvars)
    filter_docvars.each do |var_number, ranges|
      options[:"filter_docvar#{var_number}"] = ranges.map{|r|r.join(':')}.join(',')
    end
  end

  # fetch values from index tank or just the type and id to instace results localy
  options[:fetch]   =  "__type,__id"
  options[:fetch]   += ",#{fetch.join(',')}" if fetch
  options[:snippet] = snippets.join(',') if snippets

  # convert category_filters to a json string
  options[:category_filters] = options[:category_filters].to_json if options[:category_filters]

  search_on_fields = models.map{|model| model.tanker_config.indexes.map{|arr| arr[0]}.uniq}.flatten.uniq.join(":(#{query.to_s}) OR ")

  query = "(#{search_on_fields}:(#{query.to_s}) OR __any:(#{query.to_s})) __type:(#{models.map(&:name).map {|name| "\"#{name.split('::').join(' ')}\"" }.join(' OR ')})"
  options = { :start => paginate[:per_page] * (paginate[:page] - 1), :len => paginate[:per_page] }.merge(options) if paginate
  results = index.search(query, options)
  SearchState.new(results, fetch, snippets, paginate) 
end