有没有人使用 Django taggit 和 haystack?我们如何使标签字段可以被 haystack 索引?
我努力了:
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
我正在使用自定义搜索查询集进行多语言搜索:
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)