module ICFS::Elastic

Shared Elasticsearch methods

Public Instance Methods

create(maps) click to toggle source

Create ES indices @param maps [Hash] symbol to Elasticsearch mapping

# File lib/icfs/elastic.rb, line 68
def create(maps)
  head = {'Content-Type' => 'application/json'}.freeze
  maps.each do |ix, map|
    url = @map[ix]
    resp = @es.run_request(:put, url, map, head)
    if !resp.success?
      puts 'URL: %s' % url
      puts map
      puts resp.body
      raise('Elasticsearch index create failed: %s' % ix.to_s)
    end
  end
end

Private Instance Methods

_read(ix, id) click to toggle source

Read an item

@param ix [Symbol] the index to read @param id [String] the id to read

@return [String] JSON encoded object

# File lib/icfs/elastic.rb, line 32
def _read(ix, id)
  url = '%s/_doc/%s/_source' % [ @map[ix], CGI.escape(id)]
  resp = @es.run_request(:get, url, '', {})
  if resp.status == 404
    return nil
  elsif !resp.success?
    raise('Elasticsearch read failed')
  end
  return resp.body
end
_write(ix, id, item) click to toggle source

Write an item

@param ix [Symbol] the index to write @param id [String] the id to write @param item [String] JSON encoded object to write

# File lib/icfs/elastic.rb, line 51
def _write(ix, id, item)
  url = '%s/_doc/%s' % [ @map[ix], CGI.escape(id)]
  head = {'Content-Type' => 'application/json'}.freeze
  resp = @es.run_request(:put, url, item, head)
  if !resp.success?
    raise('Elasticsearch index failed')
  end
end