1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.conf import settings
6 from django.shortcuts import render_to_response
7 from django.template import RequestContext
8 from django.views.decorators import cache
9 from django.http import HttpResponse, JsonResponse
10 from django.utils.translation import ugettext as _
12 from catalogue.utils import split_tags
13 from catalogue.models import Book
14 from pdcounter.models import Author as PDCounterAuthor, BookStub as PDCounterBook
15 from search.index import Search, SearchResult
16 from suggest.forms import PublishingSuggestForm
21 def match_word_re(word):
22 if 'sqlite' in settings.DATABASES['default']['ENGINE']:
23 return r"\b%s\b" % word
24 elif 'mysql' in settings.DATABASES['default']['ENGINE']:
25 return "[[:<:]]%s[[:>:]]" % word
28 query_syntax_chars = re.compile(r"[\\/*:(){}]")
31 def remove_query_syntax_chars(query, replace=' '):
32 return query_syntax_chars.sub(' ', query)
35 def did_you_mean(query, tokens):
39 # authors = Tag.objects.filter(category='author', name__iregex=match_word_re(t))
40 # if len(authors) > 0:
44 # if not dictionary.check(t):
46 # change_to = dictionary.suggest(t)[0].lower()
47 # if change_to != t.lower():
48 # change[t] = change_to
55 # for frm, to in change.items():
56 # query = query.replace(frm, to)
63 prefix = request.GET.get('term', '')
65 return JsonResponse([], safe=False)
67 prefix = remove_query_syntax_chars(prefix)
70 # tagi beda ograniczac tutaj
71 # ale tagi moga byc na ksiazce i na fragmentach
72 # jezeli tagi dot tylko ksiazki, to wazne zeby te nowe byly w tej samej ksiazce
73 # jesli zas dotycza themes, to wazne, zeby byly w tym samym fragmencie.
76 if isinstance(tag, PDCounterAuthor):
77 if filter(lambda t: t.slug == tag.slug and t != tag, tags):
79 elif isinstance(tag, PDCounterBook):
80 if filter(lambda b: b.slug == tag.slug, tags):
85 if c.startswith('pd_'):
90 limit = int(request.GET.get('max', ''))
99 tags = search.hint_tags(prefix, pdcounter=True)
100 tags = filter(lambda t: not is_dupe(t), tags)
107 'category': category_name(t.category),
109 'url': t.get_absolute_url()
112 books = search.hint_books(prefix)
119 'category': _('book'),
121 'url': b.get_absolute_url()
124 callback = request.GET.get('callback', None)
126 return HttpResponse("%s(%s);" % (callback, json.dumps(data)),
127 content_type="application/json; charset=utf-8")
129 return JsonResponse(data, safe=False)
134 query = request.GET.get('q', '')
137 return render_to_response(
138 'catalogue/search_too_short.html', {'prefix': query},
139 context_instance=RequestContext(request))
140 elif len(query) > 256:
141 return render_to_response(
142 'catalogue/search_too_long.html', {'prefix': query}, context_instance=RequestContext(request))
144 query = remove_query_syntax_chars(query)
148 theme_terms = search.index.analyze(text=query, field="themes_pl") \
149 + search.index.analyze(text=query, field="themes")
152 tags = search.hint_tags(query, pdcounter=True, prefix=False)
153 tags = split_tags(tags)
155 author_results = search.search_phrase(query, 'authors', book=True)
156 translator_results = search.search_phrase(query, 'translators', book=True)
158 title_results = search.search_phrase(query, 'title', book=True)
160 # Boost main author/title results with mixed search, and save some of its results for end of list.
161 # boost author, title results
162 author_title_mixed = search.search_some(query, ['authors', 'translators', 'title', 'tags'], query_terms=theme_terms)
163 author_title_rest = []
165 for b in author_title_mixed:
166 also_in_mixed = filter(lambda ba: ba.book_id == b.book_id, author_results + translator_results + title_results)
167 for b2 in also_in_mixed:
169 if also_in_mixed is []:
170 author_title_rest.append(b)
172 # Do a phrase search but a term search as well - this can give us better snippets then search_everywhere,
173 # Because the query is using only one field.
174 text_phrase = SearchResult.aggregate(
175 search.search_phrase(query, 'text', snippets=True, book=False),
176 search.search_some(query, ['text'], snippets=True, book=False, query_terms=theme_terms))
178 everywhere = search.search_everywhere(query, query_terms=theme_terms)
180 def already_found(results):
183 if e.book_id == r.book_id:
189 f = already_found(author_results + translator_results + title_results + text_phrase)
190 everywhere = filter(lambda x: not f(x), everywhere)
192 author_results = SearchResult.aggregate(author_results)
193 translator_results = SearchResult.aggregate(translator_results)
194 title_results = SearchResult.aggregate(title_results)
196 everywhere = SearchResult.aggregate(everywhere, author_title_rest)
198 for field, res in [('authors', author_results),
199 ('translators', translator_results),
200 ('title', title_results),
201 ('text', text_phrase),
202 ('text', everywhere)]:
203 res.sort(reverse=True)
205 search.get_snippets(r, query, field, 3)
209 def ensure_exists(r):
212 except Book.DoesNotExist:
215 author_results = filter(ensure_exists, author_results)
216 translator_results = filter(ensure_exists, translator_results)
217 title_results = filter(ensure_exists, title_results)
218 text_phrase = filter(ensure_exists, text_phrase)
219 everywhere = filter(ensure_exists, everywhere)
221 results = author_results + translator_results + title_results + text_phrase + everywhere
222 # ensure books do exists & sort them
223 for res in (author_results, translator_results, title_results, text_phrase, everywhere):
224 res.sort(reverse=True)
226 # We don't want to redirect to book text, but rather display result page even with one result.
227 # if len(results) == 1:
228 # fragment_hits = filter(lambda h: 'fragment' in h, results[0].hits)
229 # if len(fragment_hits) == 1:
230 # #anchor = fragment_hits[0]['fragment']
231 # #frag = Fragment.objects.get(anchor=anchor)
232 # return HttpResponseRedirect(fragment_hits[0]['fragment'].get_absolute_url())
233 # return HttpResponseRedirect(results[0].book.get_absolute_url())
234 if len(results) == 0:
235 form = PublishingSuggestForm(initial={"books": query + ", "})
236 return render_to_response(
237 'catalogue/search_no_hits.html',
242 'did_you_mean': suggestion
244 context_instance=RequestContext(request))
246 return render_to_response(
247 'catalogue/search_multiple_hits.html',
252 'author': author_results,
253 'translator': translator_results,
254 'title': title_results,
255 'content': text_phrase,
258 'did_you_mean': suggestion
260 context_instance=RequestContext(request))