+ 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
+ """
+ log.debug("Indexing tags")
+ remove_only = kw.get('remove_only', False)
+ # first, remove tags from index.
+ if tags:
+ tag_qs = []
+ for tag in tags:
+ q_id = self.index.Q(tag_id=tag.id)
+
+ if isinstance(tag, PDCounterAuthor):
+ q_cat = self.index.Q(tag_category='pd_author')
+ elif isinstance(tag, PDCounterBook):
+ q_cat = self.index.Q(tag_category='pd_book')
+ else:
+ q_cat = self.index.Q(tag_category=tag.category)
+
+ q_id_cat = self.index.Q(q_id & q_cat)
+ tag_qs.append(q_id_cat)
+ self.delete_query(tag_qs)
+ else: # all
+ q = self.index.Q(tag_id__any=True)
+ self.delete_query(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 = {
+ "tag_id": int(tag.id),
+ "tag_name": tag.name,
+ "tag_name_pl": tag.name,
+ "tag_category": 'pd_author',
+ "is_pdcounter": True,
+ "uid": "tag%d_pd_a" % tag.id
+ }
+ elif isinstance(tag, PDCounterBook):
+ doc = {
+ "tag_id": int(tag.id),
+ "tag_name": tag.title,
+ "tag_name_pl": tag.title,
+ "tag_category": 'pd_book',
+ "is_pdcounter": True,
+ "uid": "tag%d_pd_b" % tag.id
+ }
+ else:
+ doc = {
+ "tag_id": int(tag.id),
+ "tag_name": tag.name,
+ "tag_name_pl": tag.name,
+ "tag_category": tag.category,
+ "is_pdcounter": False,
+ "uid": "tag%d" % tag.id
+ }
+ self.index.add(doc)
+
+ def create_book_doc(self, book):
+ """
+ Create a lucene document referring book id.
+ """
+ doc = {
+ 'book_id': int(book.id),
+ }
+ if book.parent is not None:
+ doc["parent_id"] = 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