# HG changeset patch
# User Katia Saurfelt <katia.saurfelt@logilab.fr>
# Date 1524129695 -7200
#      Thu Apr 19 11:21:35 2018 +0200
# Node ID 2a9a7b4ef36297970604c6e1adbc7a15f62ad853
# Parent  49e1524640dfa23bd05aef4ad690d3757e2018d2
[index] refresh the index after performing the `es_index` operation. (closes #51594388)

If this is not done, elasticsearch.scan method retrieves the old index values
so wrong values will be copied while synchronizing different indexes.

We probably should do it by default in `indexer.es_index` method.
We probably should do it in `indexer.es_delete` method.

see also https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-refresh.html

diff --git a/cubicweb_elasticsearch/entities.py b/cubicweb_elasticsearch/entities.py
--- a/cubicweb_elasticsearch/entities.py
+++ b/cubicweb_elasticsearch/entities.py
@@ -19,6 +19,8 @@
 
 import collections
 
+from functools import partial
+
 from urllib3.exceptions import ProtocolError
 
 from elasticsearch.exceptions import ConnectionError, NotFoundError
@@ -86,7 +88,7 @@
         if es_cnx is not None:
             es.create_index(es_cnx, index_name, settings)
 
-    def es_index(self, entity):
+    def es_index(self, entity, params=None):
         es_cnx = self.get_connection()
         if es_cnx is None or not self.index_name:
             self.error('no connection to ES (not configured) skip ES indexing')
@@ -96,7 +98,8 @@
         if not json:
             return
         es_cnx.index(index=self.index_name, id=serializable.es_id,
-                     doc_type=serializable.es_doc_type, body=json)
+                     doc_type=serializable.es_doc_type, body=json,
+                     params=params)
 
     def es_delete(self, entity):
         es_cnx = self.get_connection()
@@ -218,7 +221,7 @@
         if self._cw.deleted_in_transaction(entity.eid):
             es_method = indexer.es_delete
         elif es_operation['op_type'] == 'index':
-            es_method = indexer.es_index
+            es_method = partial(indexer.es_index, params={'refresh': True})
         elif es_operation['op_type'] == 'delete':
             es_method = indexer.es_delete
         else: