Class: Elastomer::Client::DeleteByQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/elastomer/client/delete_by_query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, query, params = {}) ⇒ DeleteByQuery

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



59
60
61
62
63
64
# File 'lib/elastomer/client/delete_by_query.rb', line 59

def initialize(client, query, params = {})
  @client = client
  @query = query
  @params = params
  @response_stats = { "took" => 0, "_indices" => { "_all" => {} }, "failures" => [] }
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



66
67
68
# File 'lib/elastomer/client/delete_by_query.rb', line 66

def client
  @client
end

#paramsObject (readonly)

Returns the value of attribute params.



66
67
68
# File 'lib/elastomer/client/delete_by_query.rb', line 66

def params
  @params
end

#queryObject (readonly)

Returns the value of attribute query.



66
67
68
# File 'lib/elastomer/client/delete_by_query.rb', line 66

def query
  @query
end

#response_statsObject (readonly)

Returns the value of attribute response_stats.



66
67
68
# File 'lib/elastomer/client/delete_by_query.rb', line 66

def response_stats
  @response_stats
end

Instance Method Details

#accumulate(item) ⇒ Object

Internal: Combine a response item with the existing statistics

item - A bulk response item



96
97
98
99
100
101
# File 'lib/elastomer/client/delete_by_query.rb', line 96

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

#categorize(item) ⇒ Object

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



84
85
86
87
88
89
90
91
# File 'lib/elastomer/client/delete_by_query.rb', line 84

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

#executeObject

Perform the Delete by Query action

Returns a Hash of statistics about the bulk operation



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/elastomer/client/delete_by_query.rb', line 106

def execute
  ops = Enumerator.new do |yielder|
    @client.scan(@query, @params.merge(:_source => false)).each_document do |hit|
      yielder.yield([:delete, hit.select { |key, _| ["_index", "_type", "_id", "_routing"].include?(key) }])
    end
  end

  stats = @client.bulk_stream_items(ops, @params) { |item| accumulate(item) }
  @response_stats["took"] = stats["took"]
  @response_stats
end

#is_ok?(status) ⇒ Boolean

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

status - HTTP status code

Returns a boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/elastomer/client/delete_by_query.rb', line 74

def is_ok?(status)
  status.between?(200, 299)
end