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
.
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)
Comments