Search code examples
ruby-on-railselasticsearchkibanasearchkick

Searchkick: Remove documents when unable to find original record


To take the example from another question

product = Product.find(10) 
   `raise_record_not_found_exception!'
product.nil?
=> true

product = Product.search "*", where: {id: 10}, load: false
product.count
=> 1

How do I remove the document without a clean wipe?

Thanks


Solution

  • If your index is out of sync with your database and you don't want to do a full reindex, you can do:

    missing_product = Product.new
    missing_product.id = 10
    Product.search_index.remove(missing_product)