自定义网站搜索,以便根据其子级的内容返回父类型 [英] customise site search so a parent type is returned based on it's children's content
问题描述
我有一个灵巧的内容类型,它是折叠式的,我希望基于它的孩子的内容(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需要根据容器中的更改自动更新.
处理以下情况:
- 某些东西已添加到容器中
- 已从容器中取出物品
- 容器中的某些内容已修改
事件处理程序可能看起来像这样:
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:
- something is added to the Container
- something is removed from the container
- something is modified in the Container
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屋!