class Cauchy::Elastic::Index

Attributes

name[R]
server[R]

Public Class Methods

new(server, name) click to toggle source
# File lib/cauchy/elastic/index.rb, line 26
def initialize(server, name)
  @server = server
  @name = name
end

Public Instance Methods

alias=(alias_name) click to toggle source
# File lib/cauchy/elastic/index.rb, line 51
def alias=(alias_name)
  server.indices.put_alias index: name, name: alias_name
end
aliases() click to toggle source
# File lib/cauchy/elastic/index.rb, line 35
def aliases
  get 'aliases'
end
close() click to toggle source
# File lib/cauchy/elastic/index.rb, line 91
def close
  server.indices.close index: name
end
create(settings: {}, mappings: {}) click to toggle source
# File lib/cauchy/elastic/index.rb, line 72
def create(settings: {}, mappings: {})
  server.indices.create index: name,
    body: { settings: settings, mappings: mappings }
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e
  if e.message =~ /IndexAlreadyExists/
    raise IndexAlreadyExistsError.new(name)
  else
    raise
  end
end
delete() click to toggle source
# File lib/cauchy/elastic/index.rb, line 83
def delete
  server.indices.delete index: name
end
exists?() click to toggle source
# File lib/cauchy/elastic/index.rb, line 31
def exists?
  server.indices.exists? index: name
end
mappings() click to toggle source
# File lib/cauchy/elastic/index.rb, line 39
def mappings
  get('mapping')[name]['mappings']
rescue
  {}
end
mappings=(mappings) click to toggle source
# File lib/cauchy/elastic/index.rb, line 55
def mappings=(mappings)
  mappings.each do |type, mapping|
    server.indices.put_mapping index: name, type: type,
      body: { type => mapping }
  end
end
open() click to toggle source
# File lib/cauchy/elastic/index.rb, line 87
def open
  server.indices.open index: name
end
scroll(options = {}) { |documents, results['total']| ... } click to toggle source
# File lib/cauchy/elastic/index.rb, line 95
def scroll(options = {})
  options = { index: name, search_type: 'scan', scroll: '5m', size: 100 }.merge(options)
  scroll_id = server.search(options)['_scroll_id']
  begin
    results = server.scroll(scroll_id: scroll_id, scroll: options[:scroll])
    documents, scroll_id = results['hits']['hits'], results['_scroll_id']
    yield documents, results['hits']['total'] if documents.any?
  end while documents.size > 0
end
settings() click to toggle source
# File lib/cauchy/elastic/index.rb, line 45
def settings
  get('settings')[name]['settings']['index']
rescue
  {}
end
settings=(settings) click to toggle source
# File lib/cauchy/elastic/index.rb, line 62
def settings=(settings)
  server.indices.put_settings index: name, body: settings
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e
  if e.message =~ /update non dynamic settings/
    raise CannotUpdateNonDynamicSettingsError.new(name)
  else
    raise
  end
end

Private Instance Methods

get(resource) click to toggle source
# File lib/cauchy/elastic/index.rb, line 107
def get(resource)
  server.indices.send(['get', resource].join('_').to_sym, index: name)
rescue Elasticsearch::Transport::Transport::Errors::NotFound
  nil
end