quick search:
 

Programmatically add indexes and metadatas to portal_catalog

Submitted by: davconvent
Last Edited: 2004-10-14

Category: CMF

Average rating is: 5.0 out of 5 (1 ratings)

Description:
You may want to programmatically add some indexes to the portal_catalog from the installer script of your CMF/Plone/CPS product. Here is how your Extensions/Install.py should look like:

Source (Text):
from StringIO import StringIO
from Products.CMFCore.utils import getToolByName
from ZPublisher.HTTPRequest import record
[.. Other imports (globals, archetypes, etc) ..]

PROJECTNAME = 'My Project Name'

# 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()

Comments:

No Comments