Extending SearchableText using collective.dexteritytextindexer

a 夏天 提交于 2019-12-13 02:27:57

问题


I am trying to extend the SearchableText index for my content type. I have succeeded in getting multiple fields to be included by marking them as indexer:searchable="true" in the model file. However I can't extend the SearchableText from my type's py as follows:

class IMyBehavior(form.Schema):

    dexteritytextindexer.searchable('description')
    description = schema.Text(title=u'Precis')

alsoProvides(IMyBehavior, IFormFieldProvider)


class MySearchableTextExtender(object):
    adapts(IMyBehavior)
    implements(dexteritytextindexer.IDynamicTextIndexExtender)

    def __init__(self, context):
        self.context = context

    def __call__(self):
        """Extend the searchable text with a custom string"""
        return 'some more searchable words'

I have to admit, I don't really know how the first class works. Do I have to set the searchable fields in this class to be able to extend the SearchableText in the second? If I remove all the indexer:searchable="true" from the model, then the SearchableText is just empty.

Is the first class trying to register the schema at the same time? If so what should this look like if it's just extending the SearchableText?


回答1:


The collective.dexteritytextindexer provides two important features:

  1. As you already achieved, dexteritytextindexer gives you the ability to put values into Plone's SearchableText index. By adding dexteritytextindexer.searchable(FIELDNAME) to your form, the value of the field will appear in the SearchableText. In Archetypes you have the same feature, by adding searchable=True to the field definition.

  2. collective.dexteritytextindexer gives you also the ability to extend the searchableText manually by registering an IDynamicTextIndexExtender adapter. It extends the values from part 1 with the values from your adapter.

I guess the Problem in your case is, that you have missed to register the adapter: https://github.com/collective/collective.dexteritytextindexer#extending-indexed-data

Example:

<adapter
    factory=".yourbehavior.MySearchableTextExtender"
    provides="collective.dexteritytextindexer.IDynamicTextIndexExtender"
    name="IMyBehavior"
    />

Here's a working example: This code extends the SearchableText of a container with the searchableText of it's children.

IDynamicTextIndexExtender adapter: https://github.com/4teamwork/ftw.simplelayout/blob/a7d631de3984b8c1747506b9411045fdf83bc908/ftw/simplelayout/indexer.py

Register the adapter with zcml: https://github.com/4teamwork/ftw.simplelayout/blob/a7d631de3984b8c1747506b9411045fdf83bc908/ftw/simplelayout/behaviors.zcml#L21

And the most important part - test the implementation: https://github.com/4teamwork/ftw.simplelayout/blob/a7d631de3984b8c1747506b9411045fdf83bc908/ftw/simplelayout/tests/test_indexer.py#L31



来源:https://stackoverflow.com/questions/24118350/extending-searchabletext-using-collective-dexteritytextindexer

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