Skip to content

Instantly share code, notes, and snippets.

@obahareth
Created August 25, 2017 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obahareth/5c26d2704f3ca683d556c27ea9d52c52 to your computer and use it in GitHub Desktop.
Save obahareth/5c26d2704f3ca683d556c27ea9d52c52 to your computer and use it in GitHub Desktop.
Old version of Algolia's Ruby Client delete_by_query method
#
# Delete all objects matching a query
#
# @param query the query string
# @param params the optional query parameters
#
def delete_by_query(query, params = nil)
raise ArgumentError.new('query cannot be nil, use the `clear` method to wipe the entire index') if query.nil? && params.nil?
params ||= {}
params.delete(:hitsPerPage)
params.delete('hitsPerPage')
params.delete(:attributesToRetrieve)
params.delete('attributesToRetrieve')
params[:hitsPerPage] = 1000
params[:attributesToRetrieve] = ['objectID']
loop do
res = search(query, params)
break if res['hits'].empty?
res = delete_objects(res['hits'].map { |h| h['objectID'] })
wait_task res['taskID']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment