Class: Elasticity::Document

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

Defined Under Namespace

Classes: Config, NotConfigured

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Document

Creates a new Document instance with the provided attributes.



146
147
148
# File 'lib/elasticity/document.rb', line 146

def initialize(attributes = {})
  super(attributes)
end

Instance Attribute Details

#_idObject

Define common attributes for all documents



143
144
145
# File 'lib/elasticity/document.rb', line 143

def _id
  @_id
end

#highlightedObject

Define common attributes for all documents



143
144
145
# File 'lib/elasticity/document.rb', line 143

def highlighted
  @highlighted
end

Class Method Details

.bulk_index(documents) ⇒ Object

Bulk index the provided documents



134
135
136
137
138
139
140
# File 'lib/elasticity/document.rb', line 134

def self.bulk_index(documents)
  self.strategy.bulk do |b|
    documents.each do |doc|
      b.index(self.document_type, doc._id, doc.to_document)
    end
  end
end

.configure {|@config| ... } ⇒ Object

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

Yields:



11
12
13
14
15
# File 'lib/elasticity/document.rb', line 11

def self.configure
  @config = Config.new
  @config.strategy = Strategies::SingleIndex
  yield(@config)
end

.create_indexObject

Creates the index for this document



52
53
54
# File 'lib/elasticity/document.rb', line 52

def self.create_index
  self.strategy.create_if_undefined(settings: Elasticity.config.settings, mappings: { document_type => mapping })
end

.delete(id) ⇒ Object

Removes one specific document from the index.



124
125
126
# File 'lib/elasticity/document.rb', line 124

def self.delete(id)
  self.strategy.delete_document(document_type, id)
end

.delete_by_search(search) ⇒ Object

Removes entries based on a search



129
130
131
# File 'lib/elasticity/document.rb', line 129

def self.delete_by_search(search)
  self.strategy.delete_by_query(document_type, search.body)
end

.delete_indexObject

Deletes the index



62
63
64
# File 'lib/elasticity/document.rb', line 62

def self.delete_index
  self.strategy.delete
end

.document_typeObject

Document type



34
35
36
37
38
39
40
# File 'lib/elasticity/document.rb', line 34

def self.document_type
  if @config.nil? || @config.document_type.blank?
    raise NotConfigured, "#{self} has not been configured, make sure you call the configure method"
  end

  @config.document_type
end

.flush_indexObject

Flushes the index, forcing any writes



83
84
85
# File 'lib/elasticity/document.rb', line 83

def self.flush_index
  self.strategy.flush
end

.from_hit(hit_data) ⇒ Object

Creates a instance of a document from a ElasticSearch hit data.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/elasticity/document.rb', line 88

def self.from_hit(hit_data)
  attrs = hit_data["_source"].merge(_id: hit_data['_id'])

  if hit_data["highlight"]
    highlighted_attrs = attrs.dup
    attrs_set = Set.new

    hit_data["highlight"].each do |name, v|
      name = name.gsub(/\..*\z/, '')
      next if attrs_set.include?(name)
      highlighted_attrs[name] = v
      attrs_set << name
    end

    highlighted = new(highlighted_attrs)
  end

  new(attrs.merge(highlighted: highlighted))
end

.get(id) ⇒ Object

Fetches one specific document from the index by ID.



117
118
119
120
121
# File 'lib/elasticity/document.rb', line 117

def self.get(id)
  if doc = self.strategy.get_document(document_type, id)
    new(doc["_source"].merge(_id: doc['_id']))
  end
end

.index_exists?Boolean

Does the index exist?

Returns:

  • (Boolean)


67
68
69
# File 'lib/elasticity/document.rb', line 67

def self.index_exists?
  !self.strategy.missing?
end

.mappingObject

Document type



43
44
45
46
47
48
49
# File 'lib/elasticity/document.rb', line 43

def self.mapping
  if @config.nil? || @config.mapping.blank?
    raise NotConfigured, "#{self} has not been configured, make sure you call the configure method"
  end

  @config.mapping
end

.recreate_indexObject

Re-creates the index for this document



57
58
59
# File 'lib/elasticity/document.rb', line 57

def self.recreate_index
  self.strategy.recreate(settings: Elasticity.config.settings, mappings: { document_type => mapping })
end

.ref_index_nameObject

Gets the index name to be used when you need to reference the index somewhere. This depends on the strategy being used, but it always refers to the search index.



73
74
75
# File 'lib/elasticity/document.rb', line 73

def self.ref_index_name
  self.strategy.ref_index_name
end

.remap!Object

Remap



78
79
80
# File 'lib/elasticity/document.rb', line 78

def self.remap!
  self.strategy.remap(settings: Elasticity.config.settings, mappings: { document_type => mapping })
end

.search(body) ⇒ Object

Searches the index using the parameters provided in the body hash, following the same structure Elasticsearch expects. Returns a DocumentSearch object.



111
112
113
114
# File 'lib/elasticity/document.rb', line 111

def self.search(body)
  search = self.strategy.search(self.document_type, body)
  Search::DocumentProxy.new(search, self)
end

.strategyObject

Returns the stategy class being used. Check Elasticity::Strategies for more information.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/elasticity/document.rb', line 19

def self.strategy
  if @config.nil? || @config.strategy.nil?
    raise NotConfigured, "#{self} has not been configured, make sure you call the configure method"
  end

  return @strategy if defined?(@strategy)

  if namespace = Elasticity.config.namespace
    index_base_name = "#{namespace}_#{@config.index_base_name}"
  end

  @strategy = @config.strategy.new(Elasticity.config.client, index_base_name)
end

Instance Method Details

#==(other) ⇒ Object

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



157
158
159
160
161
162
163
164
# File 'lib/elasticity/document.rb', line 157

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



150
151
152
153
154
# File 'lib/elasticity/document.rb', line 150

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

#created?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/elasticity/document.rb', line 182

def created?
  @created || false
end

#deleteObject



178
179
180
# File 'lib/elasticity/document.rb', line 178

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)


169
170
171
# File 'lib/elasticity/document.rb', line 169

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.



174
175
176
# File 'lib/elasticity/document.rb', line 174

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