26. Search for additional fields – Mastering Plone 6 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.

Backend chapter

Checkout ploneconf.site at tag "upgrade_steps":

git checkout upgrade_steps

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 add the two fields of a talk "speaker" and "details" to the SearchableText index. This is the index that's addressed when making a full text search.

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

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

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.

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

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 can use the function update_indexes for a new upgrade step.

Make fields searchable

Make fields searchable#