retire icon indicating copy to clipboard operation
retire copied to clipboard

Add "delete by query" support

Open romanbsd opened this issue 12 years ago • 20 comments

Tire doesn't currently support this feature: http://www.elasticsearch.org/guide/reference/api/delete-by-query.html

I hacked together the following solution (we're using it in production). Unfortunately I won't have time to work on this further.

https://gist.github.com/2346772

romanbsd avatar Apr 09 '12 21:04 romanbsd

Hello @karmi, any plans on supporting delete by query? If there is, I can work on the implementation and submit a pull request.

Cheers!

mereghost avatar Aug 20 '12 20:08 mereghost

Any word on this? Need to remove items from the search index that have been soft-deleted via ActsAsParanoid. Would like to stay withing the Tire API if posssible, something like Tire.index("users").remove_from_index("5243")

thoughtpunch avatar Oct 09 '12 14:10 thoughtpunch

@thoughtpunch simple use cases can be handled like this:

Tire::Configuration.client.delete "#{Tire::Configuration.url}/twitter/tweets/_query?q=a:23"

veesahni avatar Oct 16 '12 18:10 veesahni

Notice it's easy to use Tire's API/DSL for manually invoking the "Delete by Query" API, until there's a proper support:

# Dangerous code removed

See the code below

karmi avatar Oct 23 '12 11:10 karmi

In order to setup my tests, I want to delete all documents across al indexes, I tried using this:

index = Tire::Index.new('_all')

query = Tire::Search::Search.new do
  query { all }
end

Tire::Configuration.client.delete [index.url, query.to_hash.to_param].join('?')

But I can't search anymore. Does this delete indexes somehow?

Papipo avatar Nov 01 '12 10:11 Papipo

@Papipo Yes

karmi avatar Nov 01 '12 10:11 karmi

Well, now I am using something simpler:

Tire::Configuration.client.delete "#{Tire::Configuration.url}/_all"

My problem was the async nature of the setup, so now I am calling refresh() after save and destroy.

Papipo avatar Nov 01 '12 12:11 Papipo

@karmi any news on this now?

Will-Sommers avatar Feb 28 '13 19:02 Will-Sommers

@karmi could you please explain why index gets deleted on "manually invoking the "Delete by Query" API"? (see comment above - https://github.com/karmi/tire/issues/309#issuecomment-9699113)

jurgens avatar May 14 '13 16:05 jurgens

@jurgens @karmi's example is constructing a DELETE request to the resource endpoint of an index, e.g. http://localhost:9200/twitter/, plus some parameters. If query.to_hash.to_param evaluates to an empty string, which you'll find in @Papipo's query that it does, then you're effectively making the administrative delete index request that @karmi linked to.

ches avatar May 14 '13 16:05 ches

@ches thank you

jurgens avatar May 14 '13 19:05 jurgens

@jurgens @Will-Sommers @thoughtpunch Sorry, the advice I gave was incorrect and dangerous (@ches above is right), I apologize.

This would be the workaround for the moment:

require 'tire'

index = Tire::Index.new('articles')

index = Tire::Index.new('articles') do
  delete
  store title: 'x'
  store title: 'y'
  store title: 'z'
  refresh
end

query = Tire::Search::Search.new do
  query { term :title, 'x' }
end

p query.to_hash

puts query.to_curl

puts "curl '#{index.url}/_search?source=#{Tire::Utils.escape(query.to_hash.to_json)}'"

puts "curl -X DELETE '#{index.url}/_query?source=#{Tire::Utils.escape(query.to_hash[:query].to_json)}'"

p Tire::Configuration.client.delete "#{index.url}/_query?source=#{Tire::Utils.escape(query.to_hash[:query].to_json)}"

NOTE: Eg. the Curb gem still doesn't support HTTP bodies with DELETE, making the support a bit cumbersome.

karmi avatar May 14 '13 20:05 karmi

Thank you @karmi that was helpful!

jurgens avatar May 15 '13 09:05 jurgens

is this similar to #771?

phoet avatar Jul 05 '13 12:07 phoet

@phoet Yes -- we could support delete_by_query by serializing the payload into the source URL parameter.

karmi avatar Jul 05 '13 12:07 karmi

@karmi would you consider this a "hack"? and prefer the extension to RestClient for DELETE with payload?

phoet avatar Jul 05 '13 12:07 phoet

@phoet I think that would qualify as an "acceptable" hack :)

(Of course provided we have unit+integration tests, etc)

karmi avatar Jul 05 '13 12:07 karmi

I believe 55d12c0caaf09f80 closed this.

ches avatar Sep 06 '13 16:09 ches

@karmi I recommend deleting your post way up above if it's dangerous. Someone in a rush might just execute what you suggested w/o scrolling down below.

HenleyChiu avatar Feb 16 '14 22:02 HenleyChiu

There are a couple of fixes to make this work, mainly the curl logging on failure and the json for query on ?source=. Please review https://github.com/karmi/retire/pull/964

x0bandeira avatar May 06 '14 22:05 x0bandeira