Permalink
Browse files

./manage.py reindex_all now uses index_components() methods

  • Loading branch information...
simonw committed Sep 30, 2017
1 parent c7e7b30 commit 7f6b524c8b6c261c0deaaddf7f19bc2efc626c74
Showing with 8 additions and 28 deletions.
  1. +7 −27 blog/management/commands/reindex_all.py
  2. +1 −1 blog/models.py
@@ -1,35 +1,15 @@
from django.core.management.base import BaseCommand
from django.db.models import Value, F, Func
from django.contrib.postgres.search import SearchVector
from blog.models import Entry, Blogmark, Quotation
class Command(BaseCommand):
help = "Re-indexes all entries, blogmarks, quotations"
def handle(self, *args, **kwargs):
print 'entries', Entry.objects.update(search_document=entry_vector_fields_only)
print 'blogmarks', Blogmark.objects.update(search_document=blogmark_vector_fields_only)
print 'quotations', Quotation.objects.update(search_document=quotation_vector_fields_only)
def strip_tags_func(field):
return Func(
F(field), Value('<.*?>'), Value(''), Value('g'), function='regexp_replace'
)
entry_vector_fields_only = (
SearchVector('title', weight='A') +
SearchVector(strip_tags_func('body'), weight='C')
)
blogmark_vector_fields_only = (
SearchVector('link_title', weight='A') +
SearchVector(strip_tags_func('commentary'), weight='C')
)
quotation_vector_fields_only = (
SearchVector('source', weight='A') +
SearchVector('quotation', weight='B')
)
for klass in (Entry, Blogmark, Quotation):
i = 0
for obj in klass.objects.prefetch_related('tags').all():
obj.save()
i += 1
if i % 100 == 0:
print klass, i
View
@@ -151,7 +151,7 @@ def index_components(self):
return {
'A': self.link_title,
'B': ' '.join(self.tags.values_list('tag', flat=True)),
'C': self.commentary + ' ' + self.link_domain + ' ' + (self.via_title or ''),
'C': self.commentary + ' ' + self.link_domain() + ' ' + (self.via_title or ''),
}
def __unicode__(self):

0 comments on commit 7f6b524

Please sign in to comment.