[英]How to group Haystack search results in Django
I'm trying to group search results from Haystack. 我正在尝试对Haystack的搜索结果进行分组。 I want them to be grouped by model (Artist or Painting), then by a date field (created), then ideally by a boolean field (sold).
我希望将它们按模型(艺术家或绘画)分组,然后按日期字段(创建)分组,理想情况下按布尔值字段(出售)分组。
Ie something like the following but it's not working. 即类似以下内容,但它不起作用。 I think I need to override SearchView and somehow process the query before it's handed to the template but I'm not sure how.
我认为我需要重写SearchView并以某种方式处理查询,然后再将其提交给模板,但我不确定如何处理。
Or perhaps I should just be doing the grouping in the template? 还是我应该只在模板中进行分组?
def get_queryset():
q = SearchQuerySet().filter(display=True).order_by('-created')
paintings_unsold = q.models(Painting).filter(sold=False)
paintings_sold = q.models(Painting).filter(sold=True)
artists = q.models(Artist)
return paintings_unsold | paintings_sold | artists
urlpatterns += patterns(
'',
(r'^search/', SearchView(
searchqueryset=get_queryset()
))
)
Just to clarify that facetting is not equal to grouping results. 只是为了澄清构面不等于分组结果。 SOLR can do this, for example (Version 4): https://cwiki.apache.org/confluence/display/solr/Result+Grouping .
SOLR可以做到这一点,例如(版本4): https : //cwiki.apache.org/confluence/display/solr/Result+Grouping 。
But what you are actually asking for is faceting, as mentioned in the comments: 但是,您真正要求的是刻面,如注释中所述:
http://django-haystack.readthedocs.org/en/v2.4.0/faceting.html https://cwiki.apache.org/confluence/display/solr/Faceting http://django-haystack.readthedocs.org/en/v2.4.0/faceting.html https://cwiki.apache.org/confluence/display/solr/Faceting
I will provide an answer for SOLR, even thought the question is tagged with Whoosh. 我将为SOLR提供答案,甚至以为问题是用Whoosh标记的。 It might still help.
它可能仍然有帮助。
You want to facet on the field sold
and a field artist_exact
that you will probably have to create. 您想了解所
sold
的字段artist_exact
可能必须创建的字段artist_exact
。 It has to be of type string
otherwise your facets will look odd. 它必须是
string
类型,否则您的构面会看起来很奇怪。
So, your current artist
field is probably (recommended) a text field that tokenizes the input somehow. 因此,您当前的
artist
字段可能是(推荐)以某种方式标记输入的文本字段。 Simply add a new field that does not tokenize and a copy directive in schema.xml
that copies the artist to this new field. 只需在
schema.xml
中添加一个不会标记化的新字段和一个将艺术家复制到该新字段的copy指令。
<field name="artist" type="text_general" indexed="true" stored="true"/>
<field name="artist_exact" type="string" indexed="true" stored="false"/>
<copyField source="artist" dest="artist_exact" />
In solrconfig.xml
add the following parameters to your search handler: 在
solrconfig.xml
将以下参数添加到您的搜索处理程序中:
<str name="facet">on</str>
<str name="facet.field">sold</str>
<str name="facet.field">artist_exact</str>
Haystack provides the FacetedSearchView that you can use. Haystack提供了您可以使用的FacetedSearchView。 It will add the facet information to the template context.
它将构面信息添加到模板上下文中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.