【问题标题】:Get a list of searchable content types in Plone 4获取 Plone 4 中可搜索内容类型的列表
【发布时间】:2012-05-23 16:05:54
【问题描述】:

我正在使用 Products.AdvancedQuery 为我的网站构建替代 LiveSearch 机制。到目前为止一切正常,但标准查询会搜索所有可用的内容类型,包括在@@search-controlpanel 中标记为不可搜索的内容。

我希望 AdvancedQuery 根据@@search-controlpanel 中指定的内容动态过滤掉不可搜索的内容。我该怎么做?

如果 AQ 做不到,我可以在查询目录后立即过滤结果。我需要一个标记为可搜索的内容类型名称(或接口)列表。我怎样才能获得这样的清单?

【问题讨论】:

    标签: search plone catalog


    【解决方案1】:

    假设您可以获得控制面板以编程方式列入黑名单的类型的元组或列表,这可能就像(省略导入)一样简单:

    >>> query = myquery & AdvancedQuery.Not(AdvancedQuery.In(MY_TYPE_BLACKLIST_HERE)) 
    >>> result = getToolByName(context, 'portal_catalog').evalAdvancedQuery(query)
    

    【讨论】:

    • 感谢您的指点。你帮助我理解了如何让它发挥作用。我正在添加描述我的解决方案的回复。
    【解决方案2】:

    好的,感谢 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 2010-09-09
      • 1970-01-01
      相关资源
      最近更新 更多