Login

haystack auto update (forget about update_index)

Author:
jpic
Posted:
January 17, 2012
Language:
Python
Version:
1.3
Score:
-1 (after 1 ratings)

Dead simple snippet. Paste it in some models (i use project_specific/models.py), and you don't need to run update_index anymore. When a model is deleted from the database: it is deleted from the index. When a model is saved (created or modified): it is updated in the index.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from haystack.sites import site
from django.db.models import signals

def add_to_search_index(sender, instance=None, **kwargs):
    try:
        index = site.get_index(instance.__class__)
    except:
        return
    
    index.backend.update(index, [instance])
signals.post_save.connect(add_to_search_index)

def delete_from_search_index(sender, instance=None, **kwargs):
    try:
        index = site.get_index(instance.__class__)
    except:
        return
    
    index.backend.remove(instance)
signals.post_delete.connect(delete_from_search_index)

More like this

  1. Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 2 months, 4 weeks ago
  2. Python Django CRUD Example Tutorial by tuts_station 3 months, 2 weeks ago
  3. Browser-native date input field by kytta 4 months, 4 weeks ago
  4. Generate and render HTML Table by LLyaudet 5 months, 1 week ago
  5. My firs Snippets by GutemaG 5 months, 1 week ago

Comments

jpic (on January 17, 2012):

Don't use this snippet. Use RealTimeSearchIndex instead of SearchIndex.

#

Please login first before commenting.