import logging
+PAGE_SIZE = 10
+
logging.basicConfig()
logger = logging.getLogger(__name__)
# "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,
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:
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)
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
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)
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
<div id="results_header" class="hero_gradient_bar">
- <div class="row">
- <div class="twelve columns header_subhead">
- <a href="{{ course.get_absolute_url}}">
- <i class="fa fa-arrow-left"></i> back to {{ course.name }}
- </a>
+ {% if course %}
+ <div class="row">
+ <div class="twelve columns header_subhead">
+ <a href="{{ course.get_absolute_url}}">
+ <i class="fa fa-arrow-left"></i> back to {{ course.name }}
+ </a>
+ </div>
</div>
- </div>
+ {% endif %}
<div class="row">
<div class="small-12 columns header_subhead">
</div>
{% endif %}
</div>
+
+ <div id="search_nav_buttons" class="row">
+ <div class="small-12 columns center">
+ {% if has_prev %}
+ <a href="{% url 'note_search' %}?query={{ query|urlencode }}&course_id={{ course.id }}&page={{ prev_page }}">
+ <i class="fa fa-caret-left"></i> Previous</a>
+ {% endif %}
+ {% if has_next %}
+ <a href="{% url 'note_search' %}?query={{ query|urlencode }}&course_id={{ course.id }}&page={{ next_page }}">
+ Next <i class="fa fa-caret-right"></i></a>
+ {% endif %}
+ </div>
+ </div>
</div><!-- /course_container -->
</section>