X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/9977ccb7fb21737b6f0e91f2eac264db9a59eea2..0528c9e30aac17fe6d7726485136c4995c05b1ec:/src/search/index.py diff --git a/src/search/index.py b/src/search/index.py index 5cae3e3e6..b94d8f617 100644 --- a/src/search/index.py +++ b/src/search/index.py @@ -304,7 +304,6 @@ class Index(SolrIndex): book_info = dcparser.parse(open(book.xml_file.path)) fields['slug'] = book.slug - fields['tags'] = [t.name for t in book.tags] fields['is_book'] = True # validator, name @@ -558,6 +557,17 @@ class SearchResult(object): self._hits.append(hit) + @classmethod + def from_book(cls, book, how_found=None, query_terms=None): + doc = { + 'score': book.popularity.count, + 'book_id': book.id, + 'published_date': 0, + } + result = cls(doc, how_found=how_found, query_terms=query_terms) + result._book = book + return result + def __unicode__(self): return u"" % \ (self.book_id, len(self._hits), @@ -573,10 +583,9 @@ class SearchResult(object): def merge(self, other): if self.book_id != other.book_id: - raise ValueError("this search result is or book %d; tried to merge with %d" % (self.book_id, other.book_id)) + raise ValueError("this search result is for book %d; tried to merge with %d" % (self.book_id, other.book_id)) self._hits += other._hits - if other.score > self.score: - self._score = other._score + self._score += max(other._score, 0) return self def get_book(self): @@ -735,10 +744,19 @@ class Search(SolrIndex): return q + def search_by_author(self, words): + from catalogue.models import Book + books = Book.objects.filter(parent=None).order_by('-popularity__count') + for word in words: + books = books.filter(cached_author__iregex='\m%s\M' % word).select_related('popularity__count') + return [SearchResult.from_book(book, how_found='search_by_author', query_terms=words) for book in books[:30]] + def search_words(self, words, fields, book=True): + if book and fields == ['authors']: + return self.search_by_author(words) filters = [] for word in words: - if word not in stopwords: + if book or (word not in stopwords): word_filter = None for field in fields: q = self.index.Q(**{field: word})