module Elastics::UtilityMethods

Public Instance Methods

build_bulk_string(document, options={}) click to toggle source
# File lib/elastics/utility_methods.rb, line 85
def build_bulk_string(document, options={})
  case document
  when Hash
    bulk_string_from_hash(document, options)
  when Elastics::ModelIndexer, Elastics::ActiveModel
    bulk_string_from_elastics(document, options)
  else
    raise NotImplementedError, "Unable to convert the document #{document.inspect} to a bulk string."
  end
end
doc(*args) click to toggle source
# File lib/elastics/utility_methods.rb, line 35
def doc(*args)
  elastics.doc(*args)
end
dump_all(*vars, &block) click to toggle source
# File lib/elastics/utility_methods.rb, line 58
def dump_all(*vars, &block)
  refresh_index(*vars)
  scan_all({:params => {:_source => '*'}}, *vars) do |batch|
    batch.map!{|document| document.delete('_score'); document}
    block.call(batch)
  end
end
dump_one(*vars) click to toggle source

refresh and pull the full document from the index

# File lib/elastics/utility_methods.rb, line 67
def dump_one(*vars)
  refresh_index(*vars)
  document = search_by_id({:params => {:_source => '*'}}, *vars)
  document.delete('_score')
  document
end
find(pattern) click to toggle source
# File lib/elastics/utility_methods.rb, line 39
def find(pattern)
  elastics.find(pattern)
end
json2yaml(json) click to toggle source
# File lib/elastics/utility_methods.rb, line 21
def json2yaml(json)
  YAML.dump(MultiJson.decode(json))
end
post_bulk_collection(collection, options={}) click to toggle source

You should use Elastics.post_bulk_string if you have an already formatted bulk data-string

# File lib/elastics/utility_methods.rb, line 75
def post_bulk_collection(collection, options={})
  raise ArgumentError, "Array expected as :collection, got #{collection.inspect}" \
        unless collection.is_a?(Array)
  bulk_string = ''
  collection.each do |d|
    bulk_string << build_bulk_string(d, options)
  end
  post_bulk_string(:bulk_string => bulk_string) unless bulk_string.empty?
end
reload!() click to toggle source
# File lib/elastics/utility_methods.rb, line 29
def reload!
  elastics.variables.deep_merge! Conf.variables
  Templates.contexts.each {|c| c.elastics.reload!}
  true
end
scan_all(*vars, &block) click to toggle source
# File lib/elastics/utility_methods.rb, line 51
def scan_all(*vars, &block)
  elastics.scan_search(:match_all, *vars) do |raw_result|
    batch = raw_result['hits']['hits']
    block.call(batch)
  end
end
usage(*args) click to toggle source
# File lib/elastics/utility_methods.rb, line 43
def usage(*args)
  elastics.usage(*args)
end
yaml2json(yaml) click to toggle source
# File lib/elastics/utility_methods.rb, line 25
def yaml2json(yaml)
  MultiJson.encode(YAML.load(yaml))
end

Private Instance Methods

bulk_string_from_elastics(document, options) click to toggle source
# File lib/elastics/utility_methods.rb, line 113
def bulk_string_from_elastics(document, options)
  elastics = document.elastics
  return '' unless document.elastics_indexable?
  options[:action] ||= document.elastics_action
  meta = { '_index' => elastics.index,
           '_type'  => elastics.type,
           '_id'    => elastics.id }
  meta['_parent']  = elastics.parent  if elastics.parent
  meta['_routing'] = elastics.routing if elastics.routing
  source           = document.elastics_source unless options[:action] == 'delete'
  to_bulk_string(meta, source, options)
end
bulk_string_from_hash(document, options) click to toggle source
# File lib/elastics/utility_methods.rb, line 102
def bulk_string_from_hash(document, options)
  meta = Utils.slice_hash(document, '_index', '_type', '_id')
  if document.has_key?('fields')
    document['fields'].each do |k, v|
      meta[k] = v if k[0] == '_'
    end
  end
  source = document['_source'] unless options[:action] == 'delete'
  to_bulk_string(meta, source, options)
end
perform(*args) click to toggle source
# File lib/elastics/utility_methods.rb, line 98
def perform(*args)
  Template.new(*args).setup(Elastics.elastics).render
end
to_bulk_string(meta, source, options) click to toggle source
# File lib/elastics/utility_methods.rb, line 126
def to_bulk_string(meta, source, options)
  action = options[:action] || 'index'
  return '' if (source.nil? || source.empty?) && (action != 'delete')
  meta['_index'] = LiveReindex.prefix_index(meta['_index']) if LiveReindex.should_prefix_index?
  bulk_string = MultiJson.encode(action => meta) + "\n"
  unless action == 'delete'
    source_line = source.is_a?(String) ? source : MultiJson.encode(source)
    return '' if source.nil? || source.empty?
    bulk_string << source_line + "\n"
  end
  bulk_string
end