mirror of
https://github.com/djohnlewis/stackdump
synced 2024-12-04 23:17:37 +00:00
Implemented sorting on the search page.
This commit is contained in:
parent
ea2170edb9
commit
6511759213
@ -223,12 +223,28 @@ def search():
|
||||
redirect(settings.APP_URL_ROOT)
|
||||
|
||||
page = int(request.GET.get('p', 0))
|
||||
# page needs to be zero-based for pysolr
|
||||
page = (page > 0) and (page - 1) or page
|
||||
|
||||
rows_per_page = int(request.GET.get('r', 10))
|
||||
rows_per_page = (rows_per_page > 0) and rows_per_page or 10
|
||||
|
||||
sort_args = {
|
||||
'newest' : 'creationDate desc',
|
||||
'votes' : 'votes desc',
|
||||
'relevance' : 'score desc' # score is the special keyword for the
|
||||
# relevancy score in Lucene
|
||||
}
|
||||
sort_by = request.GET.get('s', 'relevance').lower()
|
||||
# default to sorting by relevance
|
||||
if sort_by not in sort_args.keys():
|
||||
sort_by = 'relevance'
|
||||
|
||||
# perform search
|
||||
results = solr_conn().search(query, start=page*rows_per_page, rows=rows_per_page)
|
||||
results = solr_conn().search(query,
|
||||
start=page*rows_per_page,
|
||||
rows=rows_per_page,
|
||||
sort=sort_args[sort_by])
|
||||
decode_json_fields(results)
|
||||
|
||||
context = { }
|
||||
@ -242,6 +258,7 @@ def search():
|
||||
context['current_page'] = page + 1 # page should be ones-based
|
||||
context['rows_per_page'] = rows_per_page
|
||||
context['total_pages'] = int(math.ceil(float(results.hits) / rows_per_page))
|
||||
context['sort_by'] = sort_by
|
||||
|
||||
return render_template('results.html', context)
|
||||
|
||||
|
@ -7,17 +7,17 @@
|
||||
<div class="span16">
|
||||
<form id="search" method="get" action="{{ SETTINGS.APP_URL_ROOT }}search">
|
||||
<input type="text" class="xlarge" name="q" value="{{ query }}" />
|
||||
<input type="hidden" name="s" value="{{ sort_by }}" />
|
||||
<input type="submit" class="btn primary" value="Search" />
|
||||
</form>
|
||||
|
||||
<ul class="tabs">
|
||||
<li class="active"><a href="#">newest</a></li>
|
||||
<li><a href="#">votes</a></li>
|
||||
<li><a href="#">relevance</a></li>
|
||||
<li {% if sort_by == 'newest' %}class="active"{% endif %}><a href="{{ REQUEST.url|set_get_parameters('s=newest') }}">newest</a></li>
|
||||
<li {% if sort_by == 'votes' %}class="active"{% endif %}><a href="{{ REQUEST.url|set_get_parameters('s=votes') }}">votes</a></li>
|
||||
<li {% if sort_by == 'relevance' %}class="active"{% endif %}><a href="{{ REQUEST.url|set_get_parameters('s=relevance') }}">relevance</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="span12">
|
||||
<ul id="search-results">
|
||||
|
Loading…
Reference in New Issue
Block a user