26. Search for additional fields – Mastering Plone Development

Search for additional fields

26. Search for additional fields#

In this part you will ensure that the detailed description of a talk and its speaker are included in search.

Check out mastering-plone-project at tag listing_variation:

git checkout listing_variation

The code at the end of the chapter:

git checkout searchable

More info in The code for the training

In the previous chapter we prepared a search block to search for talks. You may have noticed that a search for speaker names does not show the expected results. We need to add the two fields of a talk speaker and details to the SearchableText index. This is the index that's used when making a full text search.

The package plone.app.dexterity.textindexer allows to add fields to the SearchableText index. The package is already installed with Plone.

Add the behavior plone.textindexer to the list of behaviors of your content type.

backend/src/ploneconf/site/profiles/default/types/talk.xml

  <property name="behaviors">
    <element value="plone.dublincore" />
    <element value="plone.namefromtitle" />
    <element value="plone.versioning" />
    <element value="ploneconf.featured" />
    <element value="plone.eventbasic" />
    <element value="plone.textindexer" />
  </property>

Now you need to mark the fields you want to include in your SearchableText index. This can be done with the searchable directive.

backend/src/ploneconf/site/content/talk.py

from plone.app.dexterity import textindexer

# ...

    textindexer.searchable("details")
    details = RichText(
        title="Details",
        description="Description of the talk (max. 2000 characters)",
        max_length=2000,
        required=True,
    )

    textindexer.searchable("speaker")
    speaker = schema.TextLine(
        title="Speaker",
        description="Name (or names) of the speaker",
        required=False,
    )

The SearchableText index now includes your fields.

To be able to search for text in the detailed description of a talk and its speaker we need to restart Plone and update the catalog. This can be done with an upgrade step. Do you remember the earlier chapter Upgrade steps? You need an upgrade step which runs the typeinfo import step to add the new behavior, and runs the update_indexes function to reindex existing talks.

Make fields searchable

Make fields searchable#