Skip to content

Commit 0e1e2b8

Browse files
committed
Increase configurability of bulk refresh parameter
The Elasticsearch bulk APIs support a configurable 'refresh' parameter that can be set to: 1. empty string / true : perform refresh after operation completes 2. 'wait_for': wait until next scheduled refresh 3. false (default): don't do any refresh See documentation at: https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-refresh.html Currently the `Document.update()` API accepts a `refresh` parameter that, if `True`, passes `True` to the ES bulk API. There isn't currently a way to pass 'wait_for'. This commit modifies how `Document.update()` works so that it is possible to pass `refresh='wait_for'`. I've implemented this in a generic way, as opposed to hardcoding logic for 'wait_for', to support future changes to this ES bulk API. Note that, in keeping with the current convention, if `refresh='wait_for'` is passed, this will override the value of auto_refresh set either via setting or via the Document class. This gives developers the maximum configurability here.
1 parent 68b342a commit 0e1e2b8

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

django_elasticsearch_dsl/documents.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,10 @@ def update(self, thing, refresh=None, action='index', parallel=False, **kwargs):
191191
"""
192192
Update each document in ES for a model, iterable of models or queryset
193193
"""
194-
if refresh is True or (
195-
refresh is None and self.django.auto_refresh
196-
):
194+
if refresh is None and self.django.auto_refresh:
197195
kwargs['refresh'] = True
196+
elif refresh is not None:
197+
kwargs['refresh'] = refresh
198198

199199
if isinstance(thing, models.Model):
200200
object_list = [thing]

tests/test_documents.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,26 @@ def test_model_instance_update_no_refresh(self):
268268
doc.update(car)
269269
self.assertNotIn('refresh', mock.call_args_list[0][1])
270270

271+
def test_model_instance_update_refresh_wait_for(self):
272+
doc = CarDocument()
273+
doc.django.auto_refresh = False
274+
car = Car()
275+
with patch('django_elasticsearch_dsl.documents.bulk') as mock:
276+
doc.update(car, refresh='wait_for')
277+
self.assertEqual(
278+
mock.call_args_list[0][1]['refresh'], 'wait_for'
279+
)
280+
281+
def test_model_instance_update_refresh_wait_for_overrides_auto_refresh(self):
282+
doc = CarDocument()
283+
doc.django.auto_refresh = True
284+
car = Car()
285+
with patch('django_elasticsearch_dsl.documents.bulk') as mock:
286+
doc.update(car, refresh='wait_for')
287+
self.assertEqual(
288+
mock.call_args_list[0][1]['refresh'], 'wait_for'
289+
)
290+
271291
def test_model_instance_iterable_update_with_pagination(self):
272292
class CarDocument2(DocType):
273293
class Django:

0 commit comments

Comments
 (0)