From f7c5e610c7010c7cad03ac03751238d6f18841c2 Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Fri, 3 Jan 2014 18:03:20 -0500 Subject: [PATCH] Order search results properly, paginate them too --- karmaworld/apps/notes/search.py | 24 +++++++++--- karmaworld/apps/notes/views.py | 37 ++++++++++++++++--- karmaworld/assets/css/search.css | 8 ++++ .../templates/notes/search_results.html | 27 +++++++++++--- 4 files changed, 79 insertions(+), 17 deletions(-) diff --git a/karmaworld/apps/notes/search.py b/karmaworld/apps/notes/search.py index 3908180..48394d0 100644 --- a/karmaworld/apps/notes/search.py +++ b/karmaworld/apps/notes/search.py @@ -9,6 +9,8 @@ import karmaworld.secret.indexden as secret import logging +PAGE_SIZE = 10 + logging.basicConfig() logger = logging.getLogger(__name__) @@ -27,6 +29,13 @@ while not index.has_started(): # "Relevance" is a black box provided by IndexDen. index.add_function(0, 'relevance * log(doc.var[0])') +class SearchResult(object): + + def __init__(self, ordered_ids, snippet_dict, has_more): + self.ordered_ids = ordered_ids + self.snippet_dict = snippet_dict + self.has_more = has_more + def note_to_dict(note): d = { 'name': note.name, @@ -48,12 +57,12 @@ def add_document(note): if note.text: index.add_document(note.id, note_to_dict(note), variables={0: note.thanks}) else: - logger.warn("Note {n} has no text or thanks value, will not add to IndexDen".format(n=note)) + logger.warn("Note {n} has no text, will not add to IndexDen".format(n=note)) def remove_document(note): index.delete_document(note.id) -def search(query, course_id=None): +def search(query, course_id=None, page=0): """Returns note IDs matching the given query, filtered by course ID if given""" if course_id: @@ -61,8 +70,13 @@ def search(query, course_id=None): else: real_query = '"%s" OR name:"%s"' % (query, query) - raw_results = index.search(real_query, scoring_function=0, snippet_fields=['text']) + raw_results = index.search(real_query, snippet_fields=['text'], + length=PAGE_SIZE, start=(page*PAGE_SIZE)) + + ordered_ids = [int(r['docid']) for r in raw_results['results']] + snippet_dict = {int(r['docid']): r['snippet_text'] for r in raw_results['results']} - results = {r['docid']: r['snippet_text'] for r in raw_results['results']} + # Are there more results to show the user if they want? + has_more = True if int(raw_results['matches']) > ((page+1) * PAGE_SIZE) else False - return results + return SearchResult(ordered_ids, snippet_dict, has_more) diff --git a/karmaworld/apps/notes/views.py b/karmaworld/apps/notes/views.py index 647cb97..1928721 100644 --- a/karmaworld/apps/notes/views.py +++ b/karmaworld/apps/notes/views.py @@ -157,16 +157,25 @@ class NoteSearchView(ListView): if not 'query' in self.request.GET: return Note.objects.none() + if 'page' in self.request.GET: + page = int(self.request.GET['page']) + else: + page = 0 + if 'course_id' in self.request.GET: - matching_notes = search.search(self.request.GET['query'], - self.request.GET['course_id']) + raw_results = search.search(self.request.GET['query'], + self.request.GET['course_id'], + page=page) else: - matching_notes = search.search(self.request.GET['query']) + raw_results = search.search(self.request.GET['query'], + page=page) - instances = Note.objects.filter(id__in=matching_notes.viewkeys()) + instances = Note.objects.in_bulk(raw_results.ordered_ids) results = [] - for i in instances: - results.append((i, matching_notes[str(i.id)])) + for id in raw_results.ordered_ids: + if id in instances: + results.append((instances[id], raw_results.snippet_dict[id])) + self.has_more = raw_results.has_more return results @@ -177,6 +186,22 @@ class NoteSearchView(ListView): if 'course_id' in self.request.GET: kwargs['course'] = Course.objects.get(id=self.request.GET['course_id']) + # If query returned more search results than could + # fit on one page, show "Next" button + if self.has_more: + kwargs['has_next'] = True + if 'page' in self.request.GET: + kwargs['next_page'] = int(self.request.GET['page']) + 1 + else: + kwargs['next_page'] = 1 + + # If the user is looking at a search result page + # that isn't the first one, show "Prev" button + if 'page' in self.request.GET and \ + int(self.request.GET['page']) > 0: + kwargs['has_prev'] = True + kwargs['prev_page'] = int(self.request.GET['page']) - 1 + return super(NoteSearchView, self).get_context_data(**kwargs) diff --git a/karmaworld/assets/css/search.css b/karmaworld/assets/css/search.css index 821f110..d7a3755 100644 --- a/karmaworld/assets/css/search.css +++ b/karmaworld/assets/css/search.css @@ -10,4 +10,12 @@ font-size: 10px; color: #8e8e8e; margin-bottom: 20px; +} + +#search_nav_buttons +{ + font-family: "MuseoSlab-900"; + font-size: 25px; + color: #8e8e8e; + margin-bottom: 20px; } \ No newline at end of file diff --git a/karmaworld/templates/notes/search_results.html b/karmaworld/templates/notes/search_results.html index d1ded58..65e408b 100644 --- a/karmaworld/templates/notes/search_results.html +++ b/karmaworld/templates/notes/search_results.html @@ -14,13 +14,15 @@
-
- + {% endif %}
@@ -63,6 +65,19 @@
{% endif %}
+ +
+
+ {% if has_prev %} + +  Previous + {% endif %} + {% if has_next %} + + Next  + {% endif %} +
+
-- 2.25.1