2 from django.core.management.base import BaseCommand
3 from search import Index, Search
4 from lucene import IndexReader, IndexSearcher, Term
5 from catalogue.models import Book
8 class Command(BaseCommand):
9 help = 'Optimize Lucene search index'
12 def delete_old(self, index):
13 existing_ids = set([book.id for book in Book.objects.all()])
15 reader = IndexReader.open(index.index, False)
16 searcher = IndexSearcher(reader)
18 num = searcher.docFreq(Term('is_book', 'true'))
19 docs = searcher.search(Search.make_term_query(['true'], 'is_book'), num)
20 for result in docs.scoreDocs:
21 stored = searcher.doc(result.doc)
22 book_id = int(stored.get('book_id'))
23 if not book_id in existing_ids:
24 print "book id %d doesn't exist." % book_id
25 index.remove_book(book_id)
30 def handle(self, *args, **opts):
34 self.delete_old(index)