Skip to content

Commit 7f6b524

Browse files
author
Simon Willison
committed
./manage.py reindex_all now uses index_components() methods
1 parent c7e7b30 commit 7f6b524

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed
Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,15 @@
11
from django.core.management.base import BaseCommand
2-
from django.db.models import Value, F, Func
3-
from django.contrib.postgres.search import SearchVector
4-
52
from blog.models import Entry, Blogmark, Quotation
63

74

85
class Command(BaseCommand):
96
help = "Re-indexes all entries, blogmarks, quotations"
107

118
def handle(self, *args, **kwargs):
12-
print 'entries', Entry.objects.update(search_document=entry_vector_fields_only)
13-
print 'blogmarks', Blogmark.objects.update(search_document=blogmark_vector_fields_only)
14-
print 'quotations', Quotation.objects.update(search_document=quotation_vector_fields_only)
15-
16-
17-
def strip_tags_func(field):
18-
return Func(
19-
F(field), Value('<.*?>'), Value(''), Value('g'), function='regexp_replace'
20-
)
21-
22-
entry_vector_fields_only = (
23-
SearchVector('title', weight='A') +
24-
SearchVector(strip_tags_func('body'), weight='C')
25-
)
26-
27-
blogmark_vector_fields_only = (
28-
SearchVector('link_title', weight='A') +
29-
SearchVector(strip_tags_func('commentary'), weight='C')
30-
)
31-
32-
quotation_vector_fields_only = (
33-
SearchVector('source', weight='A') +
34-
SearchVector('quotation', weight='B')
35-
)
9+
for klass in (Entry, Blogmark, Quotation):
10+
i = 0
11+
for obj in klass.objects.prefetch_related('tags').all():
12+
obj.save()
13+
i += 1
14+
if i % 100 == 0:
15+
print klass, i

blog/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def index_components(self):
151151
return {
152152
'A': self.link_title,
153153
'B': ' '.join(self.tags.values_list('tag', flat=True)),
154-
'C': self.commentary + ' ' + self.link_domain + ' ' + (self.via_title or ''),
154+
'C': self.commentary + ' ' + self.link_domain() + ' ' + (self.via_title or ''),
155155
}
156156

157157
def __unicode__(self):

0 commit comments

Comments
 (0)