13

How I can manually remove specific record Index using Searchkick. There is option to reindex the specific record but i didnt find any option to delete a record index.

 product = Product.find 10
 product.reindex
kashif
  • 1,097
  • 4
  • 17
  • 32

4 Answers4

25

If anyone's looking for how to delete & blow away the entire index to start off fresh you can do it as so:

MyModel.searchkick_index.delete &&  MyModel.searchkick_index.create
Nathan Bertram
  • 1,079
  • 11
  • 18
23

To remove from index:

product = Product.find 10
Product.searchkick_index.remove(product)
Rodrigo
  • 5,435
  • 5
  • 42
  • 78
5

Given product = Product.find(10).

If product.should_index? returns false, product.reindex will remove that record from the index.

If you need to manually remove a record though, Product.searchkick_index.remove(product) is the way to go.

XML Slayer
  • 1,530
  • 14
  • 31
  • Are you sure about this? I just tested this on 3.1.3 and it didn't delete the record from the index. Regardless, I don't think it's intuitive that when `should_index?` is `false`, it not only skips reindexing, but it also deletes any existing record. I would have assumed that `should_index?` would be used for skipping unnecessary reindexing when only non-indexed fields were updated, etc. – Joshua Pinter Dec 28 '19 at 21:05
5

What if you only have the id of the deleted model? eg. in the case where you are getting the call in a background worker?

Looking at the source code, searchkick only checks the class of the object to work out the index and grabs the id from the object, so you could do something like this to get around it:

# given value id holding the record id of the deleted model record (eg. Product with id 123)

prod = Product.new(id: id)

Product.searchkick_index.remove(prod)

Hacky but works :P

foomip
  • 574
  • 7
  • 7