+ def delete_query(self, *queries):
+ """
+ index.delete(queries=...) doesn't work, so let's reimplement it
+ using deletion of list of uids.
+ """
+ uids = set()
+ for q in queries:
+ if isinstance(q, sunburnt.search.LuceneQuery):
+ q = self.index.query(q)
+ q.field_limiter.update(['uid'])
+ st = 0
+ rows = 100
+ while True:
+ ids = q.paginate(start=st, rows=rows).execute()
+ if not len(ids):
+ break
+ for res in ids:
+ uids.add(res['uid'])
+ st += rows
+ # print "Will delete %s" % ','.join([x for x in uids])
+ if uids:
+ self.index.delete(uids)
+ return True
+ else:
+ return False
+
+ 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:
+ 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
+
+ self.delete_query(self.index.Q(book_id=book_id))
+
+ if remove_snippets:
+ snippets = Snippets(book_id)
+ snippets.remove()
+
+ def index_book(self, book, book_info=None, overwrite=True):
+ """
+ Indexes the book.
+ Creates a lucene document for extracted metadata
+ and calls self.index_content() to index the contents of the book.
+ """
+ if overwrite:
+ # we don't remove snippets, since they might be still needed by
+ # threads using not reopened index
+ self.remove_book(book, remove_snippets=False)
+
+ book_doc = self.create_book_doc(book)
+ meta_fields = self.extract_metadata(book, book_info, dc_only=['source_name', 'authors', 'title'])
+ # let's not index it - it's only used for extracting publish date
+ if 'source_name' in meta_fields:
+ del meta_fields['source_name']
+
+ for n, f in meta_fields.items():
+ book_doc[n] = f
+
+ book_doc['uid'] = "book%s" % book_doc['book_id']
+ self.index.add(book_doc)
+ del book_doc
+ book_fields = {
+ 'title': meta_fields['title'],
+ 'authors': meta_fields['authors'],
+ 'published_date': meta_fields['published_date']
+ }
+ if 'translators' in meta_fields:
+ book_fields['translators'] = meta_fields['translators']
+
+ self.index_content(book, book_fields=book_fields)