+ index_changed.send_robust(self)
+
+ super(BaseIndex, self).close()
+
+ def __enter__(self):
+ self.open()
+ return self
+
+ def __exit__(self, type, value, tb):
+ self.close()
+
+
+index_changed = Signal()
+
+
+class Index(BaseIndex):
+ """
+ Class indexing books.
+ """
+ def __init__(self, analyzer=None):
+ super(Index, self).__init__(analyzer)
+
+ def index_tags(self, *tags, **kw):
+ """
+ Re-index global tag list.
+ Removes all tags from index, then index them again.
+ Indexed fields include: id, name (with and without polish stems), category
+ """
+ remove_only = kw.get('remove_only', False)
+ # first, remove tags from index.
+ if tags:
+ q = BooleanQuery()
+ for tag in tags:
+ b_id_cat = BooleanQuery()
+
+ q_id = NumericRangeQuery.newIntRange("tag_id", tag.id, tag.id, True, True)
+ b_id_cat.add(q_id, BooleanClause.Occur.MUST)
+
+ if isinstance(tag, PDCounterAuthor):
+ q_cat = TermQuery(Term('tag_category', 'pd_author'))
+ elif isinstance(tag, PDCounterBook):
+ q_cat = TermQuery(Term('tag_category', 'pd_book'))
+ else:
+ q_cat = TermQuery(Term('tag_category', tag.category))
+ b_id_cat.add(q_cat, BooleanClause.Occur.MUST)
+
+ q.add(b_id_cat, BooleanClause.Occur.SHOULD)
+ else: # all
+ q = NumericRangeQuery.newIntRange("tag_id", 0, Integer.MAX_VALUE, True, True)
+ self.index.deleteDocuments(q)
+
+ if not remove_only:
+ # then add them [all or just one passed]
+ if not tags:
+ tags = chain(catalogue.models.Tag.objects.exclude(category='set'), \
+ PDCounterAuthor.objects.all(), \
+ PDCounterBook.objects.all())
+
+ for tag in tags:
+ if isinstance(tag, PDCounterAuthor):
+ doc = Document()
+ doc.add(NumericField("tag_id", Field.Store.YES, True).setIntValue(int(tag.id)))
+ doc.add(Field("tag_name", tag.name, Field.Store.NO, Field.Index.ANALYZED))
+ doc.add(Field("tag_name_pl", tag.name, Field.Store.NO, Field.Index.ANALYZED))
+ doc.add(Field("tag_category", 'pd_author', Field.Store.YES, Field.Index.NOT_ANALYZED))
+ doc.add(Field("is_pdcounter", 'true', Field.Store.YES, Field.Index.NOT_ANALYZED))
+ self.index.addDocument(doc)
+ elif isinstance(tag, PDCounterBook):
+ doc = Document()
+ doc.add(NumericField("tag_id", Field.Store.YES, True).setIntValue(int(tag.id)))
+ doc.add(Field("tag_name", tag.title, Field.Store.NO, Field.Index.ANALYZED))
+ doc.add(Field("tag_name_pl", tag.title, Field.Store.NO, Field.Index.ANALYZED))
+ doc.add(Field("tag_category", 'pd_book', Field.Store.YES, Field.Index.NOT_ANALYZED))
+ doc.add(Field("is_pdcounter", 'true', Field.Store.YES, Field.Index.NOT_ANALYZED))
+ self.index.addDocument(doc)
+ else:
+ doc = Document()
+ doc.add(NumericField("tag_id", Field.Store.YES, True).setIntValue(int(tag.id)))
+ doc.add(Field("tag_name", tag.name, Field.Store.NO, Field.Index.ANALYZED))
+ doc.add(Field("tag_name_pl", tag.name, Field.Store.NO, Field.Index.ANALYZED))
+ doc.add(Field("tag_category", tag.category, Field.Store.NO, Field.Index.NOT_ANALYZED))
+ self.index.addDocument(doc)
+
+ def create_book_doc(self, book):
+ """
+ Create a lucene document referring book id.
+ """
+ doc = Document()
+ doc.add(NumericField("book_id", Field.Store.YES, True).setIntValue(int(book.id)))
+ if book.parent is not None:
+ doc.add(NumericField("parent_id", Field.Store.YES, True).setIntValue(int(book.parent.id)))
+ return doc
+
+ def remove_book(self, book_or_id, remove_snippets=True):
+ """Removes a book from search index.
+ book - Book instance."""
+ if isinstance(book_or_id, catalogue.models.Book):
+ book_id = book_or_id.id
+ else:
+ book_id = book_or_id
+
+ q = NumericRangeQuery.newIntRange("book_id", book_id, book_id, True, True)