You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

95 lines
4.4 KiB

from django.core.management.base import BaseCommand
from apps.hadis.models import (
Hadis,
Transmitters,
BookReference,
HadisCategory,
HadisCorrection,
HadisInterpretation
)
from utils.text_normalizer import build_search_blob
class Command(BaseCommand):
help = "Backfill normalized_text on Hadis, Transmitters, Categories, BookReferences, Corrections, and Interpretations"
def handle(self, *args, **options):
self.stdout.write(self.style.SUCCESS("Starting search normalization backfill..."))
# 1. Hadis
self.stdout.write("1. Normalizing Hadis records...")
hadiths = list(Hadis.objects.all().only('id', 'text', 'title', 'title_narrator', 'translation', 'normalized_text'))
for h in hadiths:
h.normalized_text = build_search_blob(
h.text,
h.title,
h.title_narrator,
h.translation
)
Hadis.objects.bulk_update(hadiths, ['normalized_text'], batch_size=500)
self.stdout.write(self.style.SUCCESS(f" Successfully updated {len(hadiths)} Hadis records."))
# 2. Transmitters
self.stdout.write("2. Normalizing Transmitters records...")
transmitters = list(Transmitters.objects.all().only('id', 'full_name', 'kunya', 'known_as', 'nickname', 'normalized_text'))
for t in transmitters:
t.normalized_text = build_search_blob(
t.full_name,
t.kunya,
t.known_as,
t.nickname
)
Transmitters.objects.bulk_update(transmitters, ['normalized_text'], batch_size=500)
self.stdout.write(self.style.SUCCESS(f" Successfully updated {len(transmitters)} Transmitters records."))
# 3. BookReference
self.stdout.write("3. Normalizing BookReference records...")
books = list(BookReference.objects.select_related('author').all().only('id', 'title', 'description', 'author__name', 'normalized_text'))
for b in books:
author_name = b.author.name if b.author else ""
b.normalized_text = build_search_blob(
b.title,
b.description,
author_name
)
BookReference.objects.bulk_update(books, ['normalized_text'], batch_size=500)
self.stdout.write(self.style.SUCCESS(f" Successfully updated {len(books)} BookReference records."))
# 4. HadisCategory
self.stdout.write("4. Normalizing HadisCategory records...")
categories = list(HadisCategory.objects.all().only('id', 'title', 'description', 'normalized_text'))
for c in categories:
c.normalized_text = build_search_blob(
c.title,
c.description
)
HadisCategory.objects.bulk_update(categories, ['normalized_text'], batch_size=500)
self.stdout.write(self.style.SUCCESS(f" Successfully updated {len(categories)} HadisCategory records."))
# 5. HadisCorrection
self.stdout.write("5. Normalizing HadisCorrection records...")
corrections = list(HadisCorrection.objects.all().only('id', 'text', 'title', 'narrator', 'translation', 'normalized_text'))
for corr in corrections:
corr.normalized_text = build_search_blob(
corr.text,
corr.title,
corr.narrator,
corr.translation
)
HadisCorrection.objects.bulk_update(corrections, ['normalized_text'], batch_size=500)
self.stdout.write(self.style.SUCCESS(f" Successfully updated {len(corrections)} HadisCorrection records."))
# 6. HadisInterpretation
self.stdout.write("6. Normalizing HadisInterpretation records...")
interpretations = list(HadisInterpretation.objects.all().only('id', 'text', 'title', 'narrator', 'translation', 'normalized_text'))
for interp in interpretations:
interp.normalized_text = build_search_blob(
interp.text,
interp.title,
interp.narrator,
interp.translation
)
HadisInterpretation.objects.bulk_update(interpretations, ['normalized_text'], batch_size=500)
self.stdout.write(self.style.SUCCESS(f" Successfully updated {len(interpretations)} HadisInterpretation records."))
self.stdout.write(self.style.SUCCESS("All search normalization fields backfilled successfully!"))