Skip to content

Commit

Permalink
[#714] Fix default sort ordering
Browse files Browse the repository at this point in the history
Change the default sort order of package_search() to 'relevance asc,
metadata_modified desc'. We want to sort by relevance by default, but
when there's no search query relevance is meaningless, in that case fall
back on showing the most recently modified datasets first. Also changes
the sort ordering of the "Relevance" option in the "Order by:" dropdown
to 'relevance asc, metadata_modified desc' instead of just 'relevance
asc'.

The previous default ordering was 'score desc, name asc'. I'm not even
sure if that works, it seems to disagree with the sort strings that the
dropdown gives you, when you chose relevance from the dropdown you got
'relevance asc' not 'score desc' (and the datasets appeared in a
different order then the default), and when you chose name you get
'title_string' not name.

Previously we've fallen back on showing datasets alphabetically but
that's boring as it simply means that all the datasets beginning with 'a'
are always shown. Last modified seems more interesting and changes over
time. Popularity is not an option because that only works if the page
view tracking feature is enabled.

Move the logic that selects the default sort order for package_search()
out of lib and into package_search().

The package_search() action function now returns the sort order it used
in the 'sort' key of the returned dict, and the package controller sends
this to the templates to decide which sort ordering to show selected in
the "Order by:" dropdown. Previously the package controller and action
function each had their own logic and the dropdown was out of sync with
the actual sort order.

Fixes #714.
  • Loading branch information
Sean Hammond committed Apr 3, 2013
1 parent 52005cb commit ca04063
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ckan/controllers/package.py
Expand Up @@ -174,7 +174,6 @@ def _sort_by(fields):
else:
c.sort_by_fields = [field.split()[0]
for field in sort_by.split(',')]
c.sort_by_selected = sort_by

def pager_url(q=None, page=None):
params = list(params_nopage)
Expand Down Expand Up @@ -245,6 +244,7 @@ def pager_url(q=None, page=None):
}

query = get_action('package_search')(context, data_dict)
c.sort_by_selected = query['sort']

c.page = h.Page(
collection=query['results'],
Expand Down
5 changes: 0 additions & 5 deletions ckan/lib/search/query.py
Expand Up @@ -319,11 +319,6 @@ def run(self, query):
rows_to_query = rows_to_return
query['rows'] = rows_to_query

# order by score if no 'sort' term given
order_by = query.get('sort')
if order_by == 'rank' or order_by is None:
query['sort'] = 'score desc, name asc'

# show only results from this CKAN instance
fq = query.get('fq', '')
if not '+site_id:' in fq:
Expand Down
11 changes: 8 additions & 3 deletions ckan/logic/action/get.py
Expand Up @@ -1161,8 +1161,9 @@ def package_search(context, data_dict):
:param rows: the number of matching rows to return.
:type rows: int
:param sort: sorting of the search results. Optional. Default:
"score desc, name asc". As per the solr documentation, this is a
comma-separated string of field names and sort-orderings.
'relevance asc, metadata_modified desc'. As per the solr
documentation, this is a comma-separated string of field names and
sort-orderings.
:type sort: string
:param start: the offset in the complete result for where the set of
returned datasets should begin.
Expand Down Expand Up @@ -1258,6 +1259,9 @@ def package_search(context, data_dict):
if not 'capacity:' in p)
data_dict['fq'] = fq + ' capacity:"public"'

if data_dict.get('sort') in (None, 'rank'):
data_dict['sort'] = 'relevance asc, metadata_modified desc'

query = search.query_for(model.Package)
query.run(data_dict)

Expand Down Expand Up @@ -1298,7 +1302,8 @@ def package_search(context, data_dict):
search_results = {
'count': count,
'facets': facets,
'results': results
'results': results,
'sort': data_dict['sort']
}

# Transform facets into a more useful data structure.
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/snippets/sort_by.html
Expand Up @@ -11,7 +11,7 @@
<span class="form-select control-group control-order-by">
<label for="field-order-by">{{ _('Order by') }}</label>
<select id="field-order-by" name="sort">
<option value="relevance asc"{% if sort =='relevance asc' %} selected="selected"{% endif %}>{{ _('Relevance') }}</option>
<option value="relevance asc, metadata_modified desc"{% if sort =='relevance asc, metadata_modified desc' %} selected="selected"{% endif %}>{{ _('Relevance') }}</option>
<option value="title_string asc"{% if sort=='title_string asc' %} selected="selected"{% endif %}>{{ _('Name Ascending') }}</option>
<option value="title_string desc"{% if sort=='title_string desc' %} selected="selected"{% endif %}>{{ _('Name Descending') }}</option>
<option value="metadata_modified desc"{% if sort=='metadata_modified desc' %} selected="selected"{% endif %}>{{ _('Last Modified') }}</option>
Expand Down

0 comments on commit ca04063

Please sign in to comment.