diff --git a/scripts/pro.py b/scripts/pro.py index 4dd976470b..fcc8f8ab8d 100755 --- a/scripts/pro.py +++ b/scripts/pro.py @@ -657,7 +657,7 @@ def get_seafes_env(): def update_search_index(): argv = [ Utils.get_python_executable(), - '-m', 'seafes.index_local', + '-m', 'seafes.indexes.repo_file.index_local', '--loglevel', 'debug', 'update', ] @@ -677,7 +677,7 @@ def delete_search_index(): argv = [ Utils.get_python_executable(), - '-m', 'seafes.index_local', + '-m', 'seafes.indexes.repo_file.index_local', '--loglevel', 'debug', 'clear', ] diff --git a/seahub/api2/endpoints/utils.py b/seahub/api2/endpoints/utils.py index a5a75e6239..f4e05c67c2 100644 --- a/seahub/api2/endpoints/utils.py +++ b/seahub/api2/endpoints/utils.py @@ -5,6 +5,7 @@ import time import logging import requests import json +import os import datetime import urllib.request import urllib.parse @@ -22,6 +23,7 @@ from seahub.utils import get_log_events_by_time, is_pro_version, is_org_context from seahub.settings import SEADOC_PRIVATE_KEY, FILE_CONVERTER_SERVER_URL, SECRET_KEY, \ SEAFEVENTS_SERVER_URL + try: from seahub.settings import MULTI_TENANCY except ImportError: @@ -319,12 +321,3 @@ def event_export_status(task_id): resp = requests.get(url, params=params, headers=headers) return resp - - -def wiki_search(params): - payload = {'exp': int(time.time()) + 300, } - token = jwt.encode(payload, SECRET_KEY, algorithm='HS256') - headers = {"Authorization": "Token %s" % token} - url = urljoin(SEAFEVENTS_SERVER_URL, '/wiki-search') - resp = requests.post(url, json=params, headers=headers) - return resp diff --git a/seahub/api2/endpoints/wiki2.py b/seahub/api2/endpoints/wiki2.py index 56ecd9d73e..acd109635b 100644 --- a/seahub/api2/endpoints/wiki2.py +++ b/seahub/api2/endpoints/wiki2.py @@ -22,7 +22,9 @@ from django.utils.translation import gettext as _ from seahub.api2.authentication import TokenAuthentication from seahub.api2.throttling import UserRateThrottle from seahub.api2.utils import api_error, is_wiki_repo -from seahub.api2.endpoints.utils import wiki_search +from seahub.utils import HAS_FILE_SEARCH, HAS_FILE_SEASEARCH +if HAS_FILE_SEARCH or HAS_FILE_SEASEARCH: + from seahub.search.utils import search_wikis, ai_search_wikis from seahub.utils.db_api import SeafileDB from seahub.wiki2.models import Wiki2 as Wiki from seahub.wiki.models import Wiki as OldWiki @@ -33,7 +35,7 @@ from seahub.wiki2.utils import is_valid_wiki_name, get_wiki_config, WIKI_PAGES_D delete_page, move_nav, revert_nav, get_sub_ids_by_page_id, get_parent_id_stack, add_convert_wiki_task from seahub.utils import is_org_context, get_user_repos, is_pro_version, is_valid_dirent_name, \ - get_no_duplicate_obj_name + get_no_duplicate_obj_name, HAS_FILE_SEARCH, HAS_FILE_SEASEARCH from seahub.views import check_folder_permission from seahub.base.templatetags.seahub_tags import email2nickname @@ -1290,6 +1292,10 @@ class WikiSearch(APIView): throttle_classes = (UserRateThrottle, ) def post(self, request): + if not HAS_FILE_SEARCH and not HAS_FILE_SEASEARCH: + error_msg = 'Search not supported.' + return api_error(status.HTTP_404_NOT_FOUND, error_msg) + query = request.data.get('query') search_wiki = request.data.get('search_wiki') @@ -1310,18 +1316,25 @@ class WikiSearch(APIView): 'wiki': search_wiki, 'count': count, } - - try: - resp = wiki_search(params) - if resp.status_code == 500: - logger.error('search in wiki error status: %s body: %s', resp.status_code, resp.text) + if HAS_FILE_SEARCH: + try: + results = search_wikis(search_wiki, query, count) + except Exception as e: + logger.error(e) + results = [] + finally: + return Response({"results": results}) + elif HAS_FILE_SEASEARCH: + try: + resp = ai_search_wikis(params) + if resp.status_code == 500: + logger.error('search in wiki error status: %s body: %s', resp.status_code, resp.text) + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error') + resp_json = resp.json() + except Exception as e: + logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error') - resp_json = resp.json() - except Exception as e: - logger.error(e) - return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error') - - return Response(resp_json, resp.status_code) + return Response(resp_json, resp.status_code) class WikiConvertView(APIView): diff --git a/seahub/search/utils.py b/seahub/search/utils.py index 84392a9aa1..1dfd27af1f 100644 --- a/seahub/search/utils.py +++ b/seahub/search/utils.py @@ -19,7 +19,7 @@ import seaserv from seaserv import seafile_api os.environ['EVENTS_CONFIG_FILE'] = EVENTS_CONFIG_FILE -from seafes import es_search +from seafes import es_search, es_wiki_search # Get an instance of a logger logger = logging.getLogger(__name__) @@ -203,6 +203,10 @@ def search_files(repos_map, search_path, keyword, obj_desc, start, size, org_id= return result, total +def search_wikis(wiki_id, keyword, count): + return es_wiki_search(wiki_id, keyword, count) + + def ai_search_files(keyword, searched_repos, count, suffixes, search_path=None, obj_type=None): params = { 'query': keyword, @@ -226,6 +230,15 @@ def ai_search_files(keyword, searched_repos, count, suffixes, search_path=None, return files_found, total +def ai_search_wikis(params): + payload = {'exp': int(time.time()) + 300, } + token = jwt.encode(payload, SECRET_KEY, algorithm='HS256') + headers = {"Authorization": "Token %s" % token} + url = urljoin(SEAFEVENTS_SERVER_URL, '/wiki-search') + resp = requests.post(url, json=params, headers=headers) + return resp + + def is_valid_date_type(data): try: data = int(data)