2022-04-04 05:37:53 +00:00
|
|
|
# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors
|
2021-04-26 01:31:06 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2022-04-04 06:02:49 +00:00
|
|
|
import json
|
|
|
|
|
2021-04-26 01:31:06 +00:00
|
|
|
import frappe
|
2022-04-04 05:37:53 +00:00
|
|
|
from frappe import _
|
2021-04-29 15:17:32 +00:00
|
|
|
from frappe.utils.redis_wrapper import RedisWrapper
|
2022-04-04 05:37:53 +00:00
|
|
|
from redis import ResponseError
|
2022-01-31 19:09:14 +00:00
|
|
|
from redisearch import AutoCompleter, Client, IndexDefinition, Suggestion, TagField, TextField
|
2021-04-26 01:31:06 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
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"
|
|
|
|
|
2021-04-26 01:31:06 +00:00
|
|
|
|
2021-09-02 08:37:59 +00:00
|
|
|
def get_indexable_web_fields():
|
|
|
|
"Return valid fields from Website Item that can be searched for."
|
|
|
|
web_item_meta = frappe.get_meta("Website Item", cached=True)
|
|
|
|
valid_fields = filter(
|
|
|
|
lambda df: df.fieldtype in ("Link", "Table MultiSelect", "Data", "Small Text", "Text Editor"),
|
2022-03-28 13:22:46 +00:00
|
|
|
web_item_meta.fields,
|
|
|
|
)
|
2021-09-02 08:37:59 +00:00
|
|
|
|
|
|
|
return [df.fieldname for df in valid_fields]
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-03-31 10:59:18 +00:00
|
|
|
def is_redisearch_enabled():
|
|
|
|
"Return True only if redisearch is loaded and enabled."
|
|
|
|
is_redisearch_enabled = frappe.db.get_single_value("E Commerce Settings", "is_redisearch_enabled")
|
|
|
|
return is_search_module_loaded() and is_redisearch_enabled
|
|
|
|
|
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
def is_search_module_loaded():
|
2021-09-18 08:53:44 +00:00
|
|
|
try:
|
|
|
|
cache = frappe.cache()
|
2022-03-28 13:22:46 +00:00
|
|
|
out = cache.execute_command("MODULE LIST")
|
2021-06-01 07:14:49 +00:00
|
|
|
|
2021-09-18 08:53:44 +00:00
|
|
|
parsed_output = " ".join(
|
|
|
|
(" ".join([s.decode() for s in o if not isinstance(s, int)]) for o in out)
|
|
|
|
)
|
|
|
|
return "search" in parsed_output
|
|
|
|
except Exception:
|
2022-04-04 05:37:53 +00:00
|
|
|
return False # handling older redis versions
|
2021-06-01 07:14:49 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-03-31 10:59:18 +00:00
|
|
|
def if_redisearch_enabled(function):
|
|
|
|
"Decorator to check if Redisearch is enabled."
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
def wrapper(*args, **kwargs):
|
2022-03-31 10:59:18 +00:00
|
|
|
if is_redisearch_enabled():
|
2021-06-01 07:14:49 +00:00
|
|
|
func = function(*args, **kwargs)
|
|
|
|
return func
|
|
|
|
return
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
def make_key(key):
|
2022-03-28 13:22:46 +00:00
|
|
|
return "{0}|{1}".format(frappe.conf.db_name, key).encode("utf-8")
|
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
|
2022-03-31 10:59:18 +00:00
|
|
|
@if_redisearch_enabled
|
2021-04-26 01:31:06 +00:00
|
|
|
def create_website_items_index():
|
2021-09-02 08:37:59 +00:00
|
|
|
"Creates Index Definition."
|
|
|
|
|
2021-04-26 01:31:06 +00:00
|
|
|
# CREATE index
|
2021-04-29 15:17:32 +00:00
|
|
|
client = Client(make_key(WEBSITE_ITEM_INDEX), conn=frappe.cache())
|
2021-04-26 01:31:06 +00:00
|
|
|
|
|
|
|
try:
|
2022-04-04 05:37:53 +00:00
|
|
|
client.drop_index() # drop if already exists
|
|
|
|
except ResponseError:
|
|
|
|
# will most likely raise a ResponseError if index does not exist
|
|
|
|
# ignore and create index
|
2021-04-26 01:31:06 +00:00
|
|
|
pass
|
2022-04-04 05:37:53 +00:00
|
|
|
except Exception:
|
|
|
|
raise_redisearch_error()
|
2021-04-26 01:31:06 +00:00
|
|
|
|
2021-04-29 15:17:32 +00:00
|
|
|
idx_def = IndexDefinition([make_key(WEBSITE_ITEM_KEY_PREFIX)])
|
2021-04-26 01:31:06 +00:00
|
|
|
|
2022-04-04 05:37:53 +00:00
|
|
|
# Index fields mentioned in e-commerce settings
|
2022-03-28 13:22:46 +00:00
|
|
|
idx_fields = frappe.db.get_single_value("E Commerce Settings", "search_index_fields")
|
|
|
|
idx_fields = idx_fields.split(",") if idx_fields else []
|
2021-04-26 01:31:06 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if "web_item_name" in idx_fields:
|
|
|
|
idx_fields.remove("web_item_name")
|
2021-06-01 07:14:49 +00:00
|
|
|
|
2021-04-26 01:31:06 +00:00
|
|
|
idx_fields = list(map(to_search_field, idx_fields))
|
|
|
|
|
|
|
|
client.create_index(
|
|
|
|
[TextField("web_item_name", sortable=True)] + idx_fields,
|
2021-04-29 15:17:32 +00:00
|
|
|
definition=idx_def,
|
2021-04-26 01:31:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
reindex_all_web_items()
|
|
|
|
define_autocomplete_dictionary()
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-04-26 01:31:06 +00:00
|
|
|
def to_search_field(field):
|
|
|
|
if field == "tags":
|
|
|
|
return TagField("tags", separator=",")
|
|
|
|
|
|
|
|
return TextField(field)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-03-31 10:59:18 +00:00
|
|
|
@if_redisearch_enabled
|
2021-04-26 01:31:06 +00:00
|
|
|
def insert_item_to_index(website_item_doc):
|
|
|
|
# Insert item to index
|
|
|
|
key = get_cache_key(website_item_doc.name)
|
2021-06-01 07:14:49 +00:00
|
|
|
cache = frappe.cache()
|
2021-04-26 01:31:06 +00:00
|
|
|
web_item = create_web_item_map(website_item_doc)
|
2021-04-29 15:17:32 +00:00
|
|
|
|
2022-04-04 05:37:53 +00:00
|
|
|
for field, value in web_item.items():
|
|
|
|
super(RedisWrapper, cache).hset(make_key(key), field, value)
|
2021-04-29 15:17:32 +00:00
|
|
|
|
2021-04-26 01:31:06 +00:00
|
|
|
insert_to_name_ac(website_item_doc.web_item_name, website_item_doc.name)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-03-31 10:59:18 +00:00
|
|
|
@if_redisearch_enabled
|
2021-04-26 01:31:06 +00:00
|
|
|
def insert_to_name_ac(web_name, doc_name):
|
2021-04-29 15:17:32 +00:00
|
|
|
ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=frappe.cache())
|
2021-04-26 01:31:06 +00:00
|
|
|
ac.add_suggestions(Suggestion(web_name, payload=doc_name))
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-04-26 01:31:06 +00:00
|
|
|
def create_web_item_map(website_item_doc):
|
|
|
|
fields_to_index = get_fields_indexed()
|
|
|
|
web_item = {}
|
|
|
|
|
2022-04-04 05:37:53 +00:00
|
|
|
for field in fields_to_index:
|
|
|
|
web_item[field] = website_item_doc.get(field) or ""
|
2021-04-26 01:31:06 +00:00
|
|
|
|
|
|
|
return web_item
|
2021-05-26 14:56:34 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-03-31 10:59:18 +00:00
|
|
|
@if_redisearch_enabled
|
2021-04-26 01:31:06 +00:00
|
|
|
def update_index_for_item(website_item_doc):
|
|
|
|
# Reinsert to Cache
|
|
|
|
insert_item_to_index(website_item_doc)
|
|
|
|
define_autocomplete_dictionary()
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-03-31 10:59:18 +00:00
|
|
|
@if_redisearch_enabled
|
2021-04-26 01:31:06 +00:00
|
|
|
def delete_item_from_index(website_item_doc):
|
2021-06-01 07:14:49 +00:00
|
|
|
cache = frappe.cache()
|
2021-04-26 01:31:06 +00:00
|
|
|
key = get_cache_key(website_item_doc.name)
|
2021-06-01 07:14:49 +00:00
|
|
|
|
2021-04-26 01:31:06 +00:00
|
|
|
try:
|
2021-06-01 07:14:49 +00:00
|
|
|
cache.delete(key)
|
2021-06-02 07:54:06 +00:00
|
|
|
except Exception:
|
2022-04-04 05:37:53 +00:00
|
|
|
raise_redisearch_error()
|
2021-04-26 01:31:06 +00:00
|
|
|
|
2021-05-05 08:17:43 +00:00
|
|
|
delete_from_ac_dict(website_item_doc)
|
2021-04-26 01:31:06 +00:00
|
|
|
return True
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-03-31 10:59:18 +00:00
|
|
|
@if_redisearch_enabled
|
2021-05-05 08:17:43 +00:00
|
|
|
def delete_from_ac_dict(website_item_doc):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""Removes this items's name from autocomplete dictionary"""
|
2021-06-01 07:14:49 +00:00
|
|
|
cache = frappe.cache()
|
|
|
|
name_ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=cache)
|
2021-05-05 08:17:43 +00:00
|
|
|
name_ac.delete(website_item_doc.web_item_name)
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-03-31 10:59:18 +00:00
|
|
|
@if_redisearch_enabled
|
2021-04-26 01:31:06 +00:00
|
|
|
def define_autocomplete_dictionary():
|
2022-04-01 13:17:01 +00:00
|
|
|
"""
|
|
|
|
Defines/Redefines an autocomplete search dictionary for Website Item Name.
|
|
|
|
Also creats autocomplete dictionary for Published Item Groups.
|
|
|
|
"""
|
2021-04-26 01:31:06 +00:00
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
cache = frappe.cache()
|
2022-04-01 13:17:01 +00:00
|
|
|
item_ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=cache)
|
|
|
|
item_group_ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=cache)
|
2021-06-01 07:14:49 +00:00
|
|
|
|
2021-04-26 05:45:20 +00:00
|
|
|
# Delete both autocomplete dicts
|
2021-04-26 01:31:06 +00:00
|
|
|
try:
|
2021-06-01 07:14:49 +00:00
|
|
|
cache.delete(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE))
|
|
|
|
cache.delete(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE))
|
2021-06-02 07:54:06 +00:00
|
|
|
except Exception:
|
2022-04-04 05:37:53 +00:00
|
|
|
raise_redisearch_error()
|
2021-06-01 07:14:49 +00:00
|
|
|
|
2022-04-01 13:17:01 +00:00
|
|
|
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."
|
2021-04-26 01:31:06 +00:00
|
|
|
items = frappe.get_all(
|
2022-03-28 13:22:46 +00:00
|
|
|
"Website Item", fields=["web_item_name", "item_group"], filters={"published": 1}
|
2021-04-26 01:31:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for item in items:
|
2022-04-01 13:17:01 +00:00
|
|
|
autocompleter.add_suggestions(Suggestion(item.web_item_name))
|
2021-04-26 01:31:06 +00:00
|
|
|
|
2022-04-01 13:17:01 +00:00
|
|
|
|
|
|
|
@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:
|
2022-04-04 07:03:25 +00:00
|
|
|
payload = json.dumps({"name": item_group.name, "route": item_group.route})
|
2022-04-01 13:17:01 +00:00
|
|
|
autocompleter.add_suggestions(
|
|
|
|
Suggestion(
|
|
|
|
string=item_group.name,
|
2022-04-04 06:34:35 +00:00
|
|
|
score=frappe.utils.flt(item_group.weightage) or 1.0,
|
2022-04-01 13:17:01 +00:00
|
|
|
payload=payload, # additional info that can be retrieved later
|
|
|
|
)
|
|
|
|
)
|
2021-04-26 01:31:06 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-03-31 10:59:18 +00:00
|
|
|
@if_redisearch_enabled
|
2021-04-26 01:31:06 +00:00
|
|
|
def reindex_all_web_items():
|
2022-03-28 13:22:46 +00:00
|
|
|
items = frappe.get_all("Website Item", fields=get_fields_indexed(), filters={"published": True})
|
2021-04-26 01:31:06 +00:00
|
|
|
|
2021-06-01 07:14:49 +00:00
|
|
|
cache = frappe.cache()
|
2021-04-26 01:31:06 +00:00
|
|
|
for item in items:
|
|
|
|
web_item = create_web_item_map(item)
|
2021-04-29 15:17:32 +00:00
|
|
|
key = make_key(get_cache_key(item.name))
|
|
|
|
|
2022-04-04 05:37:53 +00:00
|
|
|
for field, value in web_item.items():
|
|
|
|
super(RedisWrapper, cache).hset(key, field, value)
|
2021-04-29 15:17:32 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-04-26 01:31:06 +00:00
|
|
|
def get_cache_key(name):
|
|
|
|
name = frappe.scrub(name)
|
|
|
|
return f"{WEBSITE_ITEM_KEY_PREFIX}{name}"
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-04-26 01:31:06 +00:00
|
|
|
def get_fields_indexed():
|
2022-03-28 13:22:46 +00:00
|
|
|
fields_to_index = frappe.db.get_single_value("E Commerce Settings", "search_index_fields")
|
|
|
|
fields_to_index = fields_to_index.split(",") if fields_to_index else []
|
2021-04-26 01:31:06 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
mandatory_fields = ["name", "web_item_name", "route", "thumbnail", "ranking"]
|
2021-04-26 01:31:06 +00:00
|
|
|
fields_to_index = fields_to_index + mandatory_fields
|
|
|
|
|
|
|
|
return fields_to_index
|
2022-04-04 05:37:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def raise_redisearch_error():
|
|
|
|
"Create an Error Log and raise error."
|
2022-05-02 09:34:26 +00:00
|
|
|
log = frappe.log_error("Redisearch Error")
|
2022-04-04 05:37:53 +00:00
|
|
|
log_link = frappe.utils.get_link_to_form("Error Log", log.name)
|
|
|
|
|
|
|
|
frappe.throw(
|
|
|
|
msg=_("Something went wrong. Check {0}").format(log_link), title=_("Redisearch Error")
|
|
|
|
)
|