X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/35b64fd8bec183054b63234aebf8782b87cf5cc5..013ec8a416d4ff1ea3e1f2fa9ee2935623197ecd:/apps/search/index.py diff --git a/apps/search/index.py b/apps/search/index.py index ea1a6c581..31417ca87 100644 --- a/apps/search/index.py +++ b/apps/search/index.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- - +# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# from django.conf import settings import os @@ -93,7 +95,8 @@ class Snippets(object): def close(self): """Close snippet file""" - self.file.close() + if self.file: + self.file.close() def remove(self): self.revision = None @@ -133,7 +136,6 @@ class Index(SolrIndex): 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 @@ -163,7 +165,7 @@ class Index(SolrIndex): q_id_cat = self.index.Q(q_id & q_cat) tag_qs.append(q_id_cat) - self.delete_query(tag_qs) + self.delete_query(*tag_qs) else: # all q = self.index.Q(tag_id__any=True) self.delete_query(q) @@ -464,7 +466,6 @@ class Index(SolrIndex): text=u''.join(footnote), is_footnote=True) self.index.add(doc) - #print "@ footnote text: %s" % footnote footnote = [] # handle fragments and themes. @@ -497,7 +498,6 @@ class Index(SolrIndex): fragment_anchor=fid, text=fix_format(frag['text']), themes=frag['themes']) - #print '@ FRAG %s' % frag['content'] self.index.add(doc) # Collect content. @@ -510,7 +510,6 @@ class Index(SolrIndex): # in the end, add a section text. doc = add_part(snippets, header_index=position, header_type=header.tag, text=fix_format(content)) - #print '@ CONTENT: %s' % fix_format(content) self.index.add(doc) @@ -829,7 +828,7 @@ class Search(SolrIndex): idx += 1 except IOError, e: - log.error("Cannot open snippet file for book id = %d [rev=%d], %s" % (book_id, revision, e)) + log.error("Cannot open snippet file for book id = %d [rev=%s], %s" % (book_id, revision, e)) return [] finally: snippets.close() @@ -853,7 +852,7 @@ class Search(SolrIndex): q |= self.index.Q(**{field: query + "*"}) else: q |= self.make_term_query(query, field=field) - qu = self.index.query(q).exclude(tag_category="book") + qu = self.index.query(q) return self.search_tags(qu, pdcounter=pdcounter) @@ -867,6 +866,8 @@ class Search(SolrIndex): res = self.apply_filters(query, filters).execute() tags = [] + pd_tags = [] + for doc in res: is_pdcounter = doc.get('is_pdcounter', False) category = doc.get('tag_category') @@ -878,17 +879,19 @@ class Search(SolrIndex): tag = PDCounterBook.objects.get(id=doc.get('tag_id')) tag.category = 'pd_book' # make it look more lik a tag. else: - print "Warning. cannot get pdcounter tag_id=%d from db; cat=%s" % (int(doc.get('tag_id')), category) + print ("Warning. cannot get pdcounter tag_id=%d from db; cat=%s" % (int(doc.get('tag_id')), category)).encode('utf-8') + pd_tags.append(tag) else: tag = catalogue.models.Tag.objects.get(id=doc.get("tag_id")) - # don't add the pdcounter tag if same tag already exists - - tags.append(tag) + tags.append(tag) except catalogue.models.Tag.DoesNotExist: pass except PDCounterAuthor.DoesNotExist: pass except PDCounterBook.DoesNotExist: pass + tags_slugs = set(map(lambda t: t.slug, tags)) + tags = tags + filter(lambda t: not t.slug in tags_slugs, pd_tags) + log.debug('search_tags: %s' % tags) return tags @@ -914,10 +917,15 @@ class Search(SolrIndex): Searches for Book objects using query """ bks = [] + bks_found = set() + query = query.query(is_book=True) res = self.apply_filters(query, filters).field_limit(['book_id']) for r in res: try: - bks.append(catalogue.models.Book.objects.get(id=r['book_id'])) + bid = r['book_id'] + if not bid in bks_found: + bks.append(catalogue.models.Book.objects.get(id=bid)) + bks_found.add(bid) except catalogue.models.Book.DoesNotExist: pass return bks @@ -932,3 +940,7 @@ class Search(SolrIndex): for f in filters: query = query.query(f) return query + + +if getattr(settings, 'SEARCH_MOCK', False): + from .mock_search import Search