Module: Escargot::ActiveRecordExtensions::ClassMethods
- Defined in:
- lib/escargot/activerecord_ex.rb
Instance Attribute Summary collapse
-
#index_name ⇒ Object
Returns the value of attribute index_name.
-
#update_index_policy ⇒ Object
Returns the value of attribute update_index_policy.
Instance Method Summary collapse
-
#create_index_version ⇒ Object
creates a new index version for this model and sets the mapping options for the type.
- #delete_id_from_index(id, options = {}) ⇒ Object
-
#delete_index ⇒ Object
deletes all index versions for this model and the alias (if exist).
-
#elastic_index(options = {}) ⇒ Object
defines an elastic search index.
- #facets(fields_list, options = {}) ⇒ Object
- #optimize_index ⇒ Object
-
#refresh_index(index_version = nil) ⇒ Object
explicitly refresh the index, making all operations performed since the last refresh available for search.
- #search(query, options = {}) ⇒ Object
- #search_count(query = "*", options = {}) ⇒ Object
- #search_hits(query, options = {}) ⇒ Object
Instance Attribute Details
#index_name ⇒ Object
Returns the value of attribute index_name.
11 12 13 |
# File 'lib/escargot/activerecord_ex.rb', line 11 def index_name @index_name end |
#update_index_policy ⇒ Object
Returns the value of attribute update_index_policy.
12 13 14 |
# File 'lib/escargot/activerecord_ex.rb', line 12 def update_index_policy @update_index_policy end |
Instance Method Details
#create_index_version ⇒ Object
creates a new index version for this model and sets the mapping options for the type
103 104 105 106 107 108 109 |
# File 'lib/escargot/activerecord_ex.rb', line 103 def create_index_version index_version = $elastic_search_client.create_index_version(@index_name, @index_options) if @mapping $elastic_search_client.update_mapping(@mapping, :index => index_version, :type => elastic_search_type) end index_version end |
#delete_id_from_index(id, options = {}) ⇒ Object
130 131 132 133 134 |
# File 'lib/escargot/activerecord_ex.rb', line 130 def delete_id_from_index(id, = {}) [:index] ||= self.index_name [:type] ||= elastic_search_type $elastic_search_client.delete(id.to_s, ) end |
#delete_index ⇒ Object
deletes all index versions for this model and the alias (if exist)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/escargot/activerecord_ex.rb', line 112 def delete_index # set current version to delete alias later current_version = $elastic_search_client.current_index_version(index_name) # deletes any index version and the alias $elastic_search_client.index_versions(index_name).each{|index_version| $elastic_search_client.alias_index(:remove => {index_version => index_name}) if (index_version == current_version) $elastic_search_client.delete_index(index_version) } # and delete the index itself if it exists begin $elastic_search_client.delete_index(index_name) rescue ElasticSearch::RequestError # it's ok, this means that the index doesn't exist end end |
#elastic_index(options = {}) ⇒ Object
defines an elastic search index. Valid options:
:index_name (will default class name using method “underscore”)
:updates, how to to update the contents of the index when a document is changed, valid options are:
- false: do not update the index
- :immediate: update the index but do not refresh it automatically.
With the default settings, this means that the change may take up to 1 second
to be seen by other users.
See: http://www.elasticsearch.com/docs/elasticsearch/index_modules/engine/robin/
This is the default option.
- :immediate_with_refresh: update the index AND ask elasticsearch to refresh it after each
change. This garantuees that the changes will be seen by other users, but may affect
performance.
- :enqueu: enqueue the document id so that a remote worker will update the index
This is the recommended options if you have set up a job queue (such as Resque)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/escargot/activerecord_ex.rb', line 38 def elastic_index( = {}) Escargot.register_model(self) .symbolize_keys! send :include, InstanceMethods @index_name = [:index_name] || self.name.underscore.gsub(/\//,'-') @update_index_policy = .include?(:updates) ? [:updates] : :immediate if @update_index_policy after_save :update_index after_destroy :delete_from_index end @index_options = [:index_options] || {} @mapping = [:mapping] || false end |
#facets(fields_list, options = {}) ⇒ Object
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 |
# File 'lib/escargot/activerecord_ex.rb', line 66 def facets(fields_list, = {}) size = .delete(:size) || 10 fields_list = [fields_list] unless fields_list.kind_of?(Array) if ![:query] [:query] = {:match_all => { } } elsif [:query].kind_of?(String) [:query] = {:query_string => {:query => [:query]}} end [:facets] = {} fields_list.each do |field| [:facets][field] = {:terms => {:field => field, :size => size}} end hits = $elastic_search_client.search(, {:index => self.index_name, :type => elastic_search_type}) out = {} fields_list.each do |field| out[field.to_sym] = {} hits.facets[field.to_s]["terms"].each do |term| out[field.to_sym][term["term"]] = term["count"] end end out end |
#optimize_index ⇒ Object
136 137 138 |
# File 'lib/escargot/activerecord_ex.rb', line 136 def optimize_index $elastic_search_client.optimize(index_name) end |
#refresh_index(index_version = nil) ⇒ 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/
98 99 100 |
# File 'lib/escargot/activerecord_ex.rb', line 98 def refresh_index(index_version = nil) $elastic_search_client.refresh(index_version || index_name) end |
#search(query, options = {}) ⇒ Object
54 55 56 |
# File 'lib/escargot/activerecord_ex.rb', line 54 def search(query, ={}) Escargot.search(query, .merge({:index => self.index_name, :type => elastic_search_type}), true) end |
#search_count(query = "*", options = {}) ⇒ Object
62 63 64 |
# File 'lib/escargot/activerecord_ex.rb', line 62 def search_count(query = "*", = {}) Escargot.search_count(query, .merge({:index => self.index_name, :type => elastic_search_type}), true) end |
#search_hits(query, options = {}) ⇒ Object
58 59 60 |
# File 'lib/escargot/activerecord_ex.rb', line 58 def search_hits(query, = {}) Escargot.search_hits(query, .merge({:index => self.index_name, :type => elastic_search_type}), true) end |