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

Instance Method Summary collapse

Instance Attribute Details

#idObject

Returns the value of attribute id.



156
157
158
# File 'lib/elastictastic/basic_document.rb', line 156

def id
  @id
end

#versionObject

Returns the value of attribute version.



157
158
159
# File 'lib/elastictastic/basic_document.rb', line 157

def version
  @version
end

Instance Method Details

#==(other) ⇒ Object



200
201
202
# File 'lib/elastictastic/basic_document.rb', line 200

def ==(other)
  index == other.index && self.class == other.class && id == other.id
end

#attributesObject



204
205
206
# File 'lib/elastictastic/basic_document.rb', line 204

def attributes
  { :id => id, :index => index.name }
end

#elasticsearch_hit=(hit) ⇒ Object

:nodoc:



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/elastictastic/basic_document.rb', line 170

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

#indexObject



195
196
197
198
# File 'lib/elastictastic/basic_document.rb', line 195

def index
  return @index if defined? @index
  @index = Index.default
end

#initialize(attributes = {}) ⇒ Object



159
160
161
# File 'lib/elastictastic/basic_document.rb', line 159

def initialize(attributes = {})
  self.class.current_scope.initialize_instance(self)
end

#inspectObject



208
209
210
211
212
213
214
215
216
217
# File 'lib/elastictastic/basic_document.rb', line 208

def inspect
  inspected = "#<#{self.class.name} id: #{id}, index: #{index.name}"
  attributes.each_pair do |attr, value|
    inspected << ", #{attr}: #{value.inspect}"
  end
  embeds.each_pair do |attr, value|
    inspected << ", #{attr}: #{value.inspect}"
  end
  inspected << ">"
end

#reloadObject



163
164
165
166
167
168
# File 'lib/elastictastic/basic_document.rb', line 163

def reload
  params = {}
  params['routing'] = @_parent_id if @_parent_id
  self.elasticsearch_hit =
    Elastictastic.client.get(index, self.class.type, id, params)
end