所以我一直在试图修复一个真正让我恼火的bug:Django Haystack&;Elasticsearch查询使用重音符号,但每次使用包含特殊字符(如破折号-
和撇号'
)的查询时,它都会停止
例如,让我们使用Baie-d'Urfé
作为查询
这是我的密码:
forms.py
class FacetedProductSearchForm(FacetedSearchForm):
def __init__(self, *args, **kwargs):
data = dict(kwargs.get("data", []))
self.ptag = data.get('ptags', [])
self.q_from_data = data.get('q', '')
super(FacetedProductSearchForm, self).__init__(*args, **kwargs)
def search(self):
sqs = super(FacetedProductSearchForm, self).search()
# Ideally we would tell django-haystack to only apply q to destination
# ...but we're not sure how to do that, so we'll just re-apply it ourselves here.
q = self.q_from_data
sqs = sqs.filter(destination=Exact(q))
print('should be applying q: {}'.format(q))
print(sqs)
if self.ptag:
print('filtering with tags')
print(self.ptag)
sqs = sqs.filter(ptags__in=[Exact(tag) for tag in self.ptag])
return sqs
在View.py中使用FacetedSearch
class FacetedSearchView(BaseFacetedSearchView):
form_class = FacetedProductSearchForm
facet_fields = ['ptags']
template_name = 'search_result.html'
paginate_by = 30
context_object_name = 'object_list'
和我的搜索索引.py
class ProductIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.EdgeNgramField(
document=True, use_template=True,
template_name='search/indexes/product_text.txt')
destination = indexes.CharField(model_attr="destination") #boost=1.125
# Tags
ptags = indexes.MultiValueField(model_attr='_ptags', faceted=True)
# for auto complete
content_auto = indexes.EdgeNgramField(model_attr='destination')
# Spelling suggestions
suggestions = indexes.FacetCharField()
def get_model(self):
return Product
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(timestamp__lte=timezone.now())
有没有办法解决这个问题
非常感谢
问题似乎与Elasticsearch本身有关,因此我所做的是删除所有Elasticsearch实例,并将搜索视图重新格式化为简单的postgresql查询
解决此问题后的最终观察:
每月节省50美元,搜索引擎工作得很好
相关问题 更多 >
编程相关推荐