Skip to content

Commit 7df3ca1

Browse files
committed
fixup
1 parent 8622b10 commit 7df3ca1

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

readthedocs/search/management/commands/reindex_elasticsearch.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ def _run_reindex_tasks(self, models):
3838
app_label = qs.model._meta.app_label
3939
model_name = qs.model.__name__
4040

41-
old_index_name = doc._doc_type.index
41+
index_name = doc._doc_type.index
4242
timestamp_prefix = 'temp-{}-'.format(datetime.datetime.now().strftime('%Y%m%d%H%M%S'))
43-
new_index_name = timestamp_prefix + old_index_name
43+
new_index_name = timestamp_prefix + index_name
4444

4545
pre_index_task = create_new_es_index_task.si(app_label=app_label,
4646
model_name=model_name,
47-
old_index_name=old_index_name,
47+
index_name=index_name,
4848
new_index_name=new_index_name)
4949

5050
indexing_tasks = self._get_indexing_tasks(app_label=app_label, model_name=model_name,
@@ -53,7 +53,7 @@ def _run_reindex_tasks(self, models):
5353
index_name=new_index_name)
5454

5555
post_index_task = switch_es_index_task.si(app_label=app_label, model_name=model_name,
56-
old_index_name=old_index_name,
56+
index_name=index_name,
5757
new_index_name=new_index_name)
5858

5959
# Task to run in order to add the objects

readthedocs/search/mixins.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44

55
class RTDDocTypeMixin(object):
6+
67
"""
78
Override some methods of DocType of DED
9+
810
Changelog as following:
911
- Do not index object that not exist in the provided queryset
1012
- Take additional argument in update method `index_name` to update specific index
@@ -44,14 +46,16 @@ def update(self, thing, refresh=None, action='index', index_name=None, **kwargs)
4446

4547
# TODO: remove this overwrite when the issue has been fixed
4648
# https://github.com/sabricot/django-elasticsearch-dsl/issues/111
47-
# Moreover, do not need to check if its a delete action
48-
# Because while delete action, the object is already remove from database
49-
if isinstance(thing, models.Model) and action != 'delete':
49+
if isinstance(thing, models.Model):
5050
# Its a model instance.
51-
queryset = self.get_queryset()
52-
obj = queryset.filter(pk=thing.pk)
53-
if not obj.exists():
54-
return None
51+
52+
# Do not need to check if its a delete action
53+
# Because while delete action, the object is already remove from database
54+
if action != 'delete':
55+
queryset = self.get_queryset()
56+
obj = queryset.filter(pk=thing.pk)
57+
if not obj.exists():
58+
return None
5559

5660
object_list = [thing]
5761
else:

readthedocs/search/tasks.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,34 @@ def _get_document(model, document_class):
2323

2424

2525
@app.task(queue='web')
26-
def create_new_es_index_task(app_label, model_name, old_index_name, new_index_name):
26+
def create_new_es_index_task(app_label, model_name, index_name, new_index_name):
2727
model = apps.get_model(app_label, model_name)
2828
indices = registry.get_indices(models=[model])
29-
old_index = _get_index(indices=indices, index_name=old_index_name)
29+
old_index = _get_index(indices=indices, index_name=index_name)
3030
new_index = old_index.clone(name=new_index_name)
3131
new_index.create()
3232

3333

3434
@app.task(queue='web')
35-
def switch_es_index_task(app_label, model_name, old_index_name, new_index_name):
35+
def switch_es_index_task(app_label, model_name, index_name, new_index_name):
3636
model = apps.get_model(app_label, model_name)
3737
indices = registry.get_indices(models=[model])
38-
old_index = _get_index(indices=indices, index_name=old_index_name)
39-
38+
old_index = _get_index(indices=indices, index_name=index_name)
4039
new_index = old_index.clone(name=new_index_name)
40+
old_index_actual_name = None
4141

4242
if old_index.exists():
4343
# Alias can not be used to delete an index.
4444
# https://www.elastic.co/guide/en/elasticsearch/reference/6.0/indices-delete-index.html
4545
# So get the index actual name to delete it
4646
old_index_info = old_index.get()
4747
# The info is a dictionary and the key is the actual name of the index
48-
old_index_name = old_index_info.keys()[0]
49-
old_index.connection.indices.delete(index=old_index_name)
48+
old_index_actual_name = old_index_info.keys()[0]
5049

51-
new_index.put_alias(name=old_index_name)
50+
# Put alias into the new index name and delete the old index if its exist
51+
new_index.put_alias(name=index_name)
52+
if old_index_actual_name:
53+
old_index.connection.indices.delete(index=old_index_actual_name)
5254

5355

5456
@app.task(queue='web')

0 commit comments

Comments
 (0)