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.



150
151
152
# File 'lib/elastictastic/basic_document.rb', line 150

def id
  @id
end

#versionObject

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

#attributesObject



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

#indexObject



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

#inspectObject



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
  embeds.each_pair do |attr, value|
    inspected << ", #{attr}: #{value.inspect}"
  end
  inspected << ">"
end

#reloadObject



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