如何重新索引某种类型的所有内容?

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

我想重新索引特定类型的所有内容,一次。

我应该在 zmi 中制作一个 python 脚本吗?

这是我目前所拥有的

from zope.component.hooks import getSite

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

我不确定如何指定类型...或者我是否在正确的轨道上。有没有我可以剖析的做这种操作的例子?

最好的方法实际上是使用目录:

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()

那就行了。

请注意,我只使用了 plone.api 个调用,因此您的代码将面向未来。

我们使用的解决方案:

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)

使用 ZCatalog 中的 catalog_object 方法与 ZMI "Update" 功能使用的 API 相同:

优点:修改日期未更新,您只是重新索引目录数据 缺点:您不能从受限 Python 使用此 API(但您可以调用 obj.reindexObject

如果您没有关于修改日期更改的问题,gforcata 答案更简单。