DateIndex in Zope doesn't have indexed attributes

28 October 2007   0 comments   Zope

Powered by Fusion×

This took me a while to grok and perhaps by mentioning it here, it'll prevent other people from making the same mistake as I did and perhaps preventing myself from doing the same mistake again.

In the ZCatalog, when you set up indexes you can give them a name and an index attribute. If you omit the index attribute, it'll try to hook into the objects by the name of the index. For example, if you set the index to be title with no indexed attribute it'll fetch the title attribute of the objects it catalogs. But if you set the indexed attribute to be something like idx_getTitle you can do something like this in your class:

def idx_getTitle(self):
   """ return title as we want it to be available in the ZCatalog """
   return re.sub('<*.?>','', self.title)

The same can not be done with indexes of type DateIndex.

DateIndex in Zope doesn't have indexed attributes I don't know why it is so but perhaps there's a good explanation that I don't understand. If you use the ZMI it's clear that you can't add an indexed attribute but it doesn't stop you from adding one in pure python like you do with all other indexes:

from ZPublisher.HTTPRequest import record
zcatalog = self.MyCatalog
indexes = zcatalog._catalog.indexes

# this works
extra = record()
extra.indexed_attrs = 'getSearchableCountry'
zcatalog.addIndex('country', 'FieldIndex', extra)

# this does NOT work!
extra = record()
extra.indexed_attrs = 'getPublishDateHourless'
zcatalog.addIndex('publish_date', 'DateIndex', extra)

If you run this code, it'll even pick up the method getPublishDateHourless in the ZMI view of the catalog's indexes. But it's never run!

By the way, what I wanted to achieve was to index the publish date of certain objects but only index them without the hour/minute/second bit. Because I couldn't have such a "proxy method" I instead searched on the index publish_date with a range like this:

zcatalog = self.MyCatalog
v = DateTime('2007/12/13')
query = {'query':[v, v+1], 'range':'min:max'}
return zcatalog.searchResults(publish_date=query)


Your email will never ever be published

Related posts

PiteƄ, as experienced by Sam Dunstan 25 October 2007
Now I'm on 30 October 2007
Related by keywords:
EditArea vs. CodePress 03 January 2008
Filename splitter 15 November 2005
New search feature on this site 13 September 2003
Jealous of Google stemming 04 August 2005
When '_properties' gets stuck as a persistent attribute 01 October 2008
Google teething problems still with duplicated content 03 June 2011
MUnderscorePatch - tired of typing manage_main? 29 November 2006