Django-Haystack 和 Elasticsearch 刹车包含特殊字符的查询
Django-Haystack & Elasticsearch brake with queries containing special characters
所以我一直在尝试修复一个真正让我烦恼的错误: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'
还有我的search_indexes.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 美元,搜索引擎运行得非常棒!
所以我一直在尝试修复一个真正让我烦恼的错误: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
中使用 FacetedSearchclass FacetedSearchView(BaseFacetedSearchView):
form_class = FacetedProductSearchForm
facet_fields = ['ptags']
template_name = 'search_result.html'
paginate_by = 30
context_object_name = 'object_list'
还有我的search_indexes.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 美元,搜索引擎运行得非常棒!