Skip to content

Commit 753ba5d

Browse files
authored
Do not delete all search indexes when reindexing specific models (netbox-community#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
1 parent b5d8e65 commit 753ba5d

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

netbox/extras/management/commands/reindex.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ def handle(self, *model_labels, **kwargs):
6666
raise CommandError(_("No indexers found!"))
6767
self.stdout.write(f'Reindexing {len(indexers)} models.')
6868

69-
# Clear all cached values for the specified models (if not being lazy)
69+
# Clear cached values for the specified models (if not being lazy)
7070
if not kwargs['lazy']:
71+
if model_labels:
72+
content_types = [ContentType.objects.get_for_model(model) for model in indexers.keys()]
73+
else:
74+
content_types = None
75+
7176
self.stdout.write('Clearing cached values... ', ending='')
7277
self.stdout.flush()
73-
deleted_count = search_backend.clear()
78+
deleted_count = search_backend.clear(object_types=content_types)
7479
self.stdout.write(f'{deleted_count} entries deleted.')
7580

7681
# Index models

0 commit comments

Comments
 (0)