Friday, April 2, 2010

Adding a ZCTextIndex from

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

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)
catalog.addIndex(name, meta_type)
indexables.append(name)"Added %s for field %s.", meta_type, name)

I'm using a dexterity-based content-type, the code contains:
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'),
return fullname
grok.global_adapter(makerFullnameIndexer, name="MakerFullname")

No comments: