class Elastomer::Client::AppDeleteByQuery

Attributes

client[R]
params[R]
query[R]
response_stats[R]

Public Class Methods

new(client, query, params = {}) click to toggle source

Create a new DeleteByQuery command for deleting documents matching a query

client - Elastomer::Client used for HTTP requests to the server query - The query used to find documents to delete params - Other URL parameters

# File lib/elastomer/client/app_delete_by_query.rb, line 62
def initialize(client, query, params = {})
  @client = client
  @query = query
  @params = params
  @response_stats = { "took" => 0, "_indices" => { "_all" => {} }, "failures" => [] }
end

Public Instance Methods

accumulate(item) click to toggle source

Internal: Combine a response item with the existing statistics

item - A bulk response item

# File lib/elastomer/client/app_delete_by_query.rb, line 99
def accumulate(item)
  item = item["delete"]
  (@response_stats["_indices"][item["_index"]] ||= {}).merge!(categorize(item)) { |_, n, m| n + m }
  @response_stats["_indices"]["_all"].merge!(categorize(item)) { |_, n, m| n + m }
  @response_stats["failures"] << item unless is_ok? item["status"]
end
bulk_params() click to toggle source

Internal: Remove parameters that are not valid for the _bulk endpoint

# File lib/elastomer/client/app_delete_by_query.rb, line 132
def bulk_params
  return @bulk_params if defined?(@bulk_params)

  @bulk_params = @params.dup
  return @bulk_params if @bulk_params.nil?

  @bulk_params.delete(:q)
  @bulk_params
end
categorize(item) click to toggle source

Internal: Tally the contributions of an item to the found, deleted, missing, and failed counts for the summary statistics

item - An element of the items array from a bulk response

Returns a Hash of counts for each category

# File lib/elastomer/client/app_delete_by_query.rb, line 87
def categorize(item)
  {
    "found" => item["found"] || item["status"] == 409 ? 1 : 0,
    "deleted" => is_ok?(item["status"]) ? 1 : 0,
    "missing" => !item["found"]  && !item.key?("error") ? 1 : 0,
    "failed" => item.key?("error") ? 1 : 0,
  }
end
execute() click to toggle source

Perform the Delete by Query action

Returns a Hash of statistics about the bulk operation

# File lib/elastomer/client/app_delete_by_query.rb, line 109
def execute
  ops = Enumerator.new do |yielder|
    scan = @client.scan(@query, search_params)
    scan.each_document do |hit|
      yielder.yield([:delete, hit.select { |key, _| ["_index", "_type", "_id", "_routing"].include?(key) }])
    end
  end

  stats = @client.bulk_stream_items(ops, bulk_params) { |item| accumulate(item) }
  @response_stats["took"] = stats["took"]
  @response_stats
end
is_ok?(status) click to toggle source

Internal: Determine whether or not an HTTP status code is in the range 200 to 299

status - HTTP status code

Returns a boolean

# File lib/elastomer/client/app_delete_by_query.rb, line 77
def is_ok?(status)
  status.between?(200, 299)
end
search_params() click to toggle source

Internal: Remove parameters that are not valid for the _search endpoint

# File lib/elastomer/client/app_delete_by_query.rb, line 123
def search_params
  return @search_params if defined?(@search_params)

  @search_params = @params.merge(_source: false)
  @search_params.delete(:action_count)
  @search_params
end