Module: ES::Index::Model::ClassMethods

Defined in:
lib/es/index/model.rb

Overview

end

Instance Method Summary collapse

Instance Method Details

#esObject



43
44
45
# File 'lib/es/index/model.rb', line 43

def es
  ES::Index::Client.connection
end

#es_analysis(&block) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/es/index/model.rb', line 95

def es_analysis(&block)
  @es_analysis ||= {}
  if block
    @es_analysis = block.call
  else
    self.es_index_model
    @es_analysis
  end
end

#es_create_index(options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/es/index/model.rb', line 57

def es_create_index(options={})
  es_delete_index if es_index_exists?
  index_req_body = {
    settings: es_settings,
    analysis: es_analysis,
    mappings: {
      es_type.to_sym => {
        properties: es_mapping
      }
    }
  }
  ES::Index::Client.connection.indices.create(
    index: (options[:index_name] || es_index),
    body: index_req_body
  )
end

#es_delete_index(options = {}) ⇒ Object



53
54
55
# File 'lib/es/index/model.rb', line 53

def es_delete_index(options={})
  ES::Index::Client.connection.indices.delete({ index: (options[:index_name] || es_index) })
end

#es_id(&block) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/es/index/model.rb', line 151

def es_id(&block)
  @es_id ||= lambda {|o| o.id }
  if block
    @es_id = block
  else
    self.es_index_model
    @es_id
  end
end

#es_if(&block) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/es/index/model.rb', line 142

def es_if(&block)
  if block
    @es_if = block
  else
    self.es_index_model
    @es_if
  end
end

#es_import(options = {}) ⇒ Object

:batch_size :index_name :scan_filter



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
# File 'lib/es/index/model.rb', line 183

def es_import(options={})
  unless self.es_index_exists?(options)
    self.es_create_index(options)
  end

  # Use 1/4 or read provision
  read_provision = self.dynamo_table.table_schema[:provisioned_throughput][:read_capacity_units]
  raise "read_provision not set for class!" unless read_provision
  default_batch_size = (self.read_provision / 2.0).floor
  batch_size = options[:batch_size] || default_batch_size
  puts "Indexing via scan with batch size of #{batch_size}..."

  # :consumed_capacity
  scan_idx = 0
  results_hash = {}
  while scan_idx == 0 || (results_hash && results_hash[:last_evaluated_key])
    puts "Batch iteration #{scan_idx+1}..."
    scan_options = {:batch => batch_size, :manual_batching => true, :return_consumed_capacity => :total}
    scan_options.merge!(:exclusive_start_key => results_hash[:last_evaluated_key]) if results_hash[:last_evaluated_key]
    scan_options.merge!(:scan_filter => options[:scan_filter]) if options[:scan_filter]
    results_hash = self.scan(scan_options)
    unless results_hash[:results].blank?
      puts "Indexing #{results_hash[:results].size} results..."
      results_hash[:results].each do |r|
        r.update_es_index(options)
      end
    end

    # If more results to scan, sleep to throttle...
    #   Local Dynamo is not returning consumed_capacity 2014-01-12
    if results_hash[:last_evaluated_key] && results_hash[:consumed_capacity]
      # try to keep read usage under 50% of read_provision
      sleep_time = results_hash[:consumed_capacity][:capacity_units].to_f / (read_provision / 2.0)
      puts "Sleeping for #{sleep_time}..."
      sleep(sleep_time)
    end
    
    scan_idx += 1
  end
end

#es_index(val = nil) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/es/index/model.rb', line 124

def es_index(val=nil)
  if val
    @es_index = val
  else
    self.es_index_model
    @es_index || self.name.to_s.underscore.pluralize
  end
end

#es_index_exists?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/es/index/model.rb', line 47

def es_index_exists?(options={})
  ES::Index::Client.connection.indices.exists({ index: (options[:index_name] || es_index) })
end

#es_index_model(options = {}, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/es/index/model.rb', line 74

def es_index_model(options={}, &block)
  if block
    @es_index_block ||= block
  else
    unless @es_index_configged
      @es_index_block.call
      @es_index_configged = true
    end
  end
end

#es_mapping(&block) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/es/index/model.rb', line 85

def es_mapping(&block)
  @es_mapping ||= {}
  if block
    @es_mapping = block.call
  else
    self.es_index_model
    @es_mapping
  end
end

#es_results_to_models(&block) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/es/index/model.rb', line 161

def es_results_to_models(&block)
  if block
    @es_results_to_models = block
  else
    self.es_index_model
    @es_results_to_models
  end
end

#es_search(body = {}, options = {}) ⇒ Object



224
225
226
227
228
229
230
# File 'lib/es/index/model.rb', line 224

def es_search(body={}, options={})
  SearchResponse.new(ES::Index::Client.connection.search({
    index: (options[:index_name] || self.es_index),
    type: (options[:type] || self.es_type),
    body: body
  }), self)
end

#es_settings(&block) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/es/index/model.rb', line 105

def es_settings(&block)
  @es_settings ||= {}
  if block
    @es_settings = block.call
  else
    self.es_index_model
    @es_settings
  end
end

#es_suggest(body = {}, options = {}) ⇒ Object



232
233
234
235
236
237
238
# File 'lib/es/index/model.rb', line 232

def es_suggest(body={}, options={})
  SearchResponse.new(ES::Index::Client.connection.suggest({
    index: (options[:index_name] || self.es_index),
    type: (options[:type] || self.es_type),
    body: body
  }), self)
end

#es_ttl(&block) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/es/index/model.rb', line 133

def es_ttl(&block)
  if block
    @es_ttl = block
  else
    self.es_index_model
    @es_ttl
  end
end

#es_type(val = nil) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/es/index/model.rb', line 115

def es_type(val=nil)
  if val
    @es_type = val
  else
    self.es_index_model
    @es_type || self.name.to_s.underscore
  end
end

#to_es_json(&block) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/es/index/model.rb', line 170

def to_es_json(&block)
  @to_es_json ||= lambda {|o| { id: o.id } }
  if block
    @to_es_json = block
  else
    self.es_index_model
    @to_es_json
  end
end