[Zope] Keyword Index Question

Chris Withers chrisw@nipltd.com
Wed, 31 Jul 2002 10:16:56 +0100


VanL wrote:
> Hello,
> 
> I have an object which is included in a ZCatalog that watches, among 
> other things, the attributes 'keywords' (for a keyword index) and 
> 'fulltext' (for a text index).
> 
> In the obect definition, I have the following code:
> 
> def __init__(self, default_catalog='mycatalog'):
>    self.default_catalog = str(default_catalog).strip()
>    self.keywords = self.get_indexable('keyword')    self.fulltext = 
> self.get_indexable('fulltext')
> 
>    def get_indexable(self, index_type):
>        " Dynamically build indexable attributes "
>        propdict = PropertyManager.propdict(self)
>        if index_type == 'keyword':
>            keylist = []
>            for propname in propdict.keys():
>                thisID, thisVal = propname, propdict[propname]
>                thisType = PropertyManager.getPropertyType(self, thisID)
>                if thisType == 'string':
>                    keylist.append(propdict[thisID])
>                elif thisType == 'boolean':
>                    keylist.append(thisID)
>            return keylist
>                  elif index_type == 'fulltext':
>            alltext = ''
>            for propname in propdict.keys():
>                thisID, thisVal = propname, propdict[propname]
>                thisType = PropertyManager.getPropertyType(self, thisID)
>                if thisType == 'text': alltext = alltext + propdict[thisID]
>            return alltext
>                  else: return None
> 
> I also reindex_object each time I change a property, as shown in the 
> example product ZCatalogedObject.
> 
> However, I don't get what I expect.  First, even though my class 
> inhereits from CatalogAware, new objects aren't always added to the 
> zcatalog.

CatalogAware doesnt' work very well, don't rely on it :-S

> Second, when I look at the keywords actually indexed, I see this:
>       keywords [{'id': 'title', 'mode': 'w', 'type': 'string'}]
> 
> Any idea what I am doing wrong?

Your code could use refactoring ;-)
The problem was that propdict returns the property metadata, not the value of 
the property. You want getProperty for that...

      def __init__(self, default_catalog='mycatalog'):
        self.default_catalog = str(default_catalog).strip()
        self.keywords = self.get_indexablekeyword()
        self.fulltext = self.get_indexablefulltext()

      def get_indexablekeyword():
          " Dynamically build indexable attributes "
          propdict = PropertyManager.propdict(self)
          keylist = []
          for name,entry in propdict.items():
              thisType = entry['type']
              if thisType == 'string':
                  keylist.append(self.getProperty(name))
              elif thisType == 'boolean':
                  keylist.append(name)
          return keylist

      def get_indexablefulltext():
          " Dynamically build indexable attributes "
          alltext = ''
          for name,entry in propdict.items():
              if entry['type'] == 'text':
                   alltext += self.getProperty[name]
          return alltext

cheers,

Chris