2

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 Answer 1

3
+100

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

18
  • Following your ftw example, I have added an interfaces.py, with an empty interface for IMyBehavior (?), but I am not sure what "import _" means, and I can't find any docs on it. Jul 4, 2014 at 0:55
  • In the ftw.simplelayout exmaple the ISimplelayout interface is a marker behavior (See docs.plone.org/external/plone.app.dexterity/docs/behaviors/…) "_" the underscore is a message factory. It's common in plone to declare this as underscore. Did you register your adapter with zcml? Does it actualy go into the adapter (pdb?)?
    – Mathias
    Jul 4, 2014 at 6:01
  • Why did you add a interfaces.py, you already have a IMyBehavior?? please post your full code, if it doesn't work.
    – Mathias
    Jul 4, 2014 at 22:29
  • It does work now thanks. The last missing piece of the puzzle was to enable the behavior ttw Jul 7, 2014 at 3:44
  • Is it possible to have the behavior enabled by default? Jul 7, 2014 at 4:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.