Class: Elasticity::BaseDocument

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/elasticity/base_document.rb

Direct Known Subclasses

Document, SegmentedDocument

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_explanationObject

Define common attributes for all documents



15
16
17
# File 'lib/elasticity/base_document.rb', line 15

def _explanation
  @_explanation
end

#_idObject

Define common attributes for all documents



15
16
17
# File 'lib/elasticity/base_document.rb', line 15

def _id
  @_id
end

#_scoreObject

Define common attributes for all documents



15
16
17
# File 'lib/elasticity/base_document.rb', line 15

def _score
  @_score
end

#highlightedObject

Define common attributes for all documents



15
16
17
# File 'lib/elasticity/base_document.rb', line 15

def highlighted
  @highlighted
end

#highlighted_attrsObject

Define common attributes for all documents



15
16
17
# File 'lib/elasticity/base_document.rb', line 15

def highlighted_attrs
  @highlighted_attrs
end

#matched_queriesObject

Define common attributes for all documents



15
16
17
# File 'lib/elasticity/base_document.rb', line 15

def matched_queries
  @matched_queries
end

#sortObject

Define common attributes for all documents



15
16
17
# File 'lib/elasticity/base_document.rb', line 15

def sort
  @sort
end

Class Method Details

.configure(&block) ⇒ Object

Configure the given klass, changing default parameters and resetting some of the internal state.



10
11
12
# File 'lib/elasticity/base_document.rb', line 10

def self.configure(&block)
  self.config = IndexConfig.new(Elasticity.config, self.index_config_defaults, &block)
end

Instance Method Details

#==(other) ⇒ Object

Defines equality by comparing the ID and values of each instance variable.



24
25
26
27
28
29
30
31
# File 'lib/elasticity/base_document.rb', line 24

def ==(other)
  return false if other.nil?
  return false if _id != other._id

  instance_variables.all? do |ivar|
    instance_variable_get(ivar) == other.instance_variable_get(ivar)
  end
end

#attributes=(attributes) ⇒ Object



17
18
19
20
21
# File 'lib/elasticity/base_document.rb', line 17

def attributes=(attributes)
  attributes.each do |attr, value|
    self.public_send("#{attr}=", value)
  end
end

#created?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/elasticity/base_document.rb', line 49

def created?
  @created || false
end

#deleteObject



45
46
47
# File 'lib/elasticity/base_document.rb', line 45

def delete
  self.class.delete(self._id)
end

#to_documentObject

IMPLEMENT Returns a hash with the attributes as they should be stored in the index. This will be stored as _source attributes on Elasticsearch.

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/elasticity/base_document.rb', line 36

def to_document
  raise NotImplementedError, "to_document needs to be implemented for #{self.class}"
end

#updateObject

Update this object on the index, creating or updating the document.



41
42
43
# File 'lib/elasticity/base_document.rb', line 41

def update
  self._id, @created = self.class.index_document(_id, to_document)
end