Add indexes and metadatas to portal_catalog on install
This How-to applies to:
Any version.
This How-to is intended for:
Developers
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.