问题
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:
- something is added to the Container
- something is removed from the container
- 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