我在我的 django 项目中使用带有 whoosh 后端的 haystack。我的模型是多语言的模型翻译模块,它为名为 title 的字段创建自动字段,如 title_tr、title_en...
我试图让搜索知道已搜索网络并编写以下行的所选语言,但它不适用于 title_tr、entry_tags_tr 字段...#search_indexes.py
from haystack import indexes
from aksak.blog.models import Entry
class EntryIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(model_attr='descr_en', document=True, use_template=True)
text_tr = indexes.CharField(model_attr='descr_tr')
title_en = indexes.CharField(model_attr='title_en')
title_tr = indexes.CharField(model_attr='title_tr')
tags = indexes.MultiValueField(indexed=True, stored=True, model_attr='entry_tags')
def get_model(self):
return Entry
# haystackCustomQuery.py !!! IN URLS.PY I AM USING THIS CUSTOM VIEW <-------------
from django.conf import settings
from django.utils.translation import get_language
from haystack.query import SearchQuerySet, DEFAULT_OPERATOR
class MlSearchQuerySet(SearchQuerySet):
def filter(self, **kwargs):
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)