简体   繁体   English

如何在Django中将Haystack搜索结果分组

[英]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.

相关问题 Django Haystack - 如何通过布尔字段过滤搜索结果? - Django Haystack - How to filter search results by a boolean field? 如何在SAME页面上显示搜索结果(django + haystack + elasticsearch)? - How to display search results(django+haystack+elasticsearch) on the SAME page? 如何配置Django-haystack,以便搜索的“内容”仍会在搜索字段中显示结果 - How to config Django-haystack so “content” searched still shows results in the search field 如何在Django中使用干草堆实现通配符搜索 - How to achieve wildcard search using haystack in django Django Haystack - 显示结果而不需要搜索查询? - Django Haystack - Show results without needing a search query? (Haystack,Woosh,Django)搜索结果不会显示 - (Haystack, Woosh, Django) Search Results won't show up Django Haystack和Whoosh搜索正常,但SearchQuerySet返回0个结果 - Django Haystack & Whoosh Search Working, But SearchQuerySet Return 0 Results 自定义视图不显示带有Elastic Search的Django Haystack中的结果 - Custom view does not show results in Django Haystack with Elastic Search Django-Haystack(elasticsearch)自动完成给出搜索项中子字符串的结果 - Django-Haystack(elasticsearch) Autocomplete giving results for substring in search term 在Django干草堆搜索结果中显示外键值 - display Foreign Key values in Django haystack search results
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM