Module: ElasticSearchable::Indexing::ClassMethods
- Defined in:
- lib/elastic_searchable/index.rb
Instance Method Summary collapse
-
#clean_index ⇒ Object
delete all documents of this type in the index www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/delete_mapping/.
- #create_index ⇒ Object
-
#delete_id_from_index(id) ⇒ Object
delete one record from the index www.elasticsearch.com/docs/elasticsearch/rest_api/delete/.
-
#delete_index ⇒ Object
deletes the entire index www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/delete_index/.
-
#index_path(action = nil) ⇒ Object
helper method to generate elasticsearch url for this index.
-
#index_type_path(action = nil) ⇒ Object
helper method to generate elasticsearch url for this object type.
-
#refresh_index ⇒ Object
explicitly refresh the index, making all operations performed since the last refresh available for search.
-
#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.
-
#update_index_mapping ⇒ Object
configure the index for this type www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/put_mapping/.
Instance Method Details
#clean_index ⇒ Object
delete all documents of this type in the index www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/delete_mapping/
6 7 8 |
# File 'lib/elastic_searchable/index.rb', line 6 def clean_index ElasticSearchable.request :delete, index_type_path end |
#create_index ⇒ Object
20 21 22 23 24 25 |
# File 'lib/elastic_searchable/index.rb', line 20 def create_index = {} .merge! :settings => self.[:index_options] if self.[:index_options] .merge! :mappings => {index_type => self.[:mapping]} if self.[:mapping] ElasticSearchable.request :put, index_path, :json_body => end |
#delete_id_from_index(id) ⇒ Object
delete one record from the index www.elasticsearch.com/docs/elasticsearch/rest_api/delete/
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_index ⇒ Object
deletes the entire index www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/delete_index/
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_index ⇒ Object
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( = {}) self.update_index_mapping .reverse_merge! :page => 1, :per_page => 1000 scope = .delete(:scope) || self page = [:page] per_page = [: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.}]" 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.}" ElasticSearchable.logger.warn e end page += 1 records = scope.limit(per_page).offset(per_page* (page-1)).all end end |
#update_index_mapping ⇒ Object
configure the index for this type www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/put_mapping/
12 13 14 15 16 |
# File 'lib/elastic_searchable/index.rb', line 12 def update_index_mapping if mapping = self.[:mapping] ElasticSearchable.request :put, index_type_path('_mapping'), :json_body => {index_type => mapping} end end |