[英]Get a list of searchable content types in Plone 4
我正在使用Products.AdvancedQuery為我的網站構建替代LiveSearch機制。 到目前為止,一切都運行良好,但標准查詢會搜索所有可用的內容類型,包括在@@ search-controlpanel中標記為不可搜索的內容類型。
我希望AdvancedQuery根據@@ search-controlpanel中指定的內容動態過濾掉不可搜索的那些。 我怎樣才能做到這一點?
如果AQ無法做到,我可以在查詢目錄后立即過濾結果。 我需要一個標記為可搜索的內容類型名稱(或接口)列表。 我怎樣才能獲得這樣的清單?
假設您可以獲取控件面板以編程方式列入黑名單的元組或類型列表,這可能就像(導入已省略)一樣簡單:
>>> query = myquery & AdvancedQuery.Not(AdvancedQuery.In(MY_TYPE_BLACKLIST_HERE))
>>> result = getToolByName(context, 'portal_catalog').evalAdvancedQuery(query)
好的,多虧了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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.