refactor(search): Improvements in search_widget, search_link APIs
* Minor perf enhancements * Renamed sorting_comparator to relevance_sorter
This commit is contained in:
parent
ecfa8c843f
commit
ba062adca8
1 changed files with 9 additions and 11 deletions
|
|
@ -169,19 +169,17 @@ def search_widget(doctype, txt, query=None, searchfield=None, start=0,
|
|||
|
||||
if doctype in UNTRANSLATED_DOCTYPES:
|
||||
# Filtering the values array so that query is included in very element
|
||||
values = tuple(
|
||||
[
|
||||
v for v in list(values)
|
||||
if re.search(
|
||||
re.escape(txt) + ".*", (_(v.name) if as_dict else _(v[0])), re.IGNORECASE
|
||||
)
|
||||
]
|
||||
values = (
|
||||
v for v in values
|
||||
if re.search(
|
||||
f"{re.escape(txt)}.*", _(v.name if as_dict else v[0]), re.IGNORECASE
|
||||
)
|
||||
)
|
||||
|
||||
# Sorting the values array so that relevant results always come first
|
||||
# This will first bring elements on top in which query is a prefix of element
|
||||
# Then it will bring the rest of the elements and sort them in lexicographical order
|
||||
values = sorted(values, key=lambda x: sorting_comparator(x, txt, as_dict))
|
||||
values = sorted(values, key=lambda x: relevance_sorter(x, txt, as_dict))
|
||||
|
||||
# remove _relevance from results
|
||||
if as_dict:
|
||||
|
|
@ -221,10 +219,10 @@ def scrub_custom_query(query, key, txt):
|
|||
query = query.replace('%s', ((txt or '') + '%'))
|
||||
return query
|
||||
|
||||
def sorting_comparator(key, query, as_dict):
|
||||
value = (_(key.name) if as_dict else _(key[0]))
|
||||
def relevance_sorter(key, query, as_dict):
|
||||
value = _(key.name if as_dict else key[0])
|
||||
return (
|
||||
value.lower().startswith(query.lower()) is False,
|
||||
value.lower().startswith(query.lower()) == False,
|
||||
value
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue