I add a FieldIndex for my content type according to the Plone.org instructions.
In the ZMI, I can see the indexed items at /mysite/portal_catalog/Indexes/Building
. My content type (providing IMyType
, with one field building
) is folderish and contains a Photo (providing IPhoto
, without building
field) as allowed_content_types in profiles/default/types/MyType.xml
file.
I want indexing only for MyType's building field. However, it seems items of Photo type get indexed with the value from their parents. That's annoying. Does the code @indexer(IMyType)
mean indexing for IMyType and its contained types? How can I index only for IMyType?
What an indexer does is to get the attribute directly from the object being indexed. In Plone that is as special wrapper, one that will use registered indexers (as created with the @indexer
decorator) if they exist.
However, if you index happens to index building
and that is also an attribute on your IMyType
objects directly, any contained objects will have that attribute through acquisition as well. Registering an indexer for IMyType
does not prevent this.
There are a few ways around this:
Use a different name for your indexer, one that doesn't match the attribute name. Note that if all you do is index an attribute the indexer is redundant though, the index could just as well retrieve the attribute directly.
Register a "catch all" indexer:
from zope.interface import Interface
@indexer(Interface)
def catchall_ignore(ob, **kw):
# Raising AttributeError means: do not index anything
raise AttributeError
Instead of direct attribute access, now this indexer method will be used instead for Photos, causing the indexer to not register a value for building
.