How to change the sort order of search results in Plone

The Plone search function is implemented in the plone.app.search package; the sort_on variable included in the query is used to control the sort order of the results in the search pattern .

By default, when this variable does not matter, Plone uses relevance as a sort order.

What is the easiest way to change this to a date (from the start) ?

+6
source share
1 answer

You will need to customize the search view to set new sorting options, and change the default sorting if no sorting is specified.

If you still need to sort by relevance, use a non-empty value, which you then change in the filter_query method:

 from plone.app.search.browser import _, Search, SortOption class MyCustomSearch(Search): def filter_query(self, query): query = super(MyCustomSearch, self).filter_query(query) if 'sort_on' not in query: # explicitly set a sort; if no `sort_on` is present, the catalog sorts by relevance query['sort_on'] = 'EffectiveDate' query['sort_order'] = 'reverse' elif query['sort_on'] == 'relevance': del query['sort_on'] return query def sort_options(self): """ Sorting options for search results view. """ return ( SortOption(self.request, _(u'date (newest first'), 'EffectiveDate', reverse=True ), SortOption(self.request, _(u'relevance'), 'relevance'), SortOption(self.request, _(u'alphabetically'), 'sortable_title'), ) 

Then register this submission on your website; if you use the darkest layer:

 <configure xmlns="http://namespaces.zope.org/zope" xmlns:browser="http://namespaces.zope.org/browser" i18n_domain="plone"> <browser:page name="search" layer=".interfaces.IYourCustomThemeLayer" class=".yourmodule.MyCustomSearch" permission="zope2.View" for="*" template="search.pt" /> <browser:page name="updated_search" layer=".interfaces.IYourCustomThemeLayer" class=".yourmodule.MyCustomSearch" permission="zope2.View" for="Products.CMFCore.interfaces.IFolderish" template="updated_search.pt" /> </configure> 

You need to copy the search.pt and updated_search.pt templates from the plone.app.search package.

+6
source

All Articles