From a7a1e5e964d98076f0a7e71d9c931655ba1a7b03 Mon Sep 17 00:00:00 2001 From: MartinMikita Date: Fri, 22 Jul 2016 12:04:50 +0200 Subject: [PATCH] Improved and fixed boost exact hit with autocomplete (#16). --- web/websearch.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/web/websearch.py b/web/websearch.py index 1e2c2bd..fd05de5 100755 --- a/web/websearch.py +++ b/web/websearch.py @@ -207,7 +207,16 @@ def process_query_mysql(index, query, query_filter, start=0, count=0): option = "field_weights = (name = 100, display_name = 1)" option += ", retry_count = 2, retry_delay = 500, max_matches = 200, max_query_time = 10000" option += ", ranker=expr('sum((10*lcs+5*exact_order+5*exact_hit+5*wlccs)*user_weight)*1000+bm25')" - sql = "SELECT WEIGHT()*importance+IF(name=%s,1000000,0) as weight, * FROM {} WHERE {} ORDER BY {} LIMIT %s, %s OPTION {};".format( + # Prepare query for boost + query_elements = re.compile("\s*,\s*|\s+").split(query) + select_boost = [] + argsBoost = [] + for qe in query_elements: + qe = re.sub(r"\**", "", qe) + select_boost.append('IF(name=%s,1000000,0)') + argsBoost.append(qe) + sql = "SELECT WEIGHT()*importance+{} as weight, * FROM {} WHERE {} ORDER BY {} LIMIT %s, %s OPTION {};".format( + '+'.join(select_boost), index, ' AND '.join(whereFilter), ', '.join(sortBy), @@ -222,7 +231,7 @@ def process_query_mysql(index, query, query_filter, start=0, count=0): } try: - args = [query] + argsFilter + [start, count] + args = argsBoost + argsFilter + [start, count] q = cursor.execute(sql, args) pprint([sql, args, cursor._last_executed, q]) desc = cursor.description