fixup
parent
fb16187549
commit
39d80311bc
|
@ -38,13 +38,13 @@ class Command(BaseCommand):
|
|||
app_label = qs.model._meta.app_label
|
||||
model_name = qs.model.__name__
|
||||
|
||||
old_index_name = doc._doc_type.index
|
||||
index_name = doc._doc_type.index
|
||||
timestamp_prefix = 'temp-{}-'.format(datetime.datetime.now().strftime('%Y%m%d%H%M%S'))
|
||||
new_index_name = timestamp_prefix + old_index_name
|
||||
new_index_name = timestamp_prefix + index_name
|
||||
|
||||
pre_index_task = create_new_es_index_task.si(app_label=app_label,
|
||||
model_name=model_name,
|
||||
old_index_name=old_index_name,
|
||||
index_name=index_name,
|
||||
new_index_name=new_index_name)
|
||||
|
||||
indexing_tasks = self._get_indexing_tasks(app_label=app_label, model_name=model_name,
|
||||
|
@ -53,7 +53,7 @@ class Command(BaseCommand):
|
|||
index_name=new_index_name)
|
||||
|
||||
post_index_task = switch_es_index_task.si(app_label=app_label, model_name=model_name,
|
||||
old_index_name=old_index_name,
|
||||
index_name=index_name,
|
||||
new_index_name=new_index_name)
|
||||
|
||||
# Task to run in order to add the objects
|
||||
|
|
|
@ -3,8 +3,10 @@ from django.core.paginator import Paginator
|
|||
|
||||
|
||||
class RTDDocTypeMixin(object):
|
||||
|
||||
"""
|
||||
Override some methods of DocType of DED
|
||||
|
||||
Changelog as following:
|
||||
- Do not index object that not exist in the provided queryset
|
||||
- Take additional argument in update method `index_name` to update specific index
|
||||
|
@ -44,14 +46,16 @@ class RTDDocTypeMixin(object):
|
|||
|
||||
# TODO: remove this overwrite when the issue has been fixed
|
||||
# https://github.com/sabricot/django-elasticsearch-dsl/issues/111
|
||||
# Moreover, do not need to check if its a delete action
|
||||
# Because while delete action, the object is already remove from database
|
||||
if isinstance(thing, models.Model) and action != 'delete':
|
||||
if isinstance(thing, models.Model):
|
||||
# Its a model instance.
|
||||
queryset = self.get_queryset()
|
||||
obj = queryset.filter(pk=thing.pk)
|
||||
if not obj.exists():
|
||||
return None
|
||||
|
||||
# Do not need to check if its a delete action
|
||||
# Because while delete action, the object is already remove from database
|
||||
if action != 'delete':
|
||||
queryset = self.get_queryset()
|
||||
obj = queryset.filter(pk=thing.pk)
|
||||
if not obj.exists():
|
||||
return None
|
||||
|
||||
object_list = [thing]
|
||||
else:
|
||||
|
|
|
@ -23,21 +23,21 @@ def _get_document(model, document_class):
|
|||
|
||||
|
||||
@app.task(queue='web')
|
||||
def create_new_es_index_task(app_label, model_name, old_index_name, new_index_name):
|
||||
def create_new_es_index_task(app_label, model_name, index_name, new_index_name):
|
||||
model = apps.get_model(app_label, model_name)
|
||||
indices = registry.get_indices(models=[model])
|
||||
old_index = _get_index(indices=indices, index_name=old_index_name)
|
||||
old_index = _get_index(indices=indices, index_name=index_name)
|
||||
new_index = old_index.clone(name=new_index_name)
|
||||
new_index.create()
|
||||
|
||||
|
||||
@app.task(queue='web')
|
||||
def switch_es_index_task(app_label, model_name, old_index_name, new_index_name):
|
||||
def switch_es_index_task(app_label, model_name, index_name, new_index_name):
|
||||
model = apps.get_model(app_label, model_name)
|
||||
indices = registry.get_indices(models=[model])
|
||||
old_index = _get_index(indices=indices, index_name=old_index_name)
|
||||
|
||||
old_index = _get_index(indices=indices, index_name=index_name)
|
||||
new_index = old_index.clone(name=new_index_name)
|
||||
old_index_actual_name = None
|
||||
|
||||
if old_index.exists():
|
||||
# Alias can not be used to delete an index.
|
||||
|
@ -45,10 +45,12 @@ def switch_es_index_task(app_label, model_name, old_index_name, new_index_name):
|
|||
# So get the index actual name to delete it
|
||||
old_index_info = old_index.get()
|
||||
# The info is a dictionary and the key is the actual name of the index
|
||||
old_index_name = old_index_info.keys()[0]
|
||||
old_index.connection.indices.delete(index=old_index_name)
|
||||
old_index_actual_name = old_index_info.keys()[0]
|
||||
|
||||
new_index.put_alias(name=old_index_name)
|
||||
# Put alias into the new index name and delete the old index if its exist
|
||||
new_index.put_alias(name=index_name)
|
||||
if old_index_actual_name:
|
||||
old_index.connection.indices.delete(index=old_index_actual_name)
|
||||
|
||||
|
||||
@app.task(queue='web')
|
||||
|
|
Loading…
Reference in New Issue