How to make a added field searchable using collective.dexteritytextindexer?

I added a field (MyField) to dexterity content type File and try to make it searchable following the guide here . After restarting the instance, the site refused to connect .
These are what I did:

  1. add these code to buildout-cache/eggs/plone.app.contenttypes-1.4.9-py2.7.egg/plone/app/contenttypes/profiles/default/types/File.xml:
    (with<)property name="behaviors"><element(a space)value="collective.dexteritytextindexer.behavior.IDexterityTextIndexer" /></property (with>)

  2. Add these code to buildout-cache/eggs/plone.autoform-1.7.5-py2.7.egg/plone/autoform/directives.py

     from collective import dexteritytextindexer
     from plone.autoform.interfaces import IFormFieldProvider
     from plone.supermodel.model import Schema
     from zope import schema
     from zope.interface import alsoProvides
     class IMyBehavior(Schema):
         dexteritytextindexer.searchable('MyField')
         specialfield = schema.Text(title=u'My Field')
     alsoProvides(IMyBehavior, IFormFieldProvider)
    

Now what should I check and how to go forward?

Hugo

Hey Hugo,

On the first step you are adding a behavior to File contenttype, 'IDexteirtyTextIndexer', File has already some behaviors since you are editing the original source add it here.

  <!-- Enabled behaviors -->
  <property name="behaviors" purge="false">
    <!-- Existing behaviors -->
    <element value="collective.dexteritytextindexer.behavior.IDexterityTextIndexer" />
  </property>

On the second step you define a schema the same way plone does with some behaviors that provide fields, see here: https://github.com/plone/plone.app.contenttypes/tree/master/plone/app/contenttypes/behaviors

But you are not registering it anywhere. If you want to do a behavior you need to register it and then add the behavior to the contenttype.

Any reason to put the code in plone/autoform/directives.py it does not make sense for me.

So if you want to add a field to File searchable you can either register define a behavior that provides a field searchable or add the field to File schema here plone/app/contenttypes/schema/file.xml:

      <field name="specialfield" type="zope.schema.TextLine"
             indexer:searchable="true">
        <title>Special field</title>
      </field>

and aswell to the model header

 xmlns:indexer="http://namespaces.plone.org/supermodel/indexer

As specified in the tutorial. This should work I haven't tested, past aswell the error you were getting. Also if you plan to have more Plone sites on same Zope move your code to a product so the customizations are not on every site you create and easy to do upgrades.

Thanks, csanahuja :slight_smile:
I modified profiles/default/types/File.xml and app/contenttypes/schema/file.xml as you said, now the site can be reached again. But search find nothing with this message: 0 items matching your search terms.

What can I do now?

I have just tested and it works for me. Some questions

  1. Did you add collective.dexteritytextindexer to buildout?
  2. When you add a new File do you see the new field?

If you had already a site created after the modifications you need to reinstall plone.app.contenttypes. Go to ZMI adding /manage to your Plone Site, i.e: localhost:8080/Plone/manage then go to portal_quickinstaller and reinstall it.

  1. After reinstalling plone.app.contenttypes, the field I added can be searched. But except the field, other fields became unsearchable :frowning:
    Need to add indexer:searchhttps://community.plone.org/t/how-to-make-a-added-field-searchable-using-collective-dexteritytextindexer/6397/5able="true" to every field in app/contenttypes/schema/file.xml?

  2. If I set the field type as multi choice and use type="zope.schema.Choice" in schema/file.xml, reinstalling plone.app.contenttypes failed with message:

    SupermodelParseError: You must specify either values or vocabulary.
    File "/opt/plone/buildout-cache/eggs/plone.app.contenttypes-1.4.9-py2.7.egg/plone/app/contenttypes/schema/file.xml", line 26
    indexer:searchable="true">

  1. Yes you are right. I have found this on plone.app.dexterity documentation https://github.com/plone/plone.app.dexterity/blob/76eed6b55116d45568ec605ea81e1f579cc8a9d8/docs/advanced/catalog-indexing-strategies.rst#making-content-searchable

Note that if you turn on the Dynamic SearchableText indexer behavior for a content type, then you must specify all fields that need SearchableText indexing. Dublin core fields like Title and Description are no longer automatically handled.

Once you have turned on the indexer behavior, edit the XML field model to add indexer:searchable="true" to the field tag for each field you wish to add to the SearchableText index.

See the collective.dexteritytextindexer package documentation for details and for information on how to use it via Python schema.

Aparently it says that when you add that behavior you lose title and description as searchable. I have been testing this and I have find that for most default Plone contenttypes field Title and Description comes from a behavior: plone.app.dexterity.behaviors.metadata.IDublinCore

For the contenttype file you already said title and description are directly in the schema.

So I have added a new field to contenttype Folder the same way we did with File expecting to lose Title and Description as searchable aswell but they are still searchable!

So in short if the fields come from the behavior are still searchable not if they are in the schema. You have two option mark them as searchable indexer:searchable="true" or delete them from schema and add the IDublinCore interface.

  1. This error is not related with searchable but wrong definition of the field. See here to see some examples of fields.
    https://training.plone.org/5/mastering-plone/export_code.html
    I think this field fits what you want
      <field name="audience" type="zope.schema.Set">
        <description/>
        <title>Audience</title>
        <value_type type="zope.schema.Choice">
          <values>
            <element>Beginner</element>
            <element>Advanced</element>
            <element>Professional</element>
          </values>
        </value_type>
      </field>
1 Like