Skip to content

Commit

Permalink
fix(HelpdeskSearch): Recover from clear cache (#1977)
Browse files Browse the repository at this point in the history
* fix(HelpdeskSearch): Check for existing index accurately

Checking for index only is problematic as frappe.clear_cache only
removes docs and not the index

* fix(HelpdeskSearch): Handle types and other exceptions

* style: isort

* style: Ignore flake warning for fn declaration

* style(flake8): SIM104
  • Loading branch information
balamurali27 committed Sep 10, 2024
1 parent aa05831 commit 333a5a4
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions helpdesk/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import json
import re
from contextlib import suppress
from copy import deepcopy
from math import isclose

import frappe
from bs4 import BeautifulSoup, PageElement
Expand Down Expand Up @@ -152,17 +154,27 @@ def spellcheck(self, query, **kwargs):
return self.redis.ft(self.index_name).spellcheck(query, **kwargs)

def drop_index(self):
if self.index_exists():
with suppress(ResponseError): # Index may not exist
self.redis.ft(self.index_name).dropindex(delete_documents=True)

def get_records(self, doctype: str): # noqa
raise NotImplementedError

def get_all_records(self):
for doctype in self.DOCTYPE_FIELDS.keys():
yield from self.get_records(doctype)

def num_records(self):
return len(list(self.get_all_records()))

def index_exists(self):
self._index_exists = getattr(self, "_index_exists", None)
if self._index_exists is None:
try:
self.redis.ft(self.index_name).info()
if hasattr(self, "_index_exists"):
return self._index_exists
self._index_exists = False
with suppress(ResponseError):
ftinfo = self.redis.ft(self.index_name).info()
if isclose(int(ftinfo["num_docs"]), self.num_records(), rel_tol=0.1):
self._index_exists = True
except ResponseError:
self._index_exists = False
return self._index_exists


Expand Down

0 comments on commit 333a5a4

Please sign in to comment.