Class: ElasticGraph::Indexer::Operation::Update

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/indexer/operation/update.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.operations_for(event:, destination_index_def:, record_preparer:, update_target:, destination_index_mapping:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/elastic_graph/indexer/operation/update.rb', line 23

def self.operations_for(
  event:,
  destination_index_def:,
  record_preparer:,
  update_target:,
  destination_index_mapping:
)
  return [] if update_target.for_normal_indexing? && !destination_index_def.use_updates_for_indexing?

  prepared_record = record_preparer.prepare_for_index(event["type"], event["record"] || {"id" => event["id"]})

  Support::HashUtil
    .fetch_leaf_values_at_path(prepared_record, update_target.id_source)
    .reject { |id| id.to_s.strip.empty? }
    .uniq
    .map { |doc_id| new(event, prepared_record, destination_index_def, update_target, doc_id, destination_index_mapping) }
end

Instance Method Details

#categorize(response) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/elastic_graph/indexer/operation/update.rb', line 45

def categorize(response)
  update = response.fetch("update")
  status = update.fetch("status")

  if noop_result?(response)
    noop_error_message = message_from_thrown_painless_exception(update)
      &.delete_prefix(UPDATE_WAS_NOOP_MESSAGE_PREAMBLE)

    Result.noop_of(self, noop_error_message)
  elsif (200..299).cover?(status)
    Result.success_of(self)
  else
    error = update.fetch("error")

    further_detail =
      if (more_detail = error["caused_by"])
        # Usually the type/reason details are nested an extra level (`caused_by.caused_by`) but sometimes
        # it's not. I think it's nested when the script itself throws an exception where as it's unnested
        # when the datastore is unable to run the script.
        more_detail = more_detail["caused_by"] if more_detail.key?("caused_by")
        " (#{more_detail["type"]}: #{more_detail["reason"]})"
      else
        "; full response: #{::JSON.pretty_generate(response)}"
      end

    Result.failure_of(self, "#{update_target.script_id}(applied to `#{doc_id}`): #{error.fetch("reason")}#{further_detail}")
  end
end

#descriptionObject



78
79
80
81
82
83
84
# File 'lib/elastic_graph/indexer/operation/update.rb', line 78

def description
  if update_target.type == event.fetch("type")
    "#{update_target.type} update"
  else
    "#{update_target.type} update (from #{event.fetch("type")})"
  end
end

#inspectObject Also known as: to_s



86
87
88
# File 'lib/elastic_graph/indexer/operation/update.rb', line 86

def inspect
  "#<#{self.class.name} event=#{EventID.from_event(event)} target=#{update_target.type}>"
end

#to_datastore_bulkObject



41
42
43
# File 'lib/elastic_graph/indexer/operation/update.rb', line 41

def to_datastore_bulk
  @to_datastore_bulk ||= [{update: }, update_request]
end

#typeObject



74
75
76
# File 'lib/elastic_graph/indexer/operation/update.rb', line 74

def type
  :update
end

#versioned?Boolean

Returns:

  • (Boolean)


91
92
93
94
95
# File 'lib/elastic_graph/indexer/operation/update.rb', line 91

def versioned?
  # We do not track source event versions when applying derived indexing updates, but we do for
  # normal indexing updates, so if the update target is for normal indexing it's a versioned operation.
  update_target.for_normal_indexing?
end