Update index of associated objects

104 views
Skip to first unread message

medihack

unread,
Feb 9, 2011, 12:35:28 PM2/9/11
to Sunspot
Yes, it is me again ... but I feel that I am quite near the finish
line ;-)

When my model also indexes an association, like

class Project < ActiveRecord::Base
searchable do
text :description
text :task_names do
self.tasks.map { |task| task.name }
end
end
end

and now I update a name of a task and then save the task, like

task = Task.first
task.name = "foo"
task.save

what would be the best way to update the index of all associated
projects (as task.save does not seem to auto update the project
index)?

Should I just add something like an after save filter to the Task
model and call project.index on all associated projects?

One last question (I promise ;-)) ... I have a model with many many
also indexed associations (like many tasks in that above project
example). How does Sunspot/Solr internally work when saving the
project and only for example the description was edited? Does it
reindex everything, or check if the field has changed and only update
the index for this field then? Or do I have to do that myself somehow
in "searchable"?

Best regards,
Kai

Matthew A. Brown

unread,
Feb 9, 2011, 12:44:33 PM2/9/11
to ruby-s...@googlegroups.com
Hi Kai,

To answer your first question: Yes, defining your own after_save is
the way to go.

To answer your second question: Solr does not support the concept of
partial updates -- under the hood, updating a document actually
consists of removing it from the index and then re-adding it. So,
Sunspot does have to construct the full document each time anything
changes; it's an unfortunate limitation from a performance standpoint,
but it's pretty fundamental to the way Solr and Lucene work. If you
are constructing complex documents out of many associated models, I
would heartily suggest performing your indexing in the background
using something like DJ or Resque so your server response times don't
suffer.

Mat

> --
> You received this message because you are subscribed to the Google Groups "Sunspot" group.
> To post to this group, send email to ruby-s...@googlegroups.com.
> To unsubscribe from this group, send email to ruby-sunspot...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/ruby-sunspot?hl=en.
>
>

medihack

unread,
Feb 10, 2011, 7:09:34 AM2/10/11
to Sunspot
Thank you for the good info, Matthew. I just found (beside Delated_job
and Resque) another library to asynchronously update the index:
Sunspot Index Queue: https://github.com/bdurand/sunspot_index_queue
This one works with Sunspot nearly out of the box.

As I took a look around in GitHub I found quite many nice Sunspot
addons. If you don't mind I will create a Wiki page where those addons
are summarized.

And a little suggestion. As sunspot and sunspot_rails are now both in
the sunspot Git/GitHub repository, it would be handier to have also
the sunspot_rails documentation on the front page. I didn't know a lot
about Sunspot before, and it took me some time to find out that there
is another Rails specific documentation in the sunspot_solr folder.
I suggest when you host sunspot and sunspot_solr in the same
repository (as it is now) then to merge both documentations in
README.rdoc (having two main sections there). In my opinion that would
be more consistent and easier to grasp.

Best regards,
Kai

medihack

unread,
Feb 10, 2011, 7:45:17 AM2/10/11
to Sunspot
Matthew, I just want to let you know that I merged both READMEs (and
did some minor changes to make it more consistent). Please see my pull
request at GitHub.

Best regards,
Kai
Reply all
Reply to author
Forward
0 new messages