Created
August 25, 2017 15:51
-
-
Save obahareth/5c26d2704f3ca683d556c27ea9d52c52 to your computer and use it in GitHub Desktop.
Old version of Algolia's Ruby Client delete_by_query method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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