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
content=fix_format(u' '.join(filter(lambda s: s is not None, content))))
self.index.addDocument(doc)
+ position += 1
finally:
snippets.close()
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"))
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
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)
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 = {}
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):
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):
"""
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()
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)
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',
}
}
+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