过滤器没有做任何事情

过滤器没有做任何事情

本文介绍了Haystack的SearchQuerySet()。过滤器没有做任何事情(Whoosh 2.4.1,Django-haystack 1.2.7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

views.py (相关部分)

srch = request.GET.get('search', "")
sqs = SearchQuerySet().filter(has_title=True)
clean_query = sqs.query.clean(srch)
results = sqs.raw_search(clean_query)

search_indexes.py

from haystack.indexes import *
from haystack import site
from myproject.myapp.models import Person

class PersonIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    name = CharField(model_attr='name')
    has_title = BooleanField(model_attr='has_title')

site.register(Person, PersonIndex)

models.py

class Person(models.Model):
    name = models.CharField(max_length=50)
    has_title = models.BooleanField()

    def __unicode__(self):
        return u'%s' % self.name

当搜索时,我从完整的模型中得到我的结果,而不是只有那些条目 has_title True 。应用过滤器后,我还运行了 python manage.py rebuild_index 。为什么过滤器不起作用?

When searching, I get my results out of the complete model instead of only those entries were has_title is True. I also ran python manage.py rebuild_index after applying the filter. Why does the filter not work?

我正在使用 Whoosh 2.4.1 Django -haystack 1.2.7 Django 1.4

如果有人可以确认他们可以成功地使用 SearchQuerySet()。filter Whoosh 2.4 Django-haystack 1.2。 7 这将是一个很大的帮助。

If someone could just confirm that they can successfully use SearchQuerySet().filter with Whoosh 2.4 and Django-haystack 1.2.7 this would already be a big help.

推荐答案

似乎 raw_search(clean_query)再次返回 sqs 中的每个项目(不知道为什么),使用 filter(content = clean_query)而不是预期的结果。

It seems that raw_search(clean_query) just returns every item in sqs again (don't know why), using filter(content=clean_query) instead gets the expected result.

srch = request.GET.get('search', "")
sqs = SearchQuerySet().filter(has_title=True)
clean_query = sqs.query.clean(srch)
result = sqs.filter(content=clean_query)

这篇关于Haystack的SearchQuerySet()。过滤器没有做任何事情(Whoosh 2.4.1,Django-haystack 1.2.7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 10:08