Django + Haystack how to do this search

China☆狼群 提交于 2019-12-08 07:42:28

问题


I'm new to Haystack and to the search world so I didn't know how to ask this question.

What I want to achieve is the following.

Having a search query like: one two

I would like to get returned any content like:

  • This one
  • one
  • two
  • two one
  • something one here

Is this possible with Haystack + solr/xapian? Is also possible to have a relevance on the result? In the case where both words are hit, that would give more relevance to me.

I'm currently using SearchQuerySet in my view but can't achieve that.

Cheers


回答1:


So you're basically looking for an OR type query right? By default haystack uses an AND operation for joining queries.

You can do this two ways:

  • Change HAYSTACK_DEFAULT_OPERATOR within your settings.py to be OR. This will obviously be a site-wide change.
  • Modify your SearchQuerySet form to use filter_or which will force the OR style lookup. So pass a new one into your form/view: SearchQuerySet.filter_or(**kwargs)

Apart from that, you could always join Django Q objects together but considering you have these options, those are probably your best bet.

For relevance, you should read the Best Practices page which goes into using search templates and making them be your way to show relevant content.

Hope that helps!



来源:https://stackoverflow.com/questions/3157480/django-haystack-how-to-do-this-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!