# HG changeset patch
# User Katia Saurfelt <katia.saurfelt@logilab.fr>
# Date 1488898400 -3600
#      Tue Mar 07 15:53:20 2017 +0100
# Node ID 4223ea3c88ceed601f4d62e03a85314fb87cccfa
# Parent  a6137bc159fbd982338eb65df52208c14f907b14
[index] do not index entities with empty IFullTextIndexSerializable

diff --git a/ccplugin.py b/ccplugin.py
--- a/ccplugin.py
+++ b/ccplugin.py
@@ -105,11 +105,15 @@
             for entity in rset.entities():
                 serializer = entity.cw_adapt_to('IFullTextIndexSerializable')
                 json = serializer.serialize()
-                if not dry_run:
+                if not dry_run and json:
+                    # Entities with
+                    # fulltext_containers relations return their container
+                    # IFullTextIndex serializer , therefor the "id" and
+                    # "doc_type" in kwargs bellow must be container data.
                     data = {'_op_type': 'index',
                             '_index': index_name or cnx.vreg.config['index-name'],
-                            '_type': etype,
-                            '_id': entity.eid,
+                            '_type': json['cw_etype'],
+                            '_id': json['eid'],
                             '_source': json
                             }
                     self.customize_data(data)
diff --git a/hooks.py b/hooks.py
--- a/hooks.py
+++ b/hooks.py
@@ -91,13 +91,24 @@
                                          eid=entity.eid)
             indexable_entity = self.cnx.execute(rql).one()
             serializer = indexable_entity.cw_adapt_to('IFullTextIndexSerializable')
-            kwargs['body'] = json = serializer.serialize()
+            json = serializer.serialize(complete=True)
+            if not json:
+                # if en entity has been already indexed, we still
+                # keep the first indexation
+                # which is wrong. We should remove the existing es entry.
+                continue
+            kwargs['body'] = json
+            # Entities with fulltext_containers relations return their container
+            # IFullTextIndex serializer, therefore the "id" and "doc_type" in
+            # kwargs below must be container data.
+            kwargs['id'] = json['eid']
+            kwargs['doc_type'] = json['cw_etype']
             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
+                # TODO option for async ?
                 es.index(**kwargs)
             except (ConnectionError, ProtocolError):
                 log.debug('Failed to index in hook, could not connect to ES')