Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reindex only some objects in Sunspot Solr

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?

like image 330
Stanley Avatar asked Jun 29 '12 13:06

Stanley


2 Answers

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
like image 76
s01ipsist Avatar answered Oct 14 '22 17:10

s01ipsist


I have found the answer on https://github.com/sunspot/sunspot#reindexing-objects

Whenever an object is saved, it is automatically reindexed as part of the save callbacks. So all that was needed was to add all the objects that needed reindexing to an array and then loop through the array, calling save on each object. This successfully updated the required objects in the index.

like image 22
Stanley Avatar answered Oct 14 '22 17:10

Stanley