Skip to content

Commit b22d27a

Browse files
committed
Add support for --refresh to search_index command
Currently when updating or rebuilding an index, the `refresh` parameter is never set, regardless if settings.ELASTICSEARCH_DSL_AUTO_REFRESH is set or if auto_refresh is set on the Document class. This commit adds the ability to pass `--refresh` to the `search_index` management command, to force a refresh when the index is updated. This can be useful, for example, if you want to programmatically call that command from some Django tests that fill an index, and then immediately run tests against that index. Without setting `refresh=True`, there's no guarantee that the refreshed index will be available to search.
1 parent 0e1e2b8 commit b22d27a

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

django_elasticsearch_dsl/management/commands/search_index.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ def add_arguments(self, parser):
6464
help='Run populate/rebuild update single threaded'
6565
)
6666
parser.set_defaults(parallel=getattr(settings, 'ELASTICSEARCH_DSL_PARALLEL', False))
67+
parser.add_argument(
68+
'--refresh',
69+
action='store_true',
70+
dest='refresh',
71+
default=None,
72+
help='Refresh indices after populate/rebuild'
73+
)
6774
parser.add_argument(
6875
'--no-count',
6976
action='store_false',
@@ -114,7 +121,7 @@ def _populate(self, models, options):
114121
"(parallel)" if parallel else "")
115122
)
116123
qs = doc().get_indexing_queryset()
117-
doc().update(qs, parallel=parallel)
124+
doc().update(qs, parallel=parallel, refresh=options['refresh'])
118125

119126
def _delete(self, models, options):
120127
index_names = [index._name for index in registry.get_indices(models)]

docs/source/management.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ Populate the Elasticsearch mappings with the django models data (index need to b
1818

1919
::
2020

21-
$ search_index --populate [--models [app[.model] app[.model] ...]] [--parallel]
21+
$ search_index --populate [--models [app[.model] app[.model] ...]] [--parallel] [--refresh]
2222

2323
Recreate and repopulate the indices:
2424

2525
::
2626

27-
$ search_index --rebuild [-f] [--models [app[.model] app[.model] ...]] [--parallel]
27+
$ search_index --rebuild [-f] [--models [app[.model] app[.model] ...]] [--parallel] [--refresh]
2828

tests/test_commands.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,24 @@ def test_create_all_indices(self):
112112

113113
def test_populate_all_doc_type(self):
114114
call_command('search_index', stdout=self.out, action='populate')
115+
expected_kwargs = {'parallel': False, 'refresh': None}
115116
# One call for "Indexing NNN documents", one for indexing itself (via get_index_queryset).
116117
assert self.doc_a1.get_queryset.call_count == 2
117-
self.doc_a1.update.assert_called_once_with(self.doc_a1_qs.iterator(), parallel=False)
118+
self.doc_a1.update.assert_called_once_with(self.doc_a1_qs.iterator(), **expected_kwargs)
118119
assert self.doc_a2.get_queryset.call_count == 2
119-
self.doc_a2.update.assert_called_once_with(self.doc_a2_qs.iterator(), parallel=False)
120+
self.doc_a2.update.assert_called_once_with(self.doc_a2_qs.iterator(), **expected_kwargs)
120121
assert self.doc_b1.get_queryset.call_count == 2
121-
self.doc_b1.update.assert_called_once_with(self.doc_b1_qs.iterator(), parallel=False)
122+
self.doc_b1.update.assert_called_once_with(self.doc_b1_qs.iterator(), **expected_kwargs)
122123
assert self.doc_c1.get_queryset.call_count == 2
123-
self.doc_c1.update.assert_called_once_with(self.doc_c1_qs.iterator(), parallel=False)
124+
self.doc_c1.update.assert_called_once_with(self.doc_c1_qs.iterator(), **expected_kwargs)
125+
126+
def test_populate_all_doc_type_refresh(self):
127+
call_command('search_index', stdout=self.out, action='populate', refresh=True)
128+
expected_kwargs = {'parallel': False, 'refresh': True}
129+
self.doc_a1.update.assert_called_once_with(self.doc_a1_qs.iterator(), **expected_kwargs)
130+
self.doc_a2.update.assert_called_once_with(self.doc_a2_qs.iterator(), **expected_kwargs)
131+
self.doc_b1.update.assert_called_once_with(self.doc_b1_qs.iterator(), **expected_kwargs)
132+
self.doc_c1.update.assert_called_once_with(self.doc_c1_qs.iterator(), **expected_kwargs)
124133

125134
def test_rebuild_indices(self):
126135

0 commit comments

Comments
 (0)