Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove/Delete an indexed document in ElasticSearch with Tire (with soft delete via ActsAsParanoid)

I have an ElasticSearch server running that indexes and searched documents using the excellent Tire gem. Everything works great, except I'm not sure how to go about manually removing documents from the search index.

I have poured over the RDoc and searched for hours, but this is the only hint at a solution I can find https://github.com/karmi/tire/issues/309. Is there an easier way other than building a custom wrapper around curl and making the request manually?

Another hitch is that I use a soft-delete gem called ActsAsParanoid, so the Tire::Model::Callbacks won't remove the object on soft-delete.

Any ideas?

like image 784
thoughtpunch Avatar asked Oct 09 '12 14:10

thoughtpunch


2 Answers

In case you only have the ID (e.g. 12345):

User.tire.index.remove 'user', '12345'

Or more generally:

klass.tire.index.remove klass.document_type, record_id

(which I think is equivalent to what remove @user will do behind the scenes)

reference

like image 86
mahemoff Avatar answered Sep 28 '22 01:09

mahemoff


Turns out you can just manually remove the soft-deleted Object from the index like so:

@user = User.find(id) #or whatever your indexed object is
User.tire.index.remove @user #this will remove them from the index

Thats it!

like image 42
thoughtpunch Avatar answered Sep 28 '22 02:09

thoughtpunch