Module: ElasticSearchable::Indexing::ClassMethods

Defined in:
lib/elastic_searchable/index.rb

Instance Method Summary collapse

Instance Method Details

#clean_indexObject



6
7
8
# File 'lib/elastic_searchable/index.rb', line 6

def clean_index
  ElasticSearchable.request :delete, index_type_path
end

#create_indexObject



20
21
22
23
24
25
# File 'lib/elastic_searchable/index.rb', line 20

def create_index
  options = {}
  options.merge! :settings => self.elastic_options[:index_options] if self.elastic_options[:index_options]
  options.merge! :mappings => {index_type => self.elastic_options[:mapping]} if self.elastic_options[:mapping]
  ElasticSearchable.request :put, index_path, :json_body => options
end

#delete_id_from_index(id) ⇒ Object



43
44
45
46
47
# File 'lib/elastic_searchable/index.rb', line 43

def delete_id_from_index(id)
  ElasticSearchable.request :delete, index_type_path(id)
rescue ElasticSearchable::ElasticError => e
  ElasticSearchable.logger.warn e
end

#delete_indexObject



37
38
39
# File 'lib/elastic_searchable/index.rb', line 37

def delete_index
  ElasticSearchable.request :delete, index_path
end

#index_path(action = nil) ⇒ Object

helper method to generate elasticsearch url for this index



55
56
57
# File 'lib/elastic_searchable/index.rb', line 55

def index_path(action = nil)
  ['', index_name, action].compact.join('/')
end

#index_type_path(action = nil) ⇒ Object

helper method to generate elasticsearch url for this object type



50
51
52
# File 'lib/elastic_searchable/index.rb', line 50

def index_type_path(action = nil)
  index_path [index_type, action].compact.join('/')
end

#refresh_indexObject

explicitly refresh the index, making all operations performed since the last refresh available for search

www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/refresh/



31
32
33
# File 'lib/elastic_searchable/index.rb', line 31

def refresh_index
  ElasticSearchable.request :post, index_path('_refresh')
end

#reindex(options = {}) ⇒ Object

reindex all records using bulk api see www.elasticsearch.org/guide/reference/api/bulk.html options:

:scope - scope to use for looking up records to reindex. defaults to self (all)
:page - page/batch to begin indexing at. defaults to 1
:per_page - number of records to index per batch. defaults to 1000

TODO: move this to AREL relation to remove the options scope param



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
# File 'lib/elastic_searchable/index.rb', line 67

def reindex(options = {})
  self.update_index_mapping
  options.reverse_merge! :page => 1, :per_page => 1000
  scope = options.delete(:scope) || self
  page = options[:page]
  per_page = options[:per_page]
  records = scope.limit(per_page).offset(per_page * (page -1)).all
  while records.any? do
    ElasticSearchable.logger.debug "reindexing batch ##{page}..."
    actions = []
    records.each do |record|
      next unless record.should_index?
      begin
        doc = ElasticSearchable.encode_json(record.as_json_for_index)
        actions << ElasticSearchable.encode_json({:index => {'_index' => index_name, '_type' => index_type, '_id' => record.id}})
        actions << doc
      rescue => e
        ElasticSearchable.logger.warn "Unable to bulk index record: #{record.inspect} [#{e.message}]"
      end
    end
    begin
      ElasticSearchable.request(:put, '/_bulk', :body => "\n#{actions.join("\n")}\n") if actions.any?
    rescue ElasticError => e
      ElasticSearchable.logger.warn "Error indexing batch ##{page}: #{e.message}"
      ElasticSearchable.logger.warn e
    end

    page += 1
    records = scope.limit(per_page).offset(per_page* (page-1)).all
  end
end

#update_index_mappingObject



12
13
14
15
16
# File 'lib/elastic_searchable/index.rb', line 12

def update_index_mapping
  if mapping = self.elastic_options[:mapping]
    ElasticSearchable.request :put, index_type_path('_mapping'), :json_body => {index_type => mapping}
  end
end