From 50e6c6d5aa0e6b48c638b4f3e50245ffe81e9814 Mon Sep 17 00:00:00 2001 From: MartinMikita Date: Thu, 26 May 2016 11:31:51 +0200 Subject: [PATCH] Add sortBy argument: column-asc/desc, possible to use more sortBy arguments (#8) --- web/websearch.py | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/web/websearch.py b/web/websearch.py index 5d89e5f..3317ac2 100755 --- a/web/websearch.py +++ b/web/websearch.py @@ -92,6 +92,23 @@ def process_query(index, query, query_filter, start=0, count=0): continue cl.SetFilterString(f, query_filter[f]) + # Prepare sorting by custom or default + if query_filter['sortBy'] is not None: + v = query_filter['sortBy'] + if not isinstance(v, list): + v = [v] + sorting = [] + for attr in v: + # column-asc/desc + attr = attr.split('-') + asc = 'ASC' + if len(attr) > 1 and (attr[1] == 'desc' or attr[1] == 'DESC'): + asc = 'DESC' + sorting.append('{} {}'.format(attr[0], asc)) + cl.SetSortMode(SPH_SORT_EXTENDED, ', '.join(sorting)) + else: + cl.SetSortMode(SPH_SORT_EXTENDED, '@relevance DESC') + # Prepare date filtering datestart = 0 dateend = 0 @@ -255,12 +272,23 @@ def search(): q = request.args.get('q').encode('utf-8') query_filter = {'type': None, 'lang': None, 'date': None, - 'tags': None, 'datestart': None, 'dateend': None} + 'tags': None, 'datestart': None, 'dateend': None, + 'sortBy': None} filter = False for f in query_filter: if request.args.get(f): - v = request.args.get(f) - query_filter[f] = v.encode('utf-8') + v = None + # Some arguments may be list + if f in ('type', 'lang', 'sortBy', 'tags'): + vl = request.args.getlist(f) + if len(vl) == 1: + v = vl[0].encode('utf-8') + elif len(vl) > 1: + v = [x.encode('utf-8') for x in vl] + if v is None: + vl = request.args.get(f) + v = vl.encode('utf-8') + query_filter[f] = v filter = True if not q and not filter: