From ca04063ce061fb80375e73d5aad865ebd6e0ae6d Mon Sep 17 00:00:00 2001 From: Sean Hammond Date: Wed, 3 Apr 2013 15:48:16 +0200 Subject: [PATCH] [#714] Fix default sort ordering 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. --- ckan/controllers/package.py | 2 +- ckan/lib/search/query.py | 5 ----- ckan/logic/action/get.py | 11 ++++++++--- ckan/templates/snippets/sort_by.html | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py index 00cc0b09a37..1126fc16240 100644 --- a/ckan/controllers/package.py +++ b/ckan/controllers/package.py @@ -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) @@ -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'], diff --git a/ckan/lib/search/query.py b/ckan/lib/search/query.py index e43825f8d1e..e56847b7437 100644 --- a/ckan/lib/search/query.py +++ b/ckan/lib/search/query.py @@ -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: diff --git a/ckan/logic/action/get.py b/ckan/logic/action/get.py index cdeda998d1e..557c65c4b86 100644 --- a/ckan/logic/action/get.py +++ b/ckan/logic/action/get.py @@ -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. @@ -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) @@ -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. diff --git a/ckan/templates/snippets/sort_by.html b/ckan/templates/snippets/sort_by.html index fcc58fc74aa..ebbd67461cc 100644 --- a/ckan/templates/snippets/sort_by.html +++ b/ckan/templates/snippets/sort_by.html @@ -11,7 +11,7 @@