Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haystack - Why does RealtimeSearchIndex sometimes not update my saved object

I'm using Haystack and Whoosh with Django

Within search_index.py I have this

class PageIndex(RealTimeSearchIndex):
    text = CharField(document=True, use_template=True)
    creator = CharField(model_attr='creator')
    created = DateTimeField(model_attr='created')
    org = CharField(model_attr='organisation')

site.register(Page, PageIndex)

My template looks like this

{{ object.name }}
{{ object.description }}
{{ object.template|striptags }}
{% for k,v in object.get_variables.items %}
{{ v }}
{% endfor %}

If I save the Page with an updated name or description then it updates straight away and includes the variables from get_variables.items in the template. However if I update just the variable then it doesn't update.

Is it because variable is another object that's related to it and even though I am saving on the same page it does not pick up a change to the Page? If so how do I force to update the Page item when I'm updating related objects?

like image 324
frank stallone Avatar asked Aug 12 '11 14:08

frank stallone


2 Answers

I concur with Daniel Hepper, but I think the easiest solution here is to attach a listener to your related model's post_save signal (see https://docs.djangoproject.com/en/dev/topics/signals/) and in that, reindex the model.

E.g, in myapp/models.py, given model MyRelatedModel which has a foreignkey to MyModel

from myapp.search_indexes import MyModelIndex

def reindex_mymodel(sender, **kwargs):
    MyModelIndex().update_object(kwargs['instance'].mymodel)
models.signals.post_save.connect(reindex_mymodel, sender=MyRelatedModel)
like image 54
Greg Avatar answered Nov 17 '22 05:11

Greg


A RealTimeSearchIndex only updates the search index when a model it is registered on is saved or deleted, or to be more precise, when the post_save/post_delete signal of the model is emitted. These signals are not emitted if a related model is deleted/saved or when a bulk update/delete operation is executed.

To solve your problem, you could create a subclass of RealTimeSearchIndex that also updates the index on post_save/post_delete signals of the related model.

like image 6
Daniel Hepper Avatar answered Nov 17 '22 04:11

Daniel Hepper