From 347e1a7ebb537daa29daced0903ec069c1621cdd Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 1 Feb 2023 22:37:09 -0500 Subject: [PATCH] reindex should skip non-indexable items --- catalog/management/commands/index.py | 14 ++++++++++++-- catalog/search/typesense.py | 4 ++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/catalog/management/commands/index.py b/catalog/management/commands/index.py index 3d261f43..cb808ea4 100644 --- a/catalog/management/commands/index.py +++ b/catalog/management/commands/index.py @@ -33,11 +33,19 @@ class Command(BaseCommand): "--reindex", action="store_true", ) + parser.add_argument( + "--delete", + action="store_true", + ) def init_index(self): Indexer.init() self.stdout.write(self.style.SUCCESS("Index created.")) + def delete(self): + Indexer.delete_index() + self.stdout.write(self.style.SUCCESS("Index deleted.")) + def update_index(self): Indexer.update_settings() self.stdout.write(self.style.SUCCESS("Index updated.")) @@ -52,7 +60,7 @@ class Command(BaseCommand): # Indexer.update_settings() # self.stdout.write(self.style.SUCCESS('Index settings updated.')) qs = Item.objects.filter( - is_deleted=False + is_deleted=False, merged_to_item_id__isnull=True ) # if h == 0 else c.objects.filter(edited_time__gt=timezone.now() - timedelta(hours=h)) pg = Paginator(qs.order_by("id"), BATCH_SIZE) for p in tqdm(pg.page_range): @@ -62,7 +70,7 @@ class Command(BaseCommand): [ x for x in pg.get_page(p).object_list - if x.__class__ != CatalogCollection + if hasattr(x, "indexable_fields") ], ) ) @@ -80,6 +88,8 @@ class Command(BaseCommand): self.stat() elif options["reindex"]: self.reindex() + elif options["delete"]: + self.delete() # else: # try: diff --git a/catalog/search/typesense.py b/catalog/search/typesense.py index 204521d1..263b7cd0 100644 --- a/catalog/search/typesense.py +++ b/catalog/search/typesense.py @@ -132,6 +132,10 @@ class Indexer: # cls.instance().collections[INDEX_NAME].delete() cls.instance().collections.create(cls.config()) + @classmethod + def delete_index(cls): + cls.instance().collections[INDEX_NAME].delete() + @classmethod def update_settings(cls): cls.instance().collections[INDEX_NAME].update(cls.config())