Module: Elastictastic::BasicDocument
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/elastictastic/basic_document.rb
Overview
The top-level module mixed in to classes which will be mapped as ElasticSearch documents. Note that most people will want to use the Document mixin, which extends BasicDocument with ActiveModel functionality such as validations, lifecycle hooks, observers, mass-assignment security, etc. The BasicDocument module is exposed directly for those who wish to avoid the performance penalty associated with ActiveModel functionality, or those who wish to only mix in the ActiveModel modules they need.
Most of the functionality for BasicDocument is provided by submodules; see below.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #attributes ⇒ Object
-
#elasticsearch_hit=(hit) ⇒ Object
:nodoc:.
- #index ⇒ Object
- #initialize(attributes = {}) ⇒ Object
- #inspect ⇒ Object
- #reload ⇒ Object
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
150 151 152 |
# File 'lib/elastictastic/basic_document.rb', line 150 def id @id end |
#version ⇒ Object
Returns the value of attribute version.
151 152 153 |
# File 'lib/elastictastic/basic_document.rb', line 151 def version @version end |
Instance Method Details
#==(other) ⇒ Object
194 195 196 |
# File 'lib/elastictastic/basic_document.rb', line 194 def ==(other) index == other.index && id == other.id end |
#attributes ⇒ Object
198 199 200 |
# File 'lib/elastictastic/basic_document.rb', line 198 def attributes { :id => id, :index => index.name } end |
#elasticsearch_hit=(hit) ⇒ Object
:nodoc:
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/elastictastic/basic_document.rb', line 164 def elasticsearch_hit=(hit) #:nodoc: @id = hit['_id'] @index = Index.new(hit['_index']) @version = hit['_version'] persisted! doc = {} doc.merge!(hit['_source']) if hit['_source'] fields = hit['fields'] if fields unflattened_fields = Util.unflatten_hash(fields.reject { |k, v| v.nil? }) if unflattened_fields.has_key?('_source') doc.merge!(unflattened_fields.delete('_source')) end doc.merge!(unflattened_fields) end self.elasticsearch_doc=(doc) end |
#index ⇒ Object
189 190 191 192 |
# File 'lib/elastictastic/basic_document.rb', line 189 def index return @index if defined? @index @index = Index.default end |
#initialize(attributes = {}) ⇒ Object
153 154 155 |
# File 'lib/elastictastic/basic_document.rb', line 153 def initialize(attributes = {}) self.class.current_scope.initialize_instance(self) end |
#inspect ⇒ Object
202 203 204 205 206 207 208 209 210 211 |
# File 'lib/elastictastic/basic_document.rb', line 202 def inspect inspected = "#<#{self.class.name} id: #{id}, index: #{index.name}" attributes.each_pair do |attr, value| inspected << ", #{attr}: #{value.inspect}" end .each_pair do |attr, value| inspected << ", #{attr}: #{value.inspect}" end inspected << ">" end |
#reload ⇒ Object
157 158 159 160 161 162 |
# File 'lib/elastictastic/basic_document.rb', line 157 def reload params = {} params['routing'] = @_parent_id if @_parent_id self.elasticsearch_hit = Elastictastic.client.get(index, self.class.type, id, params) end |