|
1 | 1 | 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 |
| - |
5 | 2 | from blog.models import Entry, Blogmark, Quotation
|
6 | 3 |
|
7 | 4 |
|
8 | 5 | class Command(BaseCommand):
|
9 | 6 | help = "Re-indexes all entries, blogmarks, quotations"
|
10 | 7 |
|
11 | 8 | 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 |
0 commit comments