Triggering a reindex through a generic relationship with Haystack
Let's say that you have a Haystack search index defined with a summary field that has a indexed=False on it, but you'd like to have your indexes update automatically whenever an associated generic relationship is updated as well.
In my case, I had a Rating class that was attached to my objects. I wanted to display the most up to date rating in the search summary, but because the rating isn't actually in the index and is not member of the class itself, but is only generated by the summary field, it isn't always up to date.
So, how can you fix this? Easy, with a simple signal:
def update_rating(sender, **kwargs):
"""
Attach a signal to the `Rating` :method:post_save so that we can
also update the associated `Foo` when it has been rated.
"""
site.get_index(Foo).update_object(kwargs['instance'].content_object)
signals.post_save.connect(update_rating, sender=Rating)
Just do the above in your search_indexes.py and you should be all set. Whenever a Rating object is saved, it'll trigger any associated post_save signals on the attached content_object (in this case, Foo). Because Haystack itself already has signals watching the indexed objects, it will also trigger the reindexing of the rated object.
