Adrian Garner
Adrian Garner

Reputation: 5335

How to re-index all content of a certain type?

I would like to re-index all content of a particular type, just once.

Should I make a python script in the zmi?

This is what I have so far

from zope.component.hooks import getSite

site = getSite()
items = site.contentItems()
items.reindexObject()

I am not sure how to specify the type... or if I am on the right track. Are there any examples of doing this kind of operation that I can dissect?

Upvotes: 2

Views: 311

Answers (2)

keul
keul

Reputation: 7819

A solution we use:

import plone.api

catalog = plone.api.portal.get_tool(name='portal_catalog')
for brain in catalog(portal_type='My portal type'):
    obj = brain.getObject()
    catalog.catalog_object(obj)

Using catalog_object method from ZCatalog is the same API used by the ZMI "Update" feature:

ZMI Catalog update

Pros: modification date is not updated, you are simply reindexing the catalog data Cons: you can't use this API from restricted Python (while you can call obj.reindexObject)

If you don't have problem about modification date changes, the gforcata answer is simpler.

Upvotes: 3

gforcada
gforcada

Reputation: 2558

The best way would actually to use the catalog for that:

import plone.api

catalog = plone.api.portal.get_tool(name='portal_catalog')
for brain in catalog(portal_type='My portal type'):
    obj = brain.getObject()
    obj.reindexObject()

That would do it.

Notice that I used only plone.api calls, so your code would be future proof.

Upvotes: 4

Related Questions