Personal tools
You are here: Home Documentation How-tos Add indexes and metadatas to portal_catalog on install
Support

Get Help

Join our chat rooms or support forums if you have more specific questions.

Plone Training
Learn how to design, build, and deploy a website in Plone through one of the numerous Plone training sessions around the world.
Find Plone training…
 
Document Actions

Add indexes and metadatas to portal_catalog on install

This How-to applies to: Any version.
This How-to is intended for: Developers

How to add indexes and metadata columns to the portal_catalog from a script including ZCTextIndex and TextIndexNG index types.

You may want to programmatically add some indexes to the portal_catalog from the Extentions/Install.py script of your Plone product.

in Extensions/Install.py, use the code below (snippet):

  from StringIO import StringIO
  from Products.CMFCore.utils import getToolByName
  from ZPublisher.HTTPRequest import record
  from Products.MyProduct.config import PROJECTNAME
  [.. Other imports (globals, archetypes, etc) ..]

  # extra args for the ZCTextIndex index to be added to portal_catalog
  zcti_extra = record()
  zcti_extra.lexicon_id = 'plone_lexicon'
  zcti_extra.index_type = 'Okapi BM25 Rank'
  zcti_extra.doc_attr   = None

  # extra args for the TextIndexNG2 index to be added to portal_catalog
  ting2_extra = record()
  ting2_extra.indexed_fields   = 'myField,myOtherField'
  ting2_extra.default_encoding = 'utf-8'
  ting2_extra.use_converters   = 1

  # indexes to be added to portal_catalog
  catalog_indexes = (
    { 'name'  : 'SomeData',
      'type'  : 'TextIndex'
      },
    { 'name'  : 'SomeOtherData',
      'type'  : 'KeywordIndex'
      },
    { 'name'  : 'MyTextIndexNG2',
      'type'  : 'TextIndexNG2',
      'extra' : ting2_extra
    },
    { 'name'  : 'MyZCTextIndex',
      'type'  : 'ZCTextIndex',
      'extra' : zcti_extra
      },
                     )

  # metadatas (columns) to be added to portal_catalog
  catalog_columns = ('SomeData', 'SomeOtherData')

  def install(self):
      out = StringIO()

      [.. Other installation stuff (i.e. types and skins setup) ..]

      # add indexes and metadatas to the portal catalog
      ct = getToolByName(self, 'portal_catalog')
      for idx in catalog_indexes:
          if idx['name'] in ct.indexes():
              out.write("Found the '%s' index in the catalog, nothing changed.\n" % idx['name'])
          else:
              ct.addIndex(**idx)
              out.write("Added '%s' (%s) to the catalog.\n" % (idx['name'], idx['type']))
      for entry in catalog_columns:
          if entry in ct.schema():
              out.write("Found '%s' in the catalog metadatas, nothing changed.\n" % entry)
          else:
              ct.addColumn(entry)
              out.write("Added '%s' to the catalog metadatas.\n" % entry)

      print >> out, "Successfully installed %s." % PROJECTNAME
      return out.getvalue()

Notes

  • Archetypes index declarations in schema definitions should be used as much as possible.
  • From Plone 2.5 on, GenericSetup profiles can handle Index and Metadata registration in portal_catalog.
by David Convent last modified July 6, 2007 - 14:51 All content is copyright Plone Foundation and the individual contributors.

For any issues with the web site functionality, please file a ticket.

Please consult the policy on plone.org content if you want your content published on this site.

Servers and hosting by