+ tags = request.GET.get('tags', '')
+ query = request.GET['q']
+ book_id = request.GET.get('book', None)
+ book = None
+ if book_id is not None:
+ book = get_object_or_404(Book, id=book_id)
+
+ hint = srch.hint()
+ try:
+ tag_list = Tag.get_tag_list(tags)
+ except:
+ tag_list = []
+
+ if len(query) < 2:
+ return render_to_response('catalogue/search_too_short.html', {'tags': tag_list, 'prefix': query},
+ context_instance=RequestContext(request))
+
+ hint.tags(tag_list)
+ hint.books(book)
+
+ toks = StringReader(query)
+ fuzzy = 'fuzzy' in request.GET
+ if fuzzy:
+ fuzzy = 0.7
+
+ results = SearchResult.aggregate(srch.search_perfect_book(toks, fuzzy=fuzzy, hint=hint),
+ srch.search_perfect_parts(toks, fuzzy=fuzzy, hint=hint),
+ srch.search_everywhere(toks, fuzzy=fuzzy, hint=hint))
+ results.sort(reverse=True)
+
+ for r in results:
+ print r.hits
+
+ 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])
+ return HttpResponseRedirect(frag.get_absolute_url())
+ elif len(results) == 0:
+ form = PublishingSuggestForm(initial={"books": query + ", "})
+ return render_to_response('catalogue/search_no_hits.html',
+ {'tags': tag_list, 'prefix': query, "pubsuggest_form": form,
+ 'form': forms.SearchForm()},
+ context_instance=RequestContext(request))
+
+ return render_to_response('catalogue/search_multiple_hits.html',
+ {'tags': tag_list, 'prefix': query,
+ 'results': results, 'from': forms.SearchForm()},
+ context_instance=RequestContext(request))