class AgnosticBackend::Cloudsearch::Indexer

Constants

MAX_PAYLOAD_SIZE_IN_BYTES

Public Instance Methods

delete(document_id) click to toggle source
# File lib/agnostic_backend/cloudsearch/indexer.rb, line 13
def delete(document_id)
  delete_all([document_id])
end
delete_all(document_ids) click to toggle source
# File lib/agnostic_backend/cloudsearch/indexer.rb, line 17
def delete_all(document_ids)
  documents = document_ids.map do |document_id|
    {"type" => 'delete',
     "id" => document_id}
  end
  publish_all(documents)
end
publish(document) click to toggle source
# File lib/agnostic_backend/cloudsearch/indexer.rb, line 26
def publish(document)
  publish_all([document])
end
publish_all(documents) click to toggle source
# File lib/agnostic_backend/cloudsearch/indexer.rb, line 30
def publish_all(documents)
  return if documents.empty?
  payload = ActiveSupport::JSON.encode(documents)
  raise PayloadLimitExceededError.new if payload_too_heavy? payload
  with_exponential_backoff Aws::CloudSearch::Errors::Throttling do
    client.upload_documents(
      documents: payload,
      content_type:'application/json'
    )
  end
end

Private Instance Methods

add_metadata_to(document) click to toggle source
# File lib/agnostic_backend/cloudsearch/indexer.rb, line 75
def add_metadata_to(document)
  {
      "type" => "add",
      "id" => document["id"].to_s,
      "fields" => document,
  }
end
client() click to toggle source
# File lib/agnostic_backend/cloudsearch/indexer.rb, line 44
def client
  index.cloudsearch_domain_client
end
date_format(document) click to toggle source
# File lib/agnostic_backend/cloudsearch/indexer.rb, line 65
def date_format(document)
  document.each do |k, v|
    if v.is_a?(Time)
      document[k] = v.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
    elsif v.is_a?(Array) && v.all?{|e| e.is_a?(Time)}
      document[k] = v.map{|e| e.utc.strftime("%Y-%m-%dT%H:%M:%SZ")}
    end
  end
end
payload_too_heavy?(payload) click to toggle source
# File lib/agnostic_backend/cloudsearch/indexer.rb, line 83
def payload_too_heavy?(payload)
  payload.bytesize > MAX_PAYLOAD_SIZE_IN_BYTES
end
prepare(document) click to toggle source
# File lib/agnostic_backend/cloudsearch/indexer.rb, line 48
def prepare(document)
  raise IndexingError.new "Document does not have an ID field" unless document["id"].present?
  document
end
transform(document) click to toggle source
# File lib/agnostic_backend/cloudsearch/indexer.rb, line 53
def transform(document)
  return {} if document.empty?

  document = flatten document
  document = reject_blank_values_from document
  document = convert_bool_values_to_string_in document
  document = date_format document
  document = add_metadata_to document
  document

end