- q = srch.index.query(
- reduce(
- operator.or_,
- [srch.index.Q(**{self.PARAMS_TO_FIELDS.get(cn, cn): query}) for cn in self.MATCHES.keys()],
- srch.index.Q()))
- else:
- q = srch.index.query(srch.index.Q())
-
- q = srch.apply_filters(q, filters).field_limit(score=True, fields=['book_id'])
- results = q.execute()
-
- book_scores = dict([(r['book_id'], r['score']) for r in results])
- books = Book.objects.filter(id__in=set([r['book_id'] for r in results]))
- books = list(books)
- books.sort(reverse=True, key=lambda book: book_scores[book.id])
+ squery = UnaccentSearchQuery(query, config=settings.SEARCH_CONFIG)
+ books = books.filter(search_vector=squery)
+ if criteria['author']:
+ authors = Tag.objects.filter(category='author').annotate(
+ search_vector=UnaccentSearchVector('name_pl')
+ ).filter(search_vector=UnaccentSearchQuery(criteria['author'], config=settings.SEARCH_CONFIG))
+ books = books.filter(tag_relations__tag__in=authors)
+ if criteria['categories']:
+ tags = Tag.objects.filter(category__in=('genre', 'kind', 'epoch')).annotate(
+ search_vector=UnaccentSearchVector('name_pl')
+ ).filter(search_vector=UnaccentSearchQuery(criteria['categories'], config=settings.SEARCH_CONFIG))
+ books = books.filter(tag_relations__tag__in=tags)
+ if criteria['translator']:
+ # TODO
+ pass
+ if criteria['title']:
+ books = books.filter(
+ search_vector=UnaccentSearchQuery(criteria['title'], config=settings.SEARCH_CONFIG)
+ )
+
+ books = books.exclude(ancestor__in=books)
+
+ books = books.order_by('popularity__count')