From 753ba5d3f4bd3b5bf96aebd5770e15f6f6eda8dd Mon Sep 17 00:00:00 2001 From: Peter Eckel <6815386+peteeckel@users.noreply.github.com> Date: Mon, 1 Jul 2024 15:12:02 +0200 Subject: [PATCH] Do not delete all search indexes when reindexing specific models (#16755) * Do not delete all search indexes when reindexing specific models * Clear all indexes only if neither --lazy nor a list of models are specified for "manage.py reindex" * Otherwise, clear the index for a model immediately before rebuilding it * Separated clearing from re-indexing the search cache --- netbox/extras/management/commands/reindex.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/netbox/extras/management/commands/reindex.py b/netbox/extras/management/commands/reindex.py index e20fad0ce..5aab74511 100644 --- a/netbox/extras/management/commands/reindex.py +++ b/netbox/extras/management/commands/reindex.py @@ -66,11 +66,16 @@ class Command(BaseCommand): raise CommandError(_("No indexers found!")) self.stdout.write(f'Reindexing {len(indexers)} models.') - # Clear all cached values for the specified models (if not being lazy) + # Clear cached values for the specified models (if not being lazy) if not kwargs['lazy']: + if model_labels: + content_types = [ContentType.objects.get_for_model(model) for model in indexers.keys()] + else: + content_types = None + self.stdout.write('Clearing cached values... ', ending='') self.stdout.flush() - deleted_count = search_backend.clear() + deleted_count = search_backend.clear(object_types=content_types) self.stdout.write(f'{deleted_count} entries deleted.') # Index models