2 from django.shortcuts import render_to_response, get_object_or_404
3 from django.template import RequestContext
4 from django.contrib.auth.decorators import login_required
5 from django.views.decorators import cache
7 from catalogue.utils import get_random_hash
8 from catalogue.models import Book, Tag, TAG_CATEGORIES
9 from catalogue.fields import dumps
10 from catalogue.views import JSONResponse
11 from catalogue import forms
12 from search import MultiSearch, JVM, SearchResult
13 from lucene import StringReader
17 dictionary = enchant.Dict('pl_PL')
20 def did_you_mean(query, tokens):
24 print("%s ok? %s, sug: %s" %(t, dictionary.check(t), dictionary.suggest(t)))
25 if not dictionary.check(t):
27 change[t] = dictionary.suggest(t)[0]
34 for frm, to in change.items():
35 query = query.replace(frm, to)
40 def category_name(category):
42 return filter(lambda c: c[0] == category, TAG_CATEGORIES)[0][1].encode('utf-8')
44 raise KeyError("No category %s" % category)
48 prefix = request.GET.get('term', '')
50 return JSONResponse(dumps({}))
51 JVM.attachCurrentThread()
53 tags = s.hint_tags(prefix)
54 books = s.hint_books(prefix)
58 'category': category_name(t.category),
60 'url': t.get_absolute_url()}
63 'category': category_name('book'),
65 'url': b.get_absolute_url()}
71 JVM.attachCurrentThread() # where to put this?
78 if 'q' in request.GET:
79 tags = request.GET.get('tags', '')
81 tag_list = Tag.get_tag_list(tags)
87 query = request.GET['q']
88 toks = StringReader(query)
89 fuzzy = 'fuzzy' in request.GET
94 results = SearchResult.aggregate(srch.search_perfect_book(toks, fuzzy=fuzzy),
95 srch.search_perfect_parts(toks, fuzzy=fuzzy),
96 srch.search_everywhere(toks, fuzzy=fuzzy))
97 results.sort(reverse=True)
102 return render_to_response('newsearch/search.html', {'results': results,
103 'did_you_mean': (query is not None) and
104 did_you_mean(query, srch.get_tokens(query, field='SIMPLE')),
106 context_instance=RequestContext(request))