feat: Add fallback when redisearch not loaded
This commit is contained in:
parent
53bb7a9cd1
commit
41a5ebc6c0
@ -10,6 +10,7 @@ from erpnext.e_commerce.shopping_cart.product_info import set_product_info_for_w
|
|||||||
# For SEARCH -------
|
# For SEARCH -------
|
||||||
from redisearch import AutoCompleter, Client, Query
|
from redisearch import AutoCompleter, Client, Query
|
||||||
from erpnext.e_commerce.website_item_indexing import (
|
from erpnext.e_commerce.website_item_indexing import (
|
||||||
|
is_search_module_loaded,
|
||||||
WEBSITE_ITEM_INDEX,
|
WEBSITE_ITEM_INDEX,
|
||||||
WEBSITE_ITEM_NAME_AUTOCOMPLETE,
|
WEBSITE_ITEM_NAME_AUTOCOMPLETE,
|
||||||
WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE,
|
WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE,
|
||||||
@ -25,8 +26,15 @@ def get_context(context):
|
|||||||
|
|
||||||
@frappe.whitelist(allow_guest=True)
|
@frappe.whitelist(allow_guest=True)
|
||||||
def get_product_list(search=None, start=0, limit=12):
|
def get_product_list(search=None, start=0, limit=12):
|
||||||
# limit = 12 because we show 12 items in the grid view
|
data = get_product_data(search, start, limit)
|
||||||
|
|
||||||
|
for item in data:
|
||||||
|
set_product_info_for_website(item)
|
||||||
|
|
||||||
|
return [get_item_for_list_in_html(r) for r in data]
|
||||||
|
|
||||||
|
def get_product_data(search=None, start=0, limit=12):
|
||||||
|
# limit = 12 because we show 12 items in the grid view
|
||||||
# base query
|
# base query
|
||||||
query = """select I.name, I.item_name, I.item_code, I.route, I.image, I.website_image, I.thumbnail, I.item_group,
|
query = """select I.name, I.item_name, I.item_code, I.route, I.image, I.website_image, I.thumbnail, I.item_group,
|
||||||
I.description, I.web_long_description as website_description, I.is_stock_item,
|
I.description, I.web_long_description as website_description, I.is_stock_item,
|
||||||
@ -49,24 +57,25 @@ def get_product_list(search=None, start=0, limit=12):
|
|||||||
# order by
|
# order by
|
||||||
query += """ order by I.weightage desc, in_stock desc, I.modified desc limit %s, %s""" % (cint(start), cint(limit))
|
query += """ order by I.weightage desc, in_stock desc, I.modified desc limit %s, %s""" % (cint(start), cint(limit))
|
||||||
|
|
||||||
data = frappe.db.sql(query, {
|
return frappe.db.sql(query, {
|
||||||
"search": search,
|
"search": search,
|
||||||
"today": nowdate()
|
"today": nowdate()
|
||||||
}, as_dict=1)
|
}, as_dict=1)
|
||||||
|
|
||||||
for item in data:
|
|
||||||
set_product_info_for_website(item)
|
|
||||||
|
|
||||||
return [get_item_for_list_in_html(r) for r in data]
|
|
||||||
|
|
||||||
@frappe.whitelist(allow_guest=True)
|
@frappe.whitelist(allow_guest=True)
|
||||||
def search(query, limit=10, fuzzy_search=True):
|
def search(query, limit=10, fuzzy_search=True):
|
||||||
|
search_results = {"from_redisearch": True, "results": []}
|
||||||
|
|
||||||
|
if not is_search_module_loaded():
|
||||||
|
# Redisearch module not loaded
|
||||||
|
search_results["from_redisearch"] = False
|
||||||
|
search_results["results"] = get_product_data(query, 0, limit)
|
||||||
|
return search_results
|
||||||
|
|
||||||
if not query:
|
if not query:
|
||||||
# TODO: return top searches
|
return search_results
|
||||||
return []
|
|
||||||
|
|
||||||
red = frappe.cache()
|
red = frappe.cache()
|
||||||
|
|
||||||
query = clean_up_query(query)
|
query = clean_up_query(query)
|
||||||
|
|
||||||
ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=red)
|
ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=red)
|
||||||
@ -88,12 +97,12 @@ def search(query, limit=10, fuzzy_search=True):
|
|||||||
print(f"Executing query: {q.query_string()}")
|
print(f"Executing query: {q.query_string()}")
|
||||||
|
|
||||||
results = client.search(q)
|
results = client.search(q)
|
||||||
results = list(map(convert_to_dict, results.docs))
|
search_results['results'] = list(map(convert_to_dict, results.docs))
|
||||||
|
|
||||||
# FOR DEBUGGING
|
# FOR DEBUGGING
|
||||||
print("SEARCH RESULTS ------------------\n ", results)
|
print("SEARCH RESULTS ------------------\n ", search_results)
|
||||||
|
|
||||||
return results
|
return search_results
|
||||||
|
|
||||||
def clean_up_query(query):
|
def clean_up_query(query):
|
||||||
return ''.join(c for c in query if c.isalnum() or c.isspace())
|
return ''.join(c for c in query if c.isalnum() or c.isspace())
|
||||||
@ -103,11 +112,19 @@ def convert_to_dict(redis_search_doc):
|
|||||||
|
|
||||||
@frappe.whitelist(allow_guest=True)
|
@frappe.whitelist(allow_guest=True)
|
||||||
def get_category_suggestions(query):
|
def get_category_suggestions(query):
|
||||||
|
search_results = {"from_redisearch": True, "results": []}
|
||||||
|
|
||||||
|
if not is_search_module_loaded():
|
||||||
|
# Redisearch module not loaded
|
||||||
|
search_results["from_redisearch"] = False
|
||||||
|
return search_results
|
||||||
|
|
||||||
if not query:
|
if not query:
|
||||||
# TODO: return top searches
|
return search_results
|
||||||
return []
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
return [s.string for s in suggestions]
|
search_results['results'] = [s.string for s in suggestions]
|
||||||
|
|
||||||
|
return search_results
|
||||||
@ -45,8 +45,18 @@ function populateRecentSearches() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function populateResults(data) {
|
function populateResults(data) {
|
||||||
|
if (!data.message.from_redisearch) {
|
||||||
|
// Data not from redisearch
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.message.results.length === 0) {
|
||||||
|
results.innerHTML = 'No results';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
html = ""
|
html = ""
|
||||||
for (let res of data.message) {
|
search_results = data.message.results
|
||||||
|
for (let res of search_results) {
|
||||||
html += `<li class="list-group-item list-group-item-action">
|
html += `<li class="list-group-item list-group-item-action">
|
||||||
<img class="item-thumb" src="${res.thumbnail || 'img/placeholder.png'}" />
|
<img class="item-thumb" src="${res.thumbnail || 'img/placeholder.png'}" />
|
||||||
<a href="/${res.route}">${res.web_item_name} <span class="brand-line">${showBrandLine && res.brand ? "by " + res.brand : ""}</span></a>
|
<a href="/${res.route}">${res.web_item_name} <span class="brand-line">${showBrandLine && res.brand ? "by " + res.brand : ""}</span></a>
|
||||||
@ -56,13 +66,20 @@ function populateResults(data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function populateCategoriesList(data) {
|
function populateCategoriesList(data) {
|
||||||
if (data.length === 0) {
|
if (!data.message.from_redisearch) {
|
||||||
categoryList.innerHTML = 'Type something...';
|
// Data not from redisearch
|
||||||
|
categoryList.innerHTML = "Install Redisearch to enable autocompletions.";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.message.results.length === 0) {
|
||||||
|
categoryList.innerHTML = 'No results';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
html = ""
|
html = ""
|
||||||
for (let category of data.message) {
|
search_results = data.message.results
|
||||||
|
for (let category of search_results) {
|
||||||
html += `<li>${category}</li>`
|
html += `<li>${category}</li>`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user