customise site search so a parent type is returned based on it's children's content

心不动则不痛 提交于 2019-12-11 08:25:36

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/21154127/customise-site-search-so-a-parent-type-is-returned-based-on-its-childrens-cont

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!