Friday, April 2, 2010

Adding a ZCTextIndex from setuphandlers.py

Alternative title: Indexing an object's creator's full name on Plone 4 with a dexterity content type.

Adding a catalog index in profiles/default/catalog.xml is deprecated; see http://maurits.vanrees.org/weblog/archive/2009/12/catalog.

I had some trouble getting this to work with a ZCTextIndex. I have a content type for which i want to be able to make the creator's full username searchable. In order to do this, i had to create a ZCTextIndex, as a simple FieldIndex didn't seem to work.

I got it to work by replacing the loop inside for name, meta_type in wanted: with this:
   if name not in indexes:
if meta_type == 'ZCTextIndex':
class Empty: pass
title_extras = Empty()
#title_extras.doc_attr = 'index attribute?'
title_extras.index_type = 'Okapi BM25 Rank'
title_extras.lexicon_id = 'plone_lexicon'
catalog.addIndex(name, meta_type, title_extras)
else:
catalog.addIndex(name, meta_type)
indexables.append(name)
logger.info("Added %s for field %s.", meta_type, name)


I'm using a dexterity-based content-type, the code contains:
@indexer(IVideo)
def makerFullnameIndexer(obj):
creator = obj.Creator()
fullname = creator
membership = getToolByName(obj, 'portal_membership')
member = membership.getMemberById(creator)
if member:
fullname = '%s %s' % (member.getProperty('firstname'),
member.getProperty('lastname'))
return fullname
grok.global_adapter(makerFullnameIndexer, name="MakerFullname")

No comments: