2021-04-26 01:31:06 +00:00
|
|
|
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
|
2014-04-18 10:45:31 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2022-04-01 13:17:01 +00:00
|
|
|
import json
|
|
|
|
|
2014-04-18 10:45:31 +00:00
|
|
|
import frappe
|
2022-01-31 19:09:14 +00:00
|
|
|
from frappe.utils import cint, cstr
|
2021-04-26 01:31:06 +00:00
|
|
|
from redisearch import AutoCompleter, Client, Query
|
2022-01-31 19:09:14 +00:00
|
|
|
|
2021-12-10 12:43:22 +00:00
|
|
|
from erpnext.e_commerce.redisearch_utils import (
|
2022-01-31 19:09:14 +00:00
|
|
|
WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE,
|
2021-11-16 10:36:07 +00:00
|
|
|
WEBSITE_ITEM_INDEX,
|
2021-04-26 05:45:20 +00:00
|
|
|
WEBSITE_ITEM_NAME_AUTOCOMPLETE,
|
2022-03-31 10:59:18 +00:00
|
|
|
is_redisearch_enabled,
|
2022-01-31 19:09:14 +00:00
|
|
|
make_key,
|
2021-04-26 05:45:20 +00:00
|
|
|
)
|
2022-01-31 19:09:14 +00:00
|
|
|
from erpnext.e_commerce.shopping_cart.product_info import set_product_info_for_website
|
|
|
|
from erpnext.setup.doctype.item_group.item_group import get_item_for_list_in_html
|
2021-04-21 08:22:23 +00:00
|
|
|
|
2014-04-18 10:45:31 +00:00
|
|
|
no_cache = 1
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-06-27 09:24:46 +00:00
|
|
|
def get_context(context):
|
|
|
|
context.show_search = True
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-04-18 10:45:31 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
2015-12-16 07:27:02 +00:00
|
|
|
def get_product_list(search=None, start=0, limit=12):
|
2021-05-28 00:55:49 +00:00
|
|
|
data = get_product_data(search, start, limit)
|
2015-12-16 07:27:02 +00:00
|
|
|
|
2021-05-28 00:55:49 +00:00
|
|
|
for item in data:
|
|
|
|
set_product_info_for_website(item)
|
|
|
|
|
|
|
|
return [get_item_for_list_in_html(r) for r in data]
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-05-28 00:55:49 +00:00
|
|
|
def get_product_data(search=None, start=0, limit=12):
|
|
|
|
# limit = 12 because we show 12 items in the grid view
|
2014-04-18 10:45:31 +00:00
|
|
|
# base query
|
2021-06-01 07:14:49 +00:00
|
|
|
query = """
|
2021-08-12 13:31:10 +00:00
|
|
|
SELECT
|
2021-06-01 07:14:49 +00:00
|
|
|
web_item_name, item_name, item_code, brand, route,
|
|
|
|
website_image, thumbnail, item_group,
|
|
|
|
description, web_long_description as website_description,
|
|
|
|
website_warehouse, ranking
|
2021-08-12 13:31:10 +00:00
|
|
|
FROM `tabWebsite Item`
|
|
|
|
WHERE published = 1
|
2021-06-01 07:14:49 +00:00
|
|
|
"""
|
2014-04-18 10:45:31 +00:00
|
|
|
|
|
|
|
# search term condition
|
|
|
|
if search:
|
2021-06-01 07:14:49 +00:00
|
|
|
query += """ and (item_name like %(search)s
|
|
|
|
or web_item_name like %(search)s
|
|
|
|
or brand like %(search)s
|
|
|
|
or web_long_description like %(search)s)"""
|
2014-04-18 10:45:31 +00:00
|
|
|
search = "%" + cstr(search) + "%"
|
|
|
|
|
|
|
|
# order by
|
2021-09-02 08:37:59 +00:00
|
|
|
query += """ ORDER BY ranking desc, modified desc limit %s, %s""" % (cint(start), cint(limit))
|
2014-04-18 10:45:31 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
return frappe.db.sql(query, {"search": search}, as_dict=1) # nosemgrep
|
|
|
|
|
2014-04-18 10:45:31 +00:00
|
|
|
|
2021-04-21 08:22:23 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
2021-09-01 09:27:50 +00:00
|
|
|
def search(query):
|
|
|
|
product_results = product_search(query)
|
|
|
|
category_results = get_category_suggestions(query)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"product_results": product_results.get("results") or [],
|
2022-03-28 13:22:46 +00:00
|
|
|
"category_results": category_results.get("results") or [],
|
2021-09-01 09:27:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-09-01 09:27:50 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def product_search(query, limit=10, fuzzy_search=True):
|
2021-05-28 00:55:49 +00:00
|
|
|
search_results = {"from_redisearch": True, "results": []}
|
|
|
|
|
2022-03-31 10:59:18 +00:00
|
|
|
if not is_redisearch_enabled():
|
|
|
|
# Redisearch module not enabled
|
2021-05-28 00:55:49 +00:00
|
|
|
search_results["from_redisearch"] = False
|
|
|
|
search_results["results"] = get_product_data(query, 0, limit)
|
|
|
|
return search_results
|
|
|
|
|
2021-04-22 08:51:29 +00:00
|
|
|
if not query:
|
2021-05-28 00:55:49 +00:00
|
|
|
return search_results
|
2021-04-22 08:51:29 +00:00
|
|
|
|
2021-04-29 15:17:32 +00:00
|
|
|
red = frappe.cache()
|
2021-05-03 00:17:38 +00:00
|
|
|
query = clean_up_query(query)
|
|
|
|
|
2022-04-04 07:54:56 +00:00
|
|
|
# TODO: Check perf/correctness with Suggestions & Query vs only Query
|
|
|
|
# TODO: Use Levenshtein Distance in Query (max=3)
|
2021-04-29 15:17:32 +00:00
|
|
|
ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=red)
|
|
|
|
client = Client(make_key(WEBSITE_ITEM_INDEX), conn=red)
|
2021-05-11 07:30:41 +00:00
|
|
|
suggestions = ac.get_suggestions(
|
2022-03-28 13:22:46 +00:00
|
|
|
query, num=limit, fuzzy=fuzzy_search and len(query) > 3 # Fuzzy on length < 3 can be real slow
|
2021-05-11 07:30:41 +00:00
|
|
|
)
|
2021-04-22 08:51:29 +00:00
|
|
|
|
|
|
|
# Build a query
|
|
|
|
query_string = query
|
|
|
|
|
|
|
|
for s in suggestions:
|
2021-05-03 00:17:38 +00:00
|
|
|
query_string += f"|('{clean_up_query(s.string)}')"
|
2021-04-22 08:51:29 +00:00
|
|
|
|
|
|
|
q = Query(query_string)
|
|
|
|
|
|
|
|
results = client.search(q)
|
2022-03-28 13:22:46 +00:00
|
|
|
search_results["results"] = list(map(convert_to_dict, results.docs))
|
|
|
|
search_results["results"] = sorted(
|
|
|
|
search_results["results"], key=lambda k: frappe.utils.cint(k["ranking"]), reverse=True
|
|
|
|
)
|
2021-04-22 08:51:29 +00:00
|
|
|
|
2021-05-28 00:55:49 +00:00
|
|
|
return search_results
|
2021-04-22 08:51:29 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-05-03 00:17:38 +00:00
|
|
|
def clean_up_query(query):
|
2022-03-28 13:22:46 +00:00
|
|
|
return "".join(c for c in query if c.isalnum() or c.isspace())
|
|
|
|
|
2021-05-03 00:17:38 +00:00
|
|
|
|
2021-04-22 08:51:29 +00:00
|
|
|
def convert_to_dict(redis_search_doc):
|
|
|
|
return redis_search_doc.__dict__
|
2021-04-26 05:45:20 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-04-26 05:45:20 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def get_category_suggestions(query):
|
2021-06-01 07:14:49 +00:00
|
|
|
search_results = {"results": []}
|
2021-05-28 00:55:49 +00:00
|
|
|
|
2022-03-31 10:59:18 +00:00
|
|
|
if not is_redisearch_enabled():
|
|
|
|
# Redisearch module not enabled, query db
|
2021-06-01 07:14:49 +00:00
|
|
|
categories = frappe.db.get_all(
|
|
|
|
"Item Group",
|
2022-03-28 13:22:46 +00:00
|
|
|
filters={"name": ["like", "%{0}%".format(query)], "show_in_website": 1},
|
|
|
|
fields=["name", "route"],
|
2021-06-01 07:14:49 +00:00
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
search_results["results"] = categories
|
2021-05-28 00:55:49 +00:00
|
|
|
return search_results
|
|
|
|
|
2021-04-26 05:45:20 +00:00
|
|
|
if not query:
|
2021-05-28 00:55:49 +00:00
|
|
|
return search_results
|
2021-04-26 05:45:20 +00:00
|
|
|
|
2021-04-29 15:17:32 +00:00
|
|
|
ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=frappe.cache())
|
2022-04-01 13:17:01 +00:00
|
|
|
suggestions = ac.get_suggestions(query, num=10, with_payloads=True)
|
|
|
|
|
|
|
|
results = [json.loads(s.payload) for s in suggestions]
|
2021-04-26 05:45:20 +00:00
|
|
|
|
2022-04-01 13:17:01 +00:00
|
|
|
search_results["results"] = results
|
2021-05-28 00:55:49 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
return search_results
|