Search code examples
plonezopecatalog

How do I add a custom Zope2 catalog index with plone.indexer?


I'm trying to add a custom catalog index based on the recommendation here:

My code looks like this (h/t: supton):

from plone.indexer.decorator import indexer
from Products.ATContentTypes.interfaces.event import IATEvent
from Products.ATContentTypes.utils import DT2dt

@indexer(IATEvent)
def event_days(context, **kw):
    start = DT2dt(context.getStartDate())
    end = DT2dt(context.getEndDate())
    delta = end - start
    return delta.days

Registered with the following ZCML:

<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:five="http://namespaces.zope.org/five"
    xmlns:genericsetup="http://namespaces.zope.org/genericsetup">

    <five:registerPackage package="." initialize=".initialize" />

    <genericsetup:registerProfile
        description="event_days_indexer"
        directory="profiles/default"
        name="default"
        provides="Products.GenericSetup.interfaces.EXTENSION"
        title="event_days_indexer"
    />

    <adapter name="event_days" factory=".indexers.event_days" />

</configure>

Everything seems to load fine, but when I create a test event and reindex the catalog, the index remains empty. What am I missing?


Solution

  • An object will not be considered for an index if the indexer hits an AttributeError.

    In this case that happens because ATContentTypes specifies custom accessors for the startDate and endDate fields. Instead of context.getStartDate() and context.getEndDate(), use context.start() and context.end()