diff --git a/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json b/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json index 76bf283e040..bfdfb0d17a1 100644 --- a/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json +++ b/erpnext/e_commerce/doctype/e_commerce_settings/e_commerce_settings.json @@ -46,7 +46,8 @@ "shop_by_category_section", "slideshow", "item_search_settings_section", - "search_index_fields" + "search_index_fields", + "show_categories_in_search_autocomplete" ], "fields": [ { @@ -313,12 +314,18 @@ "fieldname": "item_search_settings_section", "fieldtype": "Section Break", "label": "Item Search Settings" + }, + { + "default": "1", + "fieldname": "show_categories_in_search_autocomplete", + "fieldtype": "Check", + "label": "Show Categories in Search Autocomplete" } ], "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2021-04-23 13:30:50.286088", + "modified": "2021-04-26 09:50:40.581354", "modified_by": "Administrator", "module": "E-commerce", "name": "E Commerce Settings", diff --git a/erpnext/e_commerce/website_item_indexing.py b/erpnext/e_commerce/website_item_indexing.py index 0e48a2d7e68..faf5760a6aa 100644 --- a/erpnext/e_commerce/website_item_indexing.py +++ b/erpnext/e_commerce/website_item_indexing.py @@ -17,6 +17,7 @@ from redisearch import ( WEBSITE_ITEM_INDEX = 'website_items_index' WEBSITE_ITEM_KEY_PREFIX = 'website_item:' WEBSITE_ITEM_NAME_AUTOCOMPLETE = 'website_items_name_dict' +WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE = 'website_items_category_dict' ALLOWED_INDEXABLE_FIELDS_SET = { 'item_code', @@ -108,27 +109,36 @@ def delete_item_from_index(website_item_doc): return True def define_autocomplete_dictionary(): - print("Defining ac dict...") - # AC for name - # TODO: AC for category + """Creates an autocomplete search dictionary for `name`. + Also creats autocomplete dictionary for `categories` if + checked in E Commerce Settings""" r = redis.Redis("localhost", 13000) - ac = AutoCompleter(WEBSITE_ITEM_NAME_AUTOCOMPLETE, port=13000) + name_ac = AutoCompleter(WEBSITE_ITEM_NAME_AUTOCOMPLETE, port=13000) + cat_ac = AutoCompleter(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE, port=13000) + ac_categories = frappe.db.get_single_value( + 'E Commerce Settings', + 'show_categories_in_search_autocomplete' + ) + + # Delete both autocomplete dicts try: r.delete(WEBSITE_ITEM_NAME_AUTOCOMPLETE) + r.delete(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE) except: return False items = frappe.get_all( 'Website Item', - fields=['web_item_name'], + fields=['web_item_name', 'item_group'], filters={"published": True} ) for item in items: - print("adding suggestion: " + item.web_item_name) - ac.add_suggestions(Suggestion(item.web_item_name)) + name_ac.add_suggestions(Suggestion(item.web_item_name)) + if ac_categories and item.item_group: + cat_ac.add_suggestions(Suggestion(item.item_group)) return True diff --git a/erpnext/templates/pages/product_search.py b/erpnext/templates/pages/product_search.py index c04e5a3c519..8b7aeab066f 100644 --- a/erpnext/templates/pages/product_search.py +++ b/erpnext/templates/pages/product_search.py @@ -9,7 +9,11 @@ from erpnext.e_commerce.shopping_cart.product_info import set_product_info_for_w # For SEARCH ------- from redisearch import AutoCompleter, Client, Query -from erpnext.e_commerce.website_item_indexing import WEBSITE_ITEM_INDEX, WEBSITE_ITEM_NAME_AUTOCOMPLETE +from erpnext.e_commerce.website_item_indexing import ( + WEBSITE_ITEM_INDEX, + WEBSITE_ITEM_NAME_AUTOCOMPLETE, + WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE +) # ----------------- no_cache = 1 @@ -83,3 +87,14 @@ def search(query): def convert_to_dict(redis_search_doc): return redis_search_doc.__dict__ + +@frappe.whitelist(allow_guest=True) +def get_category_suggestions(query): + if not query: + # TODO: return top/recent searches + return [] + + ac = AutoCompleter(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE, port=13000) + suggestions = ac.get_suggestions(query, num=10) + + return [s.string for s in suggestions] \ No newline at end of file diff --git a/erpnext/www/all-products/search.html b/erpnext/www/all-products/search.html index ef74536286d..1c58e65cc76 100644 --- a/erpnext/www/all-products/search.html +++ b/erpnext/www/all-products/search.html @@ -12,7 +12,19 @@ {% block page_content %} -