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!
This commit is contained in:
Fisher Yu 2018-07-05 20:25:14 +08:00 committed by GitHub
parent 2fadff05b5
commit f0aed45c76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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