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: Fallback to old style search when redisearch isn't available #208

Merged
merged 1 commit into from
Jan 1, 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
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