Skip to content
Snippets Groups Projects
Commit a6137bc159fb authored by Samuel Trégouët's avatar Samuel Trégouët
Browse files

[hook] handle delete entity

parent 3d097513f4dd
No related branches found
No related tags found
No related merge requests found
......@@ -39,7 +39,7 @@
__regid__ = 'elasticsearch.contentupdatetoes'
__select__ = hook.Hook.__select__ & score_entity(entity_indexable)
events = ('after_update_entity', 'after_add_entity')
events = ('after_update_entity', 'after_add_entity', 'after_delete_entity')
category = 'es'
def __call__(self):
......@@ -76,8 +76,18 @@
log.error('no connection to ES (not configured) skip ES indexing')
return
for entity in self.get_data():
kwargs = dict(index=indexer.index_name,
id=entity.eid,
doc_type=entity.cw_etype)
if self.cnx.deleted_in_transaction(entity.eid):
try:
# TODO option for async ?
es.delete(**kwargs)
except (ConnectionError, ProtocolError):
log.debug('Failed to index in hook, could not connect to ES')
continue
rql = fulltext_indexable_rql(entity.cw_etype,
entity._cw.vreg.schema,
eid=entity.eid)
indexable_entity = self.cnx.execute(rql).one()
serializer = indexable_entity.cw_adapt_to('IFullTextIndexSerializable')
......@@ -79,8 +89,12 @@
rql = fulltext_indexable_rql(entity.cw_etype,
entity._cw.vreg.schema,
eid=entity.eid)
indexable_entity = self.cnx.execute(rql).one()
serializer = indexable_entity.cw_adapt_to('IFullTextIndexSerializable')
json = serializer.serialize()
kwargs['body'] = json = serializer.serialize()
if json.get('parent'):
kwargs['parent'] = json.pop('parent')
else: # TODO only for types that have parents
kwargs['routing'] = entity.eid
try:
# TODO option pour coté async ? thread
......@@ -85,13 +99,5 @@
try:
# TODO option pour coté async ? thread
kwargs = dict(index=indexer.index_name,
id=entity.eid,
doc_type=entity.cw_etype,
body=json)
if json.get('parent'):
kwargs['parent'] = json.pop('parent')
else: # TODO only for types that have parents
kwargs['routing'] = entity.eid
es.index(**kwargs)
except (ConnectionError, ProtocolError):
log.debug('Failed to index in hook, could not connect to ES')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment