4

Is there anybody using Django taggit with haystack? How can we make tags field indexable by haystack?

I have tried:

class EventIndex(indexes.SearchIndex, indexes.Indexable):
        text = indexes.CharField( model_attr='descr_en', document=True, use_template=True)
        text_tr = indexes.CharField(model_attr='descr_tr')
        tags = indexes.MultiValueField()

        def prepare_text(self, obj):
            return '%s %s' % (obj.title_en, obj.descr_en)

        def prepare_text_tr(self, obj):
            return '%s %s' % (obj.title_tr, obj.descr_tr)

        def prepare_tags(self, obj):
            return [tag.name for tag in obj.tags.all()]

        def get_model(self):
            return Event

And i am using a custom searchqueryset for multilingual search :

class MlSearchQuerySet(SearchQuerySet):
    def filter(self, **kwargs):
        """Narrows the search based on certain attributes and the default operator."""
        if 'content' in kwargs:
            kwd = kwargs.pop('content')
            currentLngCode = str(get_language())
            lngCode = settings.LANGUAGE_CODE
            if currentLngCode == lngCode: 
                kwdkey = "text" 
                kwargs[kwdkey] = kwd
            else:
                kwdkey = "text_%s" % currentLngCode
                kwargs[kwdkey] = kwd


        if getattr(settings, 'HAYSTACK_DEFAULT_OPERATOR', DEFAULT_OPERATOR) == 'OR':
           return self.filter_or(**kwargs)
        else:
            return self.filter_and(**kwargs)
ratata
  • 1,129
  • 14
  • 42

1 Answers1

8

To get the tags into the search index we added them to our content template file eg

{{ object.title }}
{{ object.body }}
{% for tag in object.tags.all %} {{ tag.name }} {% endfor %}
{{ object.user.get_full_name }}

We also include it as a MultiValueField

tags = indexes.MultiValueField()

def prepare_tags(self, obj):
    return [tag.name for tag in obj.tags.all()]

Haven't had success trying to make boost work in either case, but the search definitely indexes them correctly.

Rafe Hatfield
  • 625
  • 1
  • 6
  • 10
  • I have updated my question in order to represent my real code structure better. Because i am not getting any tags related search results with the ways you suggest... – ratata May 17 '13 at 17:23
  • can you include your text template? if your tags are in there they should get indexed, they will be treated the same as any other content in your template. – Rafe Hatfield May 17 '13 at 17:28
  • in 'event_text.txt' i have : {{ object.title }} {{ object.descr }} {% for tag in object.tags.all %} {{ tag.name }} {% endfor %} – ratata May 17 '13 at 17:30
  • hmm seems ok to me - sorry to ask what may be a silly question, but just to make sure; you are reindexing after each change, correct? (ie run "python manage.py rebuild_index") – Rafe Hatfield May 17 '13 at 17:38
  • Yes i am reindexing after changing... :) – ratata May 17 '13 at 17:40
  • 1
    ah you probably also need to add tags to your prepare_text method - not confident on that but seems like its missing (still new to haystack, you're past my basic knowledge now) – Rafe Hatfield May 17 '13 at 17:52