开发者

Django Haystack refuses to show no results, even for absurd queries

My question may be a bit strange, but it's been bothering me since the behavior is not what I expected. Here is my query:

query = request.GET.get('q','')
#in search_indexes:
#start_datetime = indexes.DateTimeField(model_attr='start_datetime',null=True)
#end_datetime = indexes.DateTimeField(model_attr='end_datetime')
search_events = SearchQuerySet().models(Event).filter(content=query).
                filter(end_datetime__gte=datetime.now()).
                order_by("start_datetime")

Now I type in a query like "asdfasdfjasldf lolol hwtf asdlfka" and I still get 3 results. (Note, I only have 5 events to start with. Not sure if that could affect anything.) I print out the scores, and they are [42,42,42]. Doesn't filter() match on exact phrases? Especially if I use quotes?

/开发者_Python百科/edit I also tried using auto_query, and the results are the same.

I'm really confused about what's happening, so hopefully somebody can help clear this up. Thanks in advance!


Turns out that someone else on my team had set HAYSTACK_DEFAULT_OPERATOR to 'OR' instead of 'AND'. Explains everything - the additional filter tag was actually expanding the number of results!


You might like to perform search using auto_query():

search_events = SearchQuerySet().models(Event)
                                .auto_query(query)
                                .filter(end_datetime__gte=datetime.now())
                                .order_by("start_datetime")

It has some extra features, like for example exact query searching when phrase is enclosed in quotes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜