Search code examples
ruby-on-railselasticsearchtire

Rails: Elasticsearch has indexed records that have been removed from database


Hi Im having toruble with Elasticsearch.

I have a model and Im using

include Tire::Model::Search
include Tire::Model::Callbacks

for index the records that are created, but for some reason when I test the search it shows me 404 page not found error

then checking the server logs I could notice that elasticsearch has indexed data that does not exists in my database, it means that I deleted some records of that model but elastic search still has those records indexed so when I try to search it finds those records that are not in my database now so that's why i get that page not found error, also I tried in the console and I get the error

ActiveRecord::RecordNotFound: Couldn't find Ofert with 'id'=3

So how to maintain records synchronized with the indexed data at elasticsearch? I mean, I want that when I delete a record it also should be removed from index at elasticsearch

Thanks for you help


Solution

  • Well, what you have to do is delete the indexes and create them again, however we do not recommend do this in production.

    First delete all the indexes of you desired model in this case ofert

    Tire.index("*oferts").delete
    

    Then you will get the following exception: IndexMissingException[[products] missing]

    That's because you are trying to look for an index that does not exist, then you just need to create it again:

    rake environment tire:import CLASS='Ofert' 
    

    The above command will create the index and also import all your already created records to the ElasticSearch indexes.

    We hope have helped you.