simplify search results list
authorJan Szejko <janek37@gmail.com>
Fri, 8 Dec 2017 08:59:00 +0000 (09:59 +0100)
committerJan Szejko <janek37@gmail.com>
Fri, 8 Dec 2017 08:59:00 +0000 (09:59 +0100)
src/catalogue/templates/catalogue/search_multiple_hits.html
src/search/index.py
src/search/templatetags/search_tags.py
src/search/views.py

index 5ee0f41..b3f522b 100644 (file)
     <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 %}
index 4f9a765..7bc61c5 100644 (file)
@@ -744,7 +744,7 @@ class Search(SolrIndex):
         else:
             query = self.index.query()
         query = self.apply_filters(query, filters).field_limit(score=True, all_fields=True)
-        return [SearchResult(found, how_found='search_words') for found in query]
+        return [SearchResult(found, how_found='search_words') for found in query.execute()]
 
     def get_snippets(self, searchresult, query, field='text', num=1):
         """
index 517f18f..ea8d4ed 100644 (file)
@@ -22,13 +22,6 @@ register = template.Library()
 def book_searched(context, result):
     book = Book.objects.get(pk=result.book_id)
 
-    # snippets = []
-    # for hit in result.hits:
-    #     if hit['snippets']:
-    #         snippets.append(hit['snippets'])
-    #     elif hit['fragment']:
-    #         snippets.append(hit['fragment'].short_text)
-
     # We don't need hits which lead to sections but do not have
     # snippets.
     hits = filter(lambda (idx, h):
index 8b055a4..a659329 100644 (file)
@@ -153,52 +153,35 @@ def main(request):
 
     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''
 
@@ -208,16 +191,9 @@ def main(request):
         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',
@@ -232,14 +208,9 @@ def main(request):
     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))