好的,多亏了 sdupton 的建议,我找到了让它工作的方法。
这是解决方案(省略了明显的导入):
from Products.AdvancedQuery import (Eq, Not, In,
RankByQueries_Sum, MatchGlob)
from my.product.interfaces import IQuery
class CatalogQuery(object):
implements(IQuery)
...
def _search(self, q, limit):
"""Perform the Catalog search on the 'SearchableText' index
using phrase 'q', and filter any content types
blacklisted as 'unsearchable' in the '@@search-controlpanel'.
"""
# ask the portal_properties tool for a list of names of
# unsearchable content types
ptool = getToolByName(self.context, 'portal_properties')
types_not_searched = ptool.site_properties.types_not_searched
# define the search ranking strategy
rs = RankByQueries_Sum(
(Eq('Title', q), 16),
(Eq('Description', q), 8)
)
# tune the normalizer
norm = 1 + rs.getQueryValueSum()
# prepare the search glob
glob = "".join((q, "*"))
# build the query statement, filter using unsearchable list
query = MatchGlob('SearchableText', glob) & Not(
In('portal_type', types_not_searched)
)
# perform the search using the portal catalog tool
brains = self._ctool.evalAdvancedQuery(query, (rs,))
return brains[:limit], norm