自定义网站搜索,以便根据其子级的内容返回父类型 [英] customise site search so a parent type is returned based on it's children's content

查看:68
本文介绍了自定义网站搜索,以便根据其子级的内容返回父类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个灵巧的内容类型,它是折叠式的,我希望基于它的孩子的内容(pdf等)将其显示在搜索结果中.这是可能的,在何处以及如何实现?

解决方案

一种可行的解决方案是在子级而不是项本​​身上重新索引子级的searchableText(连接所有子级的searchableText).

您的新searchableText索引可能如下所示:

 @indexer(IYourContainerType)
def SearchableText(obj):
    searchable_text = obj.SearchableText()

    for item in obj.getFolderContents({'portal_type': 'File'}, full_object=True):
        searchable_text += item.SearchableText()
    return searchable_text
 

现在您必须预订一些事件,因为容器的SearchableText需要根据容器中的更改自动更新.

处理以下情况:

  1. 某些东西已添加到容器中
  2. 已从容器中取出物品
  3. 容器中的某些内容已修改

Docu for Plone中的事件

事件处理程序可能看起来像这样:

 def reindex_container(obj, event):
    parent = aq_parent(aq_inner(obj))

    if not IYourContainerType.providedBy(parent):
        return

    catalog = getToolByName(obj, 'portal_catalog')
    # Only reindex existing brains! The parent may be just
    # deleted, we should not put it back in the catalog.
    parent_path = '/'.join(parent.getPhysicalPath())
    if catalog.getrid(parent_path) is not None:
        parent.reindexObject()
 

可能您需要单独处理MOVE.

I have a dexterity content type which is folderish and I would like to have it show up in search results based on it's child's content (pdfs etc). Is this possible, where and how would this be achieved?

解决方案

One possible solution is to reindex the searchableText of the children on the parent and not on the item itself (concatenate the searchableText of all children).

Your new searchableText Index could look like this:

@indexer(IYourContainerType)
def SearchableText(obj):
    searchable_text = obj.SearchableText()

    for item in obj.getFolderContents({'portal_type': 'File'}, full_object=True):
        searchable_text += item.SearchableText()
    return searchable_text

Now you have to subscribe some events, because the SearchableText of the container needs to be updated automatically on changes in the container.

Handle if:

  1. something is added to the Container
  2. something is removed from the container
  3. something is modified in the Container

Docu for Events in Plone

The eventhandler could look like this:

def reindex_container(obj, event):
    parent = aq_parent(aq_inner(obj))

    if not IYourContainerType.providedBy(parent):
        return

    catalog = getToolByName(obj, 'portal_catalog')
    # Only reindex existing brains! The parent may be just
    # deleted, we should not put it back in the catalog.
    parent_path = '/'.join(parent.getPhysicalPath())
    if catalog.getrid(parent_path) is not None:
        parent.reindexObject()

Probably you need to handle MOVE separately.

这篇关于自定义网站搜索,以便根据其子级的内容返回父类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆