From f0aed45c76bda96a56a7df1872e41bf53733fb1a Mon Sep 17 00:00:00 2001 From: Fisher Yu Date: Thu, 5 Jul 2018 20:25:14 +0800 Subject: [PATCH] Update global_search.py support the user to use and operator (&) in the search text, to search target document by combined multi search term(key words separated by &, thus we can search purchase order by vendor name, the items purchased all together! --- frappe/utils/global_search.py | 81 +++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 32 deletions(-) diff --git a/frappe/utils/global_search.py b/frappe/utils/global_search.py index 5811d78057..d76c9a8a7b 100644 --- a/frappe/utils/global_search.py +++ b/frappe/utils/global_search.py @@ -342,27 +342,34 @@ def search(text, start=0, limit=20, doctype=""): :param limit: number of results to return, default 20 :return: Array of result objects """ - - text = "+" + text + "*" - if not doctype: - results = frappe.db.sql(''' - select - doctype, name, content - from - __global_search - where - match(content) against (%s IN BOOLEAN MODE) - limit {start}, {limit}'''.format(start=start, limit=limit), text+"*", as_dict=True) - else: - results = frappe.db.sql(''' - select - doctype, name, content - from - __global_search - where - doctype = %s AND - match(content) against (%s IN BOOLEAN MODE) - limit {start}, {limit}'''.format(start=start, limit=limit), (doctype, text), as_dict=True) + results = [] + texts = text.split('&') + for text in texts: + text = "+" + text + "*" + if not doctype: + result = frappe.db.sql(''' + select + doctype, name, content + from + __global_search + where + match(content) against (%s IN BOOLEAN MODE) + limit {start}, {limit}'''.format(start=start, limit=limit), text+"*", as_dict=True) + else: + result = frappe.db.sql(''' + select + doctype, name, content + from + __global_search + where + doctype = %s AND + match(content) against (%s IN BOOLEAN MODE) + limit {start}, {limit}'''.format(start=start, limit=limit), (doctype, text), as_dict=True) + tmp_result=[] + for i in result: + if i in results or not results: + tmp_result.append(i) + results = tmp_result for r in results: try: @@ -384,15 +391,25 @@ def web_search(text, start=0, limit=20): :return: Array of result objects """ - text = "+" + text + "*" - results = frappe.db.sql(''' - select - doctype, name, content, title, route - from - __global_search - where - published = 1 and - match(content) against (%s IN BOOLEAN MODE) - limit {start}, {limit}'''.format(start=start, limit=limit), - text, as_dict=True) + results = [] + texts = text.split('&') + for text in texts: + text = "+" + text + "*" + result = frappe.db.sql(''' + select + doctype, name, content, title, route + from + __global_search + where + published = 1 and + match(content) against (%s IN BOOLEAN MODE) + limit {start}, {limit}'''.format(start=start, limit=limit), + text, as_dict=True) + + tmp_result=[] + for i in result: + if i in results or not results: + tmp_result.append(i) + results = tmp_result + return results