【问题标题】:Django-Haystack & Elasticsearch brake with queries containing special charactersDjango-Haystack 和 Elasticsearch 制动与包含特殊字符的查询
【发布时间】:2020-10-30 15:08:15
【问题描述】:

所以我一直在尝试修复一个真正让我烦恼的错误: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())

关于如何解决这个问题的任何想法?

非常感谢!

【问题讨论】:

    标签: python django elasticsearch django-haystack


    【解决方案1】:

    这个问题似乎与 Elasticsearch 本身有关,所以我所做的是删除所有 Elasticsearch 实例并将我的搜索视图重新构造为简单的 postgresql 查询。

    解决这个问题后的最后观察:

    每月节省 50 美元,并且搜索引擎的工作就像一个魅力!

    【讨论】:

      猜你喜欢
      • 2011-05-30
      • 2020-12-25
      • 2011-02-19
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      相关资源
      最近更新 更多