如何在Plone中覆盖搜索结果排序顺序

hve*_*rde 7 search overriding plone

Plone搜索功能在plone.app.search包中实现; 请求中包含的sort_on变量用于控制搜索模板上结果的排序顺序.

默认情况下,当此变量没有值时,Plone使用相关性作为排序顺序.

迄今为止最简单的更改方式是什么(最新的第一个)

Mar*_*ers 7

您需要自定义搜索视图以设置新的排序选项,并在未设置排序时更改默认排序.

如果您仍然需要能够按相关性排序,请使用您在filter_query方法中更改的非空值:

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'),
        )
Run Code Online (Sandbox Code Playgroud)

然后将此视图注册到您的站点; 如果您使用最简单的主题图层:

<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>
Run Code Online (Sandbox Code Playgroud)

您需要复制包中的模板search.ptupdated_search.pt模板plone.app.search.

  • @hvelarde:编辑应该只用于修复拼写,语法等.超过2k的所有用户都可以查看. (2认同)