From: Marcin Koziej Date: Mon, 10 Sep 2012 12:26:49 +0000 (+0200) Subject: fix snippet display X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/c54df654d0d513e91167a53454c5c2fdcd166752?ds=inline;hp=f6903aaf5ffb78a75f2b303b165aca7112211d84 fix snippet display --- diff --git a/apps/search/custom.py b/apps/search/custom.py index 6c16f228f..788b6c409 100644 --- a/apps/search/custom.py +++ b/apps/search/custom.py @@ -134,18 +134,22 @@ class CustomSolrInterface(sunburnt.SolrInterface): start = None end = None totlen = len(text) - matches_margins = map(lambda (s, e): (max(0, s - margins), min(totlen, e + margins)), matches) - (start, end) = matches_margins[0] - - for (s, e) in matches_margins[1:]: + matches_margins = map(lambda (s, e): + ((s, e), + (max(0, s - margins), min(totlen, e + margins))), + matches) + (start, end) = matches_margins[0][1] + matches = [] + for (m, (s, e)) in matches_margins[1:]: if end < s or start > e: continue start = min(start, s) end = max(end, e) - + matches.append(m) + snip = text[start:end] - matches = list(matches) matches.sort(lambda a, b: cmp(b[0], a[0])) + for (s, e) in matches: off = - start snip = snip[:e + off] + mark[1] + snip[e + off:] diff --git a/apps/search/templatetags/search_tags.py b/apps/search/templatetags/search_tags.py index 6a2ccad44..57734c10c 100644 --- a/apps/search/templatetags/search_tags.py +++ b/apps/search/templatetags/search_tags.py @@ -46,6 +46,9 @@ def book_searched(context, result): continue snip = result.snippets[idx] # fix some formattting + snip = re.subn(r"(^[ \t\n]+|[ \t\n]+$)", u"", + re.subn(r"[ \t\n]*\n[ \t\n]*", u"\n", snip)) + snip = snip.replace("\n", "
").replace('---', '—') hit['snippet'] = snip @@ -54,6 +57,6 @@ def book_searched(context, result): 'book': book, 'main_link': book.get_absolute_url(), 'request': context.get('request'), - 'hits': zip(*hits)[1], + 'hits': hits and zip(*hits)[1] or [], 'main_link': book.get_absolute_url(), } diff --git a/apps/search/views.py b/apps/search/views.py index 98184299a..72852d0d4 100644 --- a/apps/search/views.py +++ b/apps/search/views.py @@ -168,17 +168,9 @@ def main(request): ('text', text_phrase), ('text', everywhere)]: res.sort(reverse=True) - print "get snips %s, res size %d" % (field, len(res)) for r in res: - print "Get snippets for %s" % r search.get_snippets(r, query, field, 3) - # for r in res: - # for h in r.hits: - # h['snippets'] = map(lambda s: - # re.subn(r"(^[ \t\n]+|[ \t\n]+$)", u"", - # re.subn(r"[ \t\n]*\n[ \t\n]*", u"\n", s)[0])[0], h['snippets']) - # suggestion = did_you_mean(query, search.get_tokens(toks, field="SIMPLE")) suggestion = u'' def ensure_exists(r):