DX RichText field indexed as 'SearchableText'

Hi!
I bet this has been asked many times, but nor the docs nor google are giving me any solution.
I created a DX CT with a behavior "plone.app.contenttypes.behaviors.richtext.IRichText".
That field is not being indexed by default.
Plone only indexes the "metadata.IDublinCore" 'Description' and 'Title' contents.
I need that my CT indexes as 'SearchableText' the contents of that RichText field merged with the 'Description' and 'Title' contents, just like the News Item does it.
How can I do this on my CT?

Write your own indexer with plone.indexer

-aj

https://github.com/collective/collective.dexteritytextindexer comes in handy here.

Thanks, but my goal is to have all the needed code on my own add-on, and not depend on another add-on, unless you tell me that it is impossible or too much dificult??
(it should not be...)

It is common to depend on other add-ons and modules.
So what is your point?

-aj

For the record I copied this code from plone.app.contenttypes:

def _unicode_save_string_concat(*args):
    """
    concats args with spaces between and returns utf-8 string, it does not
    matter if input was unicode or str
    """
    result = ''
    for value in args:
        if isinstance(value, unicode):
            value = value.encode('utf-8', 'replace')
        if value:
            result = ' '.join((result, value))
    return result

def SearchableText(obj):
    text = u""
    richtext = IRichText(obj, None)
    if richtext:
        textvalue = richtext.text
        if IRichTextValue.providedBy(textvalue):
            transforms = getToolByName(obj, 'portal_transforms')
            text = transforms.convertTo(
                'text/plain',
                safe_unicode(textvalue.output).encode('utf8'),
                mimetype=textvalue.mimeType,
            ).getData().strip()

    subject = u' '.join(
        [safe_unicode(s) for s in obj.Subject()]
    )

    return u" ".join((
        safe_unicode(obj.id),
        safe_unicode(obj.title) or u"",
        safe_unicode(obj.description) or u"",
        safe_unicode(text),
        safe_unicode(subject),
    ))

@indexer(Imyaddon)
def SearchableText_myaddon(obj):
    return _unicode_save_string_concat(SearchableText(obj))

and it seems to work, but I'm not sure if this is the right way to do it??
Are there other (better) ways??

@zopyx

My dear friend, aren't you a little bit rude and unfriendly??

that's fine; here you have another example:

I am not sure you need to go through the effort and waste of copylifting/redefining SearchableText verbatim, if your fieldname is the same ('text')? You might as well just import it, wrap it with the indexer function, and add to your ZCML; less code, same result.

Sean

Perhaps it is rude/ignorant/dumb (make your choice) to argue about three perfectly hints to your problem given by two Plone long-term developers, my dear friend??

-aj

Sorry, I also do not understand at all why you do not want to go the easy path. Anyway, even if you want to reinvent the wheel: The code of collective.dexterityindexer shows exactly how you do this. This reminds me a bit on a tweet I saw recently https://twitter.com/ThePracticalDev/status/705825638851149824

So the question: "So what is your point?" is maybe short but imo valid.

If you want to indexed a Rich Text field in catalog ,So you should create your own ZCatalog object in your custom add where you can override the Index filed if you do not want to use onther product.

It is easy.
Because I'm (re)learning Plone, I'm just doing my first DX CT, and someone once told me that in everything in life, the harder path is always the best to learn and then be the number one.

I can see what you mean, but I'm afraid I don't know how to do it! :wink:
But thanks anyway!

1 Like

Thanks, maybe I use something like that code, because I want to have "stop words".
(or is there a better way to have my own list of "stop words" ??)

yes, you can modify the portal_catalog object(Metadata,Indexes) through ZMI or you also have a provision to filter the object from the portal_catalog using 'archetype_tool/manage_catalogs' option in your ZMI.

Please don't ever use this old-school approach any longer. It will be removed at some point in future together with Archetypes.

Use plone.indexer and its tools for custom indexers. We have this way more flexible way for custom indexing for a long time now. This is well documented here http://docs.plone.org/develop/plone/searching_and_indexing/indexing.html#custom-index-methods