问题
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:
As you already achieved,
dexteritytextindexer
gives you the ability to putvalues
into Plone'sSearchableText
index. By addingdexteritytextindexer.searchable(FIELDNAME)
to your form, the value of the field will appear in theSearchableText
. In Archetypes you have the same feature, by addingsearchable=True
to the field definition.collective.dexteritytextindexer
gives you also the ability to extend the searchableText manually by registering anIDynamicTextIndexExtender
adapter. It extends the values frompart 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