Module: ElasticsearchRecord::Persistence::ClassMethods

Defined in:
lib/elasticsearch_record/persistence.rb

Instance Method Summary collapse

Instance Method Details

#_delete_record(constraints) ⇒ Object

removes a persistent entry from the Elasticsearch index NOTICE: We don't want to mess up with the Arel-builder - so data is directly send to the API



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/elasticsearch_record/persistence.rb', line 50

def _delete_record(constraints)
  # build new query
  query = ElasticsearchRecord::Query.new(
    index:     table_name,
    type:      ElasticsearchRecord::Query::TYPE_DELETE,
    arguments: { id: constraints[self.primary_key] },
    refresh:   true)

  # execute query and return total deletes
  connection.delete(query, "#{self} Delete")
end

#_insert_record(values, returning) ⇒ Object

insert a new record into the Elasticsearch index NOTICE: We don't want to mess up with the Arel-builder - so we send new data directly to the API

Parameters:

  • values (ActiveModel::Attribute)

Returns:

  • (Object)

    id



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/elasticsearch_record/persistence.rb', line 10

def _insert_record(values, returning)
  # values is not a "key=>values"-Hash, but a +ActiveModel::Attribute+ - so the casted values gets resolved here
  values = values.transform_values(&:value)

  # resolve & update a auto_increment value, if configured
  _insert_with_auto_increment(values) do |arguments|
    # build new query
    query = ElasticsearchRecord::Query.new(
      index:     table_name,
      type:      ElasticsearchRecord::Query::TYPE_CREATE,
      # IMPORTANT: always exclude possible provided +_id+ field
      body:      values.except('_id'),
      arguments: arguments,
      refresh:   true)

    # execute query and return inserted id
    connection.insert(query, "#{self} Create", returning: returning)
  end
end

#_update_record(values, constraints) ⇒ Object

updates a persistent entry in the Elasticsearch index NOTICE: We don't want to mess up with the Arel-builder - so data is directly send to the API



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/elasticsearch_record/persistence.rb', line 32

def _update_record(values, constraints)
  # values is not a "key=>values"-Hash, but a +ActiveModel::Attribute+ - so we need to resolve the casted values here
  values = values.transform_values(&:value)

  # build new query
  query = ElasticsearchRecord::Query.new(
    index:     table_name,
    type:      ElasticsearchRecord::Query::TYPE_UPDATE,
    body:      { doc: values },
    arguments: { id: constraints[self.primary_key] },
    refresh:   true)

  # execute query and return total updates
  connection.update(query, "#{self} Update")
end