plone / plone.app.dexterity

Dexterity is a content type system for Plone
http://docs.plone.org/external/plone.app.dexterity/docs/index.html
18 stars 43 forks source link

Dexterity IRichText Behavior field is not searchable #237

Open angelawong opened 7 years ago

angelawong commented 7 years ago

When I choose the IRichText behavior:text, it creates a field called "text" automatically. I noticed that when I use this field, the content inside this field is not searchable. I know I am to use collective.dexteritytextindexer but for this field that is automatically produced by Dexterity, I don't know where I can add indexer:searchable="true".

Seems like for the Page content type, the "text" field is searchable. Should this field that is added automatically when one checks the IRichText behavior be also a part of Searchable Text in portal_catalog?

This question was first posed at the Plone forum at: https://community.plone.org/t/dexterity-irichtext-behavior-field-is-not-searchable/3565

jensens commented 7 years ago

After a quick look at the code I found the problem is in plone.app.contenttypes as IRichText is also located there - and it's indexers. Anyway, I answer here.


plone.app.contenttypes.indexers defines a some functions collecting SearchableText from fields and also its indexers.zcml registers the @indexer functions as named utilities. The whole implementation is not generic bound to IRichText (as one might expect). Moreover, it is bound to the specific content-types. Overall this is all but perfect.

In order to make your indexer work you need to register an own indexer for your content-type, and so your content-type needs at leats a marker interface (you can try to reuse IRichText as this marker, but this may break the Document content-types - its worth a try). Off head (watch for bugs), this should look like so in your custom myindexer.py:

from plone.app.contenttypes.indexer import SearchableText
from plone.indexer.decorator import indexer
from plone.indexer.decorator import _unicode_save_string_concat

@indexer(IRichText)  # or for IMyMarker
def SearchableText_for_my_marker(obj):
    return _unicode_save_string_concat(SearchableText(obj))

and in your configure.zcmlyou do a

<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:five="http://namespaces.zope.org/five"
    xmlns:i18n="http://namespaces.zope.org/i18n"
    xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
    i18n_domain="plone">
  ...
  <adapter name="SearchableText" factory=".myindexers.SearchableText_for_my_marker" />
  ...
</configure>

HTH.

Btw.: I just started the process of moving the functionality of collective.dexterityindexer into Plone core. This should simplify the process and would make it possible to also fix the default indexers to be much more generic - after it your case would be just configuration. See collective/collective.dexteritytextindexer#22

fgrcon commented 6 years ago

I think this should be considered a bug ?

@jensens what is the current situation - what is the recommended method to index Richtext (behavior) fields of custom dx types in 5.1? Thanks i.a.

jensens commented 6 years ago

@fgrcon collective.dexterityindexer is it for now. In fact I would like to see this functionality moved to plone.app.dexterity itself. See ticket referenced above.