result categories working.
authorMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Sun, 22 Jan 2012 21:29:31 +0000 (22:29 +0100)
committerMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Mon, 23 Jan 2012 12:26:14 +0000 (13:26 +0100)
apps/search/index.py
apps/search/views.py
wolnelektury/settings.py
wolnelektury/templates/catalogue/search_multiple_hits.html

index 9972c2c..adb7679 100644 (file)
@@ -397,8 +397,9 @@ class Index(BaseIndex):
 
         fragments = {}
         snippets = Snippets(book.id).open('w')
+        position = 0
         try:
-            for header, position in zip(list(master), range(len(master))):
+            for header in list(master):
 
                 if header.tag in self.skip_header_tags:
                     continue
@@ -459,6 +460,7 @@ class Index(BaseIndex):
                                content=fix_format(u' '.join(filter(lambda s: s is not None, content))))
 
                 self.index.addDocument(doc)
+                position += 1
 
         finally:
             snippets.close()
@@ -564,7 +566,7 @@ class SearchResult(object):
         self.boost = 1.0
 
         self._hits = []
-        self.hits = None  # processed hits
+        self._processed_hits = None  # processed hits
 
         stored = searcher.doc(scoreDocs.doc)
         self.book_id = int(stored.get("book_id"))
@@ -606,7 +608,11 @@ class SearchResult(object):
 
     book = property(get_book)
 
-    def process_hits(self):
+    @property
+    def hits(self):
+        if self._processed_hits is not None:
+            return self._processed_hits
+
         POSITION = 0
         FRAGMENT = 1
         POSITION_INDEX = 1
@@ -683,9 +689,9 @@ class SearchResult(object):
 
         hits.sort(lambda a, b: cmp(a['score'], b['score']), reverse=True)
 
-        self.hits = hits
+        self._processed_hits = hits
 
-        return self
+        return hits
 
     def __unicode__(self):
         return u'SearchResult(book_id=%d, score=%d)' % (self.book_id, self.score)
@@ -913,7 +919,7 @@ class Search(IndexStore):
         return q
 
     def search_phrase(self, searched, field, book=True, max_results=20, fuzzy=False,
-                      filters=None, tokens_cache=None, boost=None):
+                      filters=None, tokens_cache=None, boost=None, snippets=False):
         if filters is None: filters = []
         if tokens_cache is None: tokens_cache = {}
 
@@ -924,7 +930,7 @@ class Search(IndexStore):
             filters.append(self.term_filter(Term('is_book', 'true')))
         top = self.searcher.search(query, self.chain_filters(filters), max_results)
 
-        return [SearchResult(self.searcher, found) for found in top.scoreDocs]
+        return [SearchResult(self.searcher, found, snippets=(snippets and self.get_snippets(found, query) or None)) for found in top.scoreDocs]
 
     def search_some(self, searched, fields, book=True, max_results=20, fuzzy=False,
                     filters=None, tokens_cache=None, boost=None):
@@ -944,7 +950,7 @@ class Search(IndexStore):
 
         top = self.searcher.search(query, self.chain_filters(filters), max_results)
 
-        return [SearchResult(self.searcher, found, searched=searched, tokens_cache=tokens_cache) for found in top.scoreDocs]
+        return [SearchResult(self.searcher, found, searched=searched, tokens_cache=tokens_cache, snippets=self.get_snippets(found, query)) for found in top.scoreDocs]
 
     def search_perfect_book(self, searched, max_results=20, fuzzy=False, hint=None):
         """
@@ -1123,11 +1129,15 @@ class Search(IndexStore):
 
         stored = self.searcher.doc(scoreDoc.doc)
 
+        position = stored.get('snippets_position')
+        length = stored.get('snippets_length')
+        if position is None or length is None:
+            return None
         # locate content.
         snippets = Snippets(stored.get('book_id')).open()
         try:
-            text = snippets.get((int(stored.get('snippets_position')),
-                                 int(stored.get('snippets_length'))))
+            text = snippets.get((int(position),
+                                 int(length)))
         finally:
             snippets.close()
 
index e9b2564..00391f1 100644 (file)
@@ -138,11 +138,9 @@ def main(request):
             if bks is []:
                 author_title_rest.append(b)
         
-        text_phrase = SearchResult.aggregate(srch.search_phrase(toks, 'content', fuzzy=fuzzy, tokens_cache=tokens_cache))
-        [r.process_hits() for r in text_phrase]
+        text_phrase = SearchResult.aggregate(srch.search_phrase(toks, 'content', fuzzy=fuzzy, tokens_cache=tokens_cache, snippets=True, book=False))
         
         everywhere = SearchResult.aggregate(srch.search_everywhere(toks, fuzzy=fuzzy, tokens_cache=tokens_cache), author_title_rest)
-        [r.process_hits() for r in everywhere]
 
         for res in [author_results, title_results, text_phrase, everywhere]:
             res.sort(reverse=True)
@@ -153,11 +151,12 @@ def main(request):
         results.sort(reverse=True)
         
         if len(results) == 1:
-            if len(results[0].hits) == 0:
-                return HttpResponseRedirect(results[0].book.get_absolute_url())
-            elif len(results[0].hits) == 1 and results[0].hits[0] is not None:
-                frag = Fragment.objects.get(anchor=results[0].hits[0])
+            fragment_hits = filter(lambda h: 'fragment' in h, results[0].hits)
+            if len(fragment_hits) == 1:
+                anchor = fragment_hits[0]['fragment']
+                frag = Fragment.objects.get(anchor=anchor)
                 return HttpResponseRedirect(frag.get_absolute_url())
+            return HttpResponseRedirect(results[0].book.get_absolute_url())
         elif len(results) == 0:
             form = PublishingSuggestForm(initial={"books": query + ", "})
             return render_to_response('catalogue/search_no_hits.html',
index 01f5737..48d96e0 100644 (file)
@@ -24,6 +24,18 @@ DATABASES = {
     }
 }
 
+DATABASES['default'] = {
+    'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
+    'NAME': 'mkoziej_wl', # Or path to database file if using sqlite3.
+    'USER': 'mkoziej',                      # Not used with sqlite3.
+    'PASSWORD': 'pelolpe',                  # Not used with sqlite3.
+}
+
+DATABASE_OPTIONS = {
+   "init_command": "SET storage_engine=INNODB DEFAULT CHARSET=utf8",
+}
+
+
 
 # Local time zone for this installation. Choices can be found here:
 # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
index 31e67a8..a3c7258 100644 (file)
@@ -48,7 +48,6 @@
     {% endif %}
 
     {% if results.content %}
-    {% for result in results.content %}
     <div class="book-list-header">
       <div class="book-box-inner">
       <p>{% trans "Results in text" %}</p>
     </div>
     <div>
       <ol class="work-list">
-       {% for result in results.title %}
+       {% for result in results.content %}
        <li class="work-item">
           {% book_searched result %}
        </li>
        {% endfor %}
       </ol>
     </div>
-    {% endfor %}
     {% endif %}
 
     {% if results.other %}
-    {% for result in results.other %}
     <div class="book-list-header">
       <div class="book-box-inner">
        <p>{% trans "Other results" %}</p>
@@ -82,7 +79,6 @@
        {% endfor %}
       </ol>
     </div>
-    {% endfor %}
     {% endif %}