Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(HelpdeskSearch): Recover from clear cache #1977

Merged
merged 5 commits into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading