From c54df654d0d513e91167a53454c5c2fdcd166752 Mon Sep 17 00:00:00 2001 From: Marcin Koziej Date: Mon, 10 Sep 2012 14:26:49 +0200 Subject: [PATCH] fix snippet display --- apps/search/custom.py | 16 ++++++++++------ apps/search/templatetags/search_tags.py | 5 ++++- apps/search/views.py | 8 -------- 3 files changed, 14 insertions(+), 15 deletions(-) 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): -- 2.20.1