--- a/src/pyams_content_es/index.py Fri Dec 14 19:49:52 2018 +0100
+++ b/src/pyams_content_es/index.py Tue Dec 18 16:36:48 2018 +0100
@@ -12,17 +12,13 @@
__docformat__ = 'restructuredtext'
-
-# import standard library
-
-# import interfaces
-from pyams_content_es.interfaces import IContentIndexerUtility, IDocumentIndexTarget
+from pyramid.events import subscriber
from transaction.interfaces import ITransactionManager
+from zope.intid import IIntIds
from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent
-# import packages
-from pyams_utils.registry import query_utility
-from pyramid.events import subscriber
+from pyams_content_es.interfaces import IContentIndexerUtility, IDocumentIndexTarget
+from pyams_utils.registry import get_utility, query_utility
#
@@ -54,12 +50,19 @@
@subscriber(IObjectModifiedEvent, context_selector=IDocumentIndexTarget)
def handle_modified_document(event):
- """Handle modified document"""
+ """Handle modified document
+
+ We add transaction annotations to avoid several indexations of the same document!
+ """
+ intids = get_utility(IIntIds)
document = event.object
+ document_id = intids.queryId(document)
transaction = ITransactionManager(document).get()
- if 'pyams_content_es.index_document' not in transaction.extension:
+ documents = transaction.extension.get('pyams_content_es.indexed_documents') or set()
+ if document_id not in documents:
+ documents.add(document_id)
transaction.addAfterCommitHook(index_document, kws={'document': document})
- transaction.extension['pyams_content_es.index_document'] = True
+ transaction.extension['pyams_content_es.indexed_documents'] = documents
@subscriber(IObjectRemovedEvent, context_selector=IDocumentIndexTarget)