From: Marcin Koziej Date: Sun, 22 Jan 2012 21:29:31 +0000 (+0100) Subject: result categories working. X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/e0e33e24cf53aa1b65e08bcabcc674529d09741a?ds=inline result categories working. --- diff --git a/apps/search/index.py b/apps/search/index.py index 9972c2c56..adb76797e 100644 --- a/apps/search/index.py +++ b/apps/search/index.py @@ -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() diff --git a/apps/search/views.py b/apps/search/views.py index e9b25649c..00391f1be 100644 --- a/apps/search/views.py +++ b/apps/search/views.py @@ -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', diff --git a/wolnelektury/settings.py b/wolnelektury/settings.py index 01f5737db..48d96e056 100644 --- a/wolnelektury/settings.py +++ b/wolnelektury/settings.py @@ -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 diff --git a/wolnelektury/templates/catalogue/search_multiple_hits.html b/wolnelektury/templates/catalogue/search_multiple_hits.html index 31e67a8c1..a3c725837 100644 --- a/wolnelektury/templates/catalogue/search_multiple_hits.html +++ b/wolnelektury/templates/catalogue/search_multiple_hits.html @@ -48,7 +48,6 @@ {% endif %} {% if results.content %} - {% for result in results.content %}

{% trans "Results in text" %}

@@ -56,18 +55,16 @@
    - {% for result in results.title %} + {% for result in results.content %}
  1. {% book_searched result %}
  2. {% endfor %}
- {% endfor %} {% endif %} {% if results.other %} - {% for result in results.other %}

{% trans "Other results" %}

@@ -82,7 +79,6 @@ {% endfor %}
- {% endfor %} {% endif %}