How to reindex only some objects in Sunspot Solr

Stanley picture Stanley · Jun 29, 2012 · Viewed 7.4k times · Source

We use Sunspot Solr for indexing and searching in our Ruby on Rails application.

We wanted to reindex some objects and someone accidentally ran the Product.reindex command from the Rails Console. The result was that indexing of all products started from scratch and our catalogue appeared empty while indexing was taking place.

Since we have a vast amount of data the reindexing has been taken three days so far. This morning when I checked on the progress of the reindexing, it seems like there was one corrupt data entry which resulted in the reindexing stopping without completing.

I cannot restart the entire Product.reindex operation again as it takes way too long. Is there a way to only run reindexing on selected products? I want to select a range of products that aren't indexed and then just run indexing on thise. How can I add a single product to the index without having to run a complete reindex of entire data set?

Answer

s01ipsist picture s01ipsist · Apr 11, 2013

Sunspot does index an object in the save callback so you could save each object but maybe that would trigger other callbacks too. A more precise way to do it would be

Sunspot.index [post1, post2]
Sunspot.commit

or with autocommit

Sunspot.index! [post1, post2]

You could even pass in object relations as they are just an array too

Sunspot.index! post1.comments