Login

haystack auto update (forget about update_index)

Author:
jpic
Posted:
January 17, 2012
Language:
Python
Version:
1.3
Tags:
haystack
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. Removing old ImageFields and FileFields when updating through admin by alejandro.alonso 3 years, 3 months ago
  2. plaintext filter by onelson 5 years, 3 months ago
  3. Haystack whoosh backend for stemming non-english language words by shamakhov 1 year, 5 months ago
  4. FileField / ImageField with a delete checkbox by tomZ 7 years, 8 months ago
  5. Updated FileField / ImageField with a delete checkbox by tomZ 7 years, 4 months ago

Comments

jpic (on January 17, 2012):

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

#

Please login first before commenting.