Django Haystack and Tagit

Is there anyone using a Django taggit with a haystack? How can we make a tag field indexable with haystack?

I 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 use custom search 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) 
+4
source share
2 answers

To get tags in the search index, we added them to our content template file, for example

 {{ 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()] 

It was not possible to succeed in any case, but the search definitely indexes them correctly.

+8
source

To add the answer above to Rafe. For a haystack, you must also use a different backend, besides a simple backend. For some reason, a simple backend does not call any of the specific preparation functions.

0
source

Source: https://habr.com/ru/post/1481429/


All Articles