Title and description aren't indexed with collective.dexteritytextindexer

断了今生、忘了曾经 提交于 2019-12-05 20:50:27

Got pretty much the same issue. Following the documentation on http://pypi.python.org/pypi/collective.dexteritytextindexer I used

from collective import dexteritytextindexer
from plone.autoform.interfaces import IFormFieldProvider
from plone.directives import form
from zope import schema
from zope.interface import alsoProvides

class IMyBehavior(form.Schema):

    dexteritytextindexer.searchable('specialfield')
    specialfield = schema.TextField(title=u'Special field')

alsoProvides(IMyBehavior, IFormFieldProvider)

to get my own fields indexed. However, the code

from plone.app.dexterity.interfaces import IBasic
from collective.dexteritytextindexer.utils import searchable

searchable(IBasic, 'title')
searchable(IBasic, 'description')

Didn't work. The import of IBasic fails. Seems this can easily be solved by importing

from plone.app.dexterity.behaviors.metadata import IBasic

The problem is probably that the field is coming from the IBasic or IDublineCore behaviour and not from your schema. I don't know enough about collective.dexteritytextindexer to know how to work around this, though.

Another option may be to just use plone.indexer and create your own SearchableText indexer that returns "%s %s %s" % (context.title, context.description, context.long_desc,). See the Dexterity docs for details.

As a reference this is the code I ended up writing:

@indexer(IMyDexterityType)
def searchableIndexer(context):
    transforms = getToolByName(context, 'portal_transforms')
    long_desc = context.long_desc // long_desc is a rich text field
    if long_desc is not None:
        long_desc = transforms.convert('html_to_text', long_desc).getData()
    contacts = context.contacts // contacts is also a rich text field
    if contacts is not None:
        contacts = transforms.convert('html_to_text', contacts).getData()

    return "%s %s %s %s" % (context.title, context.description, long_desc, contacts,)
grok.global_adapter(searchableIndexer, name="SearchableText")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!