When you’re using Solr, sometimes the search can index records that have already been removed from your database - causing some frustration. Here’s how to remove these hanging records from Solr so you have a clean search again.
Solr is an open source enterprise search platform. However, like any piece of software, there can be certain conditions or situations where it may not perform as you wish or expect.
This article looks at one of these cases: Sometimes your Solr search engine contains indexed records that are no longer actually present in the database. It’s very hard to detect these kind of records during a regular search, because they don't have any actual representation in the database.
In most cases you would run a search similar to this:
Model.search do
# search criteria
end.results
results = Model.search do
# search criteria
end.raw_results
ids = []
results.each do |result|
ids << result.primary_key
end
ids.each do |id|
unless Model.exists?(id)
Sunspot.remove_by_id(Model, id)
end
end
def clean_up_model(model)
results = model.search.raw_results
results.each do |result|
if model.find_by(id: result.primary_key).blank?
Sunspot.remove_by_id(model, result.primary_key)
end
end
end
clean_up_model(Post)
clean_up_model(User)
We like to share these types of fixes so that others can get the solution quickly and easily if they find themselves in the same situation. If you have some strange behaviors in your enterprise software or infrastructure and would like an expert eye over the issue, then please get in touch. We love to help our clients find better, more efficient ways of running their systems!