Skip to content

Commit

Permalink
fix: Fallback to old style search when redisearch isn't available
Browse files Browse the repository at this point in the history
  • Loading branch information
ankush committed Jan 1, 2024
1 parent d9e1310 commit 7a72024
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions wiki/wiki/doctype/wiki_page/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@
from frappe.search import web_search
from frappe.utils import strip_html_tags, update_progress_bar
from frappe.utils.redis_wrapper import RedisWrapper
from redis.commands.search.field import TextField
from redis.commands.search.indexDefinition import IndexDefinition
from redis.commands.search.query import Query
from redis.exceptions import ResponseError

PREFIX = "wiki_page_search_doc"


_redisearch_available = False
try:
from redis.commands.search.query import Query # noqa: F401

_redisearch_available = True
except ImportError:
pass


@frappe.whitelist(allow_guest=True)
def search(query, path, space):
if not space:
space = get_space_route(path)

# fallback to frappe web search if redisearch is not enabled
if not frappe.db.get_single_value("Wiki Settings", "use_redisearch_for_search"):
use_redisearch = frappe.db.get_single_value("Wiki Settings", "use_redisearch_for_search")
if not use_redisearch or not _redisearch_available:
result = web_search(query, space, 5)

for d in result:
Expand All @@ -34,6 +39,9 @@ def search(query, path, space):

return {"docs": result, "search_engine": "frappe_web_search"}

from redis.commands.search.query import Query # noqa: F811
from redis.exceptions import ResponseError

# if redisearch enabled use redisearch
r = frappe.cache()
query = Query(query).paging(0, 5).highlight(tags=['<b class="match">', "</b>"])
Expand Down Expand Up @@ -73,6 +81,10 @@ def get_space_route(path):


def rebuild_index():
from redis.commands.search.field import TextField
from redis.commands.search.indexDefinition import IndexDefinition
from redis.exceptions import ResponseError

r = frappe.cache()
r.set_value("wiki_page_index_in_progress", True)

Expand Down Expand Up @@ -124,6 +136,8 @@ def create_index_for_records(records, space):


def remove_index_for_records(records, space):
from redis.exceptions import ResponseError

r = frappe.cache()
for d in records:
try:
Expand Down Expand Up @@ -155,6 +169,8 @@ def remove_index(doc):


def drop_index(space):
from redis.exceptions import ResponseError

try:
frappe.cache().ft(space).dropindex(delete_documents=True)
except ResponseError:
Expand Down

0 comments on commit 7a72024

Please sign in to comment.