module ElasticSearch::Api::Index

Public Instance Methods

bulk(options={}) { |self| ... } click to toggle source

Starts a bulk operation batch and yields self. Index and delete requests will be queued until the block closes, then sent as a single _bulk call.

# File lib/elasticsearch/client/index.rb, line 116
def bulk(options={})
  # allow nested bulk calls
  if @batch
    yield(self)
  else
    begin
      @batch = []
      yield(self)
      response = execute(:bulk, @batch, options)
    ensure
      @batch = nil
    end
  end
end
count(query, options={}) click to toggle source

df The default field to use when no field prefix is defined within the query. analyzer The analyzer name to be used when analyzing the query string. default_operator The default operator to be used, can be AND or OR. Defaults to OR.

# File lib/elasticsearch/client/index.rb, line 107
def count(query, options={})
  index, type, options = extract_scope(options)

  response = execute(:count, index, type, query, options)
  response["count"].to_i #TODO check if count is nil
end
delete(id, options={}) click to toggle source
# File lib/elasticsearch/client/index.rb, line 43
def delete(id, options={})
  index, type, options = extract_required_scope(options)

  if @batch
    #TODO add routing, parent
    @batch << { :delete => { :_index => index, :_type => type, :_id => id }}
  else
    result = execute(:delete, index, type, id, options)
    result["ok"]
  end
end
delete_by_query(query, options = {}) click to toggle source
# File lib/elasticsearch/client/index.rb, line 55
def delete_by_query(query, options = {})
  index, type, options = extract_required_scope(options)
  execute(:delete_by_query, index, type, query, options)
end
get(id, options={}) click to toggle source
# File lib/elasticsearch/client/index.rb, line 30
def get(id, options={})
  index, type, options = extract_required_scope(options)
  # index
  # type
  # id
  # fields
  
  hit = execute(:get, index, type, id, options)
  if hit
    Hit.new(hit)
  end
end
index(document, options={}) click to toggle source
# File lib/elasticsearch/client/index.rb, line 6
def index(document, options={})
  index, type, options = extract_required_scope(options)
  # type
  # index
  # id (optional)
  # op_type (optional)
  # timeout (optional)
  # document (optional)

  id = options.delete(:id)
  if @batch
    #TODO add routing, parent
    @batch << { :index => { :_index => index, :_type => type, :_id => id }.merge(options)}
    @batch << document
  else
    result = execute(:index, index, type, id, document, options)
    if result["ok"]
      result["_id"]
    else
      false
    end
  end
end
multi_get(ids, options={}) click to toggle source

minor multi get support

# File lib/elasticsearch/client/index.rb, line 132
def multi_get(ids, options={})
  index, type, options = extract_required_scope(options)
  results = execute(:multi_get, index, type, ids, options)
  if(results)
    hits = []
    results.each { |hit| hits << Hit.new(hit) }
    hits
  end
end
scroll(scroll_id, options={}) { |hits| ... } click to toggle source

ids_only Return ids instead of hits pass a block to execute the block for each batch of hits

# File lib/elasticsearch/client/index.rb, line 91
def scroll(scroll_id, options={}, &block)
  begin
    search_options = options.reject { |k, v| [:page, :per_page, :ids_only, :limit, :offset].include?(k) }
    response = execute(:scroll, scroll_id, options)
    hits = Hits.new(response, { :ids_only => options[:ids_only] })
    if block_given? && !hits.empty?
      yield hits 
      scroll_id = hits.scroll_id
    end
  end until !block_given? || hits.empty?
  hits
end