问题
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)
回答1:
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.
来源:https://stackoverflow.com/questions/16613742/django-haystack-and-taggit