fix: Use Payload in AutoCompleter (categories in search) and misc

- Separate Item group and Item autocomplete dict definition
- Add payload along with Item group, containing namke and route
- Pass weightage while defining item group autocomplete dict (auto sort)
- Use payload while getting results for categories in search
- Remove check to show categories, always show
- Search fields mandatory if reidsearch enabled
- Code separation (rough)
This commit is contained in:
marination
2022-04-01 18:47:01 +05:30
parent fd29722d36
commit 17f95b1f83
3 changed files with 41 additions and 25 deletions

View File

@@ -48,7 +48,6 @@
"redisearch_warning", "redisearch_warning",
"search_index_fields", "search_index_fields",
"is_redisearch_enabled", "is_redisearch_enabled",
"show_categories_in_search_autocomplete",
"is_redisearch_loaded", "is_redisearch_loaded",
"shop_by_category_section", "shop_by_category_section",
"slideshow", "slideshow",
@@ -294,6 +293,7 @@
"fieldname": "search_index_fields", "fieldname": "search_index_fields",
"fieldtype": "Small Text", "fieldtype": "Small Text",
"label": "Search Index Fields", "label": "Search Index Fields",
"mandatory_depends_on": "is_redisearch_enabled",
"read_only_depends_on": "eval:!doc.is_redisearch_loaded" "read_only_depends_on": "eval:!doc.is_redisearch_loaded"
}, },
{ {
@@ -302,14 +302,6 @@
"fieldtype": "Section Break", "fieldtype": "Section Break",
"label": "Item Search Settings" "label": "Item Search Settings"
}, },
{
"default": "1",
"depends_on": "is_redisearch_enabled",
"fieldname": "show_categories_in_search_autocomplete",
"fieldtype": "Check",
"label": "Show Categories in Search Autocomplete",
"read_only_depends_on": "eval:!doc.is_redisearch_loaded"
},
{ {
"default": "0", "default": "0",
"fieldname": "is_redisearch_loaded", "fieldname": "is_redisearch_loaded",
@@ -379,7 +371,7 @@
"index_web_pages_for_search": 1, "index_web_pages_for_search": 1,
"issingle": 1, "issingle": 1,
"links": [], "links": [],
"modified": "2022-03-31 16:01:46.308663", "modified": "2022-04-01 18:35:56.106756",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "E-commerce", "module": "E-commerce",
"name": "E Commerce Settings", "name": "E Commerce Settings",

View File

@@ -157,17 +157,14 @@ def delete_from_ac_dict(website_item_doc):
@if_redisearch_enabled @if_redisearch_enabled
def define_autocomplete_dictionary(): def define_autocomplete_dictionary():
"""Creates an autocomplete search dictionary for `name`. """
Also creats autocomplete dictionary for `categories` if Defines/Redefines an autocomplete search dictionary for Website Item Name.
checked in E Commerce Settings""" Also creats autocomplete dictionary for Published Item Groups.
"""
cache = frappe.cache() cache = frappe.cache()
name_ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=cache) item_ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=cache)
cat_ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=cache) item_group_ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=cache)
ac_categories = frappe.db.get_single_value(
"E Commerce Settings", "show_categories_in_search_autocomplete"
)
# Delete both autocomplete dicts # Delete both autocomplete dicts
try: try:
@@ -176,16 +173,39 @@ def define_autocomplete_dictionary():
except Exception: except Exception:
return False return False
create_items_autocomplete_dict(autocompleter=item_ac)
create_item_groups_autocomplete_dict(autocompleter=item_group_ac)
@if_redisearch_enabled
def create_items_autocomplete_dict(autocompleter):
"Add items as suggestions in Autocompleter."
items = frappe.get_all( items = frappe.get_all(
"Website Item", fields=["web_item_name", "item_group"], filters={"published": 1} "Website Item", fields=["web_item_name", "item_group"], filters={"published": 1}
) )
for item in items: for item in items:
name_ac.add_suggestions(Suggestion(item.web_item_name)) autocompleter.add_suggestions(Suggestion(item.web_item_name))
if ac_categories and item.item_group:
cat_ac.add_suggestions(Suggestion(item.item_group))
return True
@if_redisearch_enabled
def create_item_groups_autocomplete_dict(autocompleter):
"Add item groups with weightage as suggestions in Autocompleter."
published_item_groups = frappe.get_all(
"Item Group", fields=["name", "route", "weightage"], filters={"show_in_website": 1}
)
if not published_item_groups:
return
for item_group in published_item_groups:
payload = {"name": item_group, "route": item_group.route}
autocompleter.add_suggestions(
Suggestion(
string=item_group.name,
score=item_group.weightage,
payload=payload, # additional info that can be retrieved later
)
)
@if_redisearch_enabled @if_redisearch_enabled

View File

@@ -1,6 +1,8 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt # License: GNU General Public License v3. See license.txt
import json
import frappe import frappe
from frappe.utils import cint, cstr from frappe.utils import cint, cstr
from redisearch import AutoCompleter, Client, Query from redisearch import AutoCompleter, Client, Query
@@ -135,8 +137,10 @@ def get_category_suggestions(query):
return search_results return search_results
ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=frappe.cache()) ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=frappe.cache())
suggestions = ac.get_suggestions(query, num=10) suggestions = ac.get_suggestions(query, num=10, with_payloads=True)
search_results["results"] = [s.string for s in suggestions] results = [json.loads(s.payload) for s in suggestions]
search_results["results"] = results
return search_results return search_results