Class: Elastomer::Client::AppDeleteByQuery

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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



62
63
64
65
66
67
# 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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



69
70
71
# File 'lib/elastomer/client/app_delete_by_query.rb', line 69

def client
  @client
end

#paramsObject (readonly)

Returns the value of attribute params.



69
70
71
# File 'lib/elastomer/client/app_delete_by_query.rb', line 69

def params
  @params
end

#queryObject (readonly)

Returns the value of attribute query.



69
70
71
# File 'lib/elastomer/client/app_delete_by_query.rb', line 69

def query
  @query
end

#response_statsObject (readonly)

Returns the value of attribute response_stats.



69
70
71
# File 'lib/elastomer/client/app_delete_by_query.rb', line 69

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



99
100
101
102
103
104
# 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_paramsObject

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



132
133
134
135
136
137
138
139
140
# 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) ⇒ 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



87
88
89
90
91
92
93
94
# 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

#executeObject

Perform the Delete by Query action

Returns a Hash of statistics about the bulk operation



109
110
111
112
113
114
115
116
117
118
119
120
# 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) ⇒ 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)


77
78
79
# File 'lib/elastomer/client/app_delete_by_query.rb', line 77

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

#search_paramsObject

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



123
124
125
126
127
128
129
# 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