<span class="did_you_mean">{% trans "Did you mean" %}
<a href="{% url 'search' %}?q={{did_you_mean|urlencode}}">{{did_you_mean|lower}}</a>?</span>
{% endif %}
- <!-- tu pójdą trafienia w tagi: Autorzy - z description oraz motywy i rodzaje -->
<div class="inline-tag-lists top-tag-list">
- {% if tags.author %}
+ {% if tags %}
<div>
- <h2>{% trans "Authors" %}:</h2>
- {% for tag in tags.author %}
- <a class="tag-box" href="{{ tag.get_absolute_url }}">
- {% include "catalogue/tag_box.html" %}
- </a>
- {% endfor %}
- </div>
- {% endif %}
- {% if tags.kind %}
- <div>
- <h2>{% trans "Kinds" %}:</h2>
- {% for tag in tags.kind %}
- <a class="tag-box" href="{{ tag.get_absolute_url }}">
- {% include "catalogue/tag_box.html" %}
- </a>
- {% endfor %}
- </div>
- {% endif %}
- {% if tags.genre %}
- <div>
- <h2>{% trans "Genres" %}:</h2>
- {% for tag in tags.genre %}
- <a class="tag-box" href="{{ tag.get_absolute_url }}">
- {% include "catalogue/tag_box.html" %}
- </a>
- {% endfor %}
- </div>
- {% endif %}
- {% if tags.epoch %}
- <div class="inline-tag-list">
- <h2>{% trans "Epochs" %}:</h2>
- {% for tag in tags.epoch %}
+ {% for tag in tags %}
<a class="tag-box" href="{{ tag.get_absolute_url }}">
{% include "catalogue/tag_box.html" %}
</a>
{% endif %}
</div>
- {% if results.title %}
- <div class="book-list-header">
- <div class="book-box-inner">
- <p>{% trans "Results by title" %}</p>
- </div>
- </div>
- <div>
- <ol class="work-list">
- {% for result in results.title %}
- <li class="Book-item">
- {% ssi_include 'catalogue_book_short' pk=result.book.pk %}
- </li>
- {% endfor %}
- </ol>
- </div>
- {% endif %}
-
- {% if results.author %}
- <div class="book-list-header">
- <div class="book-box-inner">
- <p>{% trans "Results by authors" %}</p>
- </div>
- </div>
- <div>
- <ol class="work-list">
- {% for author in results.author %}
- <li class="Book-item">{% ssi_include 'catalogue_book_short' pk=author.book.pk %}</li>
- {% endfor %}
- </ol>
- </div>
- {% endif %}
-
- {% if results.translator %}
- <div class="book-list-header">
- <div class="book-box-inner">
- <p>{% trans "Results by translators" %}</p>
- </div>
- </div>
- <div>
- <ol class="work-list">
- {% for translator in results.translator %}
- <li class="Book-item">{% ssi_include 'catalogue_book_short' pk=translator.book.pk %}</li>
- {% endfor %}
- </ol>
- </div>
- {% endif %}
-
- {% if results.content %}
- <div class="book-list-header">
- <div class="book-box-inner">
- <p>{% trans "Results in text" %}</p>
- </div>
- </div>
<div>
- <ol class="work-list">
- {% for result in results.content %}
+ <ul class="work-list">
+ {% for result in results %}
<li class="Book-item">
{% book_searched result %}
</li>
{% endfor %}
- </ol>
+ </ul>
</div>
- {% endif %}
-
- {% if results.other %}
- <div class="book-list-header">
- <div class="book-box-inner">
- <p>{% trans "Other results" %}</p>
- </div>
- </div>
- <div>
- <ol class="work-list">
- {% for result in results.other %}
- <li class="Book-item">
- {% book_searched result %}
- </li>
- {% endfor %}
- </ol>
- </div>
- {% endif %}
{% endblock %}
search = Search()
- # change hints
tags = search.hint_tags(query, pdcounter=True, prefix=False)
tags = split_tags(tags)
- author_results = search.search_words(words, ['authors'])
-
- title_results = search.search_words(words, ['title'])
-
- author_title_mixed = search.search_words(words, ['authors', 'title', 'metadata'])
- author_title_rest = []
-
- for b in author_title_mixed:
- also_in_mixed = filter(lambda ba: ba.book_id == b.book_id, author_results + title_results)
- for b2 in also_in_mixed:
- b2.boost *= 1.1
- if not also_in_mixed:
- author_title_rest.append(b)
-
- text_phrase = SearchResult.aggregate(search.search_words(words, ['text'], book=False))
-
- everywhere = search.search_words(words, ['metadata', 'text', 'themes_pl'], book=False)
-
- def already_found(results):
- def f(e):
- for r in results:
- if e.book_id == r.book_id:
- e.boost = 0.9
- results.append(e)
- return True
- return False
- return f
- f = already_found(author_results + title_results + text_phrase)
- everywhere = filter(lambda x: not f(x), everywhere)
-
- author_results = SearchResult.aggregate(author_results, author_title_rest)
- title_results = SearchResult.aggregate(title_results)
-
- everywhere = SearchResult.aggregate(everywhere, author_title_rest)
-
- for field, res in [('authors', author_results),
- ('title', title_results),
- ('text', text_phrase),
- ('text', everywhere)]:
- res.sort(reverse=True)
- for r in res:
- search.get_snippets(r, query, field, 3)
+ results_parts = []
+
+ search_fields = []
+ fieldsets = (
+ (['authors'], True),
+ (['title'], True),
+ (['metadata'], True),
+ (['text', 'themes_pl'], False),
+ )
+ for fieldset, is_book in fieldsets:
+ search_fields += fieldset
+ results_parts.append(search.search_words(words, search_fields, book=is_book))
+
+ results = []
+ ids_results = {}
+ for results_part in results_parts:
+ for result in sorted(SearchResult.aggregate(results_part), reverse=True):
+ book_id = result.book_id
+ if book_id in ids_results:
+ ids_results[book_id].merge(result)
+ else:
+ results.append(result)
+ ids_results[book_id] = result
+
+ for result in results:
+ search.get_snippets(result, query, num=3)
suggestion = u''
except Book.DoesNotExist:
return False
- author_results = filter(ensure_exists, author_results)
- title_results = filter(ensure_exists, title_results)
- text_phrase = filter(ensure_exists, text_phrase)
- everywhere = filter(ensure_exists, everywhere)
-
- # ensure books do exists & sort them
- for res in (author_results, title_results, text_phrase):
- res.sort(reverse=True)
+ results = filter(ensure_exists, results)
- if not (author_results or title_results or text_phrase or everywhere):
+ if not results:
form = PublishingSuggestForm(initial={"books": query + ", "})
return render_to_response(
'catalogue/search_no_hits.html',
return render_to_response(
'catalogue/search_multiple_hits.html',
{
- 'tags': tags,
+ 'tags': tags['author'] + tags['kind'] + tags['genre'] + tags['epoch'] + tags['theme'],
'prefix': query,
- 'results': {
- 'author': author_results,
- 'title': title_results,
- 'content': text_phrase,
- 'other': everywhere
- },
+ 'results': results,
'did_you_mean': suggestion
},
context_instance=RequestContext(request))