perf: optimise globals search
This commit is contained in:
parent
86e585c1f4
commit
93b9ca86f7
2 changed files with 34 additions and 30 deletions
|
|
@ -29,12 +29,21 @@ class GlobalSearchSettings(Document):
|
|||
repeated_dts = (", ".join([frappe.bold(dt) for dt in repeated_dts]))
|
||||
frappe.throw(_("Document Type {0} has been repeated.").format(repeated_dts))
|
||||
|
||||
def on_update(self):
|
||||
get_doctypes_for_global_search()
|
||||
|
||||
def get_doctypes_for_global_search():
|
||||
doctypes = frappe.get_list("Global Search DocType", fields=["document_type"], order_by="idx ASC")
|
||||
if not doctypes:
|
||||
return []
|
||||
|
||||
return [d.document_type for d in doctypes]
|
||||
priorities = [d.document_type for d in doctypes]
|
||||
allowed_doctypes = ",".join(["'{0}'".format(dt) for dt in priorities])
|
||||
|
||||
frappe.cache().hset("global_search", "search_priorities", priorities)
|
||||
frappe.cache().hset("global_search", "allowed_doctypes", allowed_doctypes)
|
||||
|
||||
return priorities, allowed_doctypes
|
||||
|
||||
@frappe.whitelist()
|
||||
def reset_global_search_settings_doctypes():
|
||||
|
|
|
|||
|
|
@ -418,10 +418,19 @@ def search(text, start=0, limit=20, doctype=""):
|
|||
from frappe.desk.doctype.global_search_settings.global_search_settings import get_doctypes_for_global_search
|
||||
|
||||
results = []
|
||||
texts = [t.strip() for t in text.split('&') if t]
|
||||
priorities = get_doctypes_for_global_search()
|
||||
allowed_doctypes = ",".join(["'{0}'".format(dt) for dt in priorities])
|
||||
for text in texts:
|
||||
sorted_results = []
|
||||
|
||||
priorities = frappe.cache().hget("global_search", "search_priorities")
|
||||
allowed_doctypes = frappe.cache().hget("global_search", "allowed_doctypes")
|
||||
|
||||
if not priorities or not allowed_doctypes:
|
||||
priorities, allowed_doctypes = get_doctypes_for_global_search()
|
||||
|
||||
for text in set(text.split('&')):
|
||||
text = text.strip()
|
||||
if not text:
|
||||
continue
|
||||
|
||||
mariadb_conditions = ''
|
||||
postgres_conditions = ''
|
||||
offset = ''
|
||||
|
|
@ -455,36 +464,22 @@ def search(text, start=0, limit=20, doctype=""):
|
|||
'postgres': common_query.format(fields=postgres_fields, conditions=postgres_conditions, limit=limit, offset=offset)
|
||||
}, as_dict=True)
|
||||
|
||||
tmp_result=[]
|
||||
for i in result:
|
||||
if i.rank > 0.0:
|
||||
if i in results or not results:
|
||||
tmp_result.extend([i])
|
||||
results.extend(tmp_result)
|
||||
|
||||
for r in results:
|
||||
try:
|
||||
if frappe.get_meta(r.doctype).image_field:
|
||||
r.image = frappe.db.get_value(r.doctype, r.name, frappe.get_meta(r.doctype).image_field)
|
||||
except Exception:
|
||||
frappe.clear_messages()
|
||||
|
||||
sorted_results = []
|
||||
results.extend(result)
|
||||
|
||||
for priority in priorities:
|
||||
tmp_result = []
|
||||
if not results:
|
||||
break
|
||||
|
||||
for index, r in enumerate(results):
|
||||
if r.doctype == priority:
|
||||
tmp_result.extend([r])
|
||||
if r.doctype == priority and r.rank > 0.0:
|
||||
try:
|
||||
meta = frappe.get_meta(r.doctype)
|
||||
if meta.image_field:
|
||||
r.image = frappe.db.get_value(r.doctype, r.name, meta.image_field)
|
||||
except Exception:
|
||||
frappe.clear_messages()
|
||||
|
||||
sorted_results.extend([r])
|
||||
results.pop(index)
|
||||
|
||||
sorted_results.extend(tmp_result)
|
||||
|
||||
return sorted_results
|
||||
|
||||
return sorted_results or results
|
||||
|
||||
@frappe.whitelist(allow_guest=True)
|
||||
def web_search(text, scope=None, start=0, limit=20):
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue