mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-20 10:58:33 +00:00
keyword search use seasearch (#5816)
This commit is contained in:
@@ -259,87 +259,21 @@ export default class AISearch extends Component {
|
|||||||
this.updateSearchPageURL(queryData);
|
this.updateSearchPageURL(queryData);
|
||||||
queryData['per_page'] = PER_PAGE;
|
queryData['per_page'] = PER_PAGE;
|
||||||
queryData['page'] = page;
|
queryData['page'] = page;
|
||||||
queryData['search_filename_only'] = true;
|
this.onAiSearch(queryData, cancelToken, page);
|
||||||
if (this.state.indexState === INDEX_STATE.FINISHED) {
|
|
||||||
this.onCombinedSearch(queryData, cancelToken, page);
|
|
||||||
} else {
|
|
||||||
this.onNormalSearch(queryData, cancelToken, page);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
onNormalSearch = (queryData, cancelToken, page) => {
|
onAiSearch = (queryData, cancelToken, page) => {
|
||||||
seafileAPI.searchFiles(queryData, cancelToken).then(res => {
|
|
||||||
this.source = null;
|
|
||||||
if (res.data.total > 0) {
|
|
||||||
this.setState({
|
|
||||||
resultItems: [...this.state.resultItems, ...this.formatResultItems(res.data.results)],
|
|
||||||
isResultGetted: true,
|
|
||||||
isLoading: false,
|
|
||||||
page: page + 1,
|
|
||||||
hasMore: res.data.has_more,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.setState({
|
|
||||||
highlightIndex: 0,
|
|
||||||
resultItems: [],
|
|
||||||
isLoading: false,
|
|
||||||
isResultGetted: true,
|
|
||||||
hasMore: res.data.has_more,
|
|
||||||
});
|
|
||||||
}).catch(error => {
|
|
||||||
/* eslint-disable */
|
|
||||||
console.log(error);
|
|
||||||
this.setState({ isLoading: false });
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
onCombinedSearch = (queryData, cancelToken, page) => {
|
|
||||||
const { indexState } = this.state;
|
|
||||||
if (indexState === INDEX_STATE.UNCREATED) {
|
|
||||||
toaster.warning(gettext('Please create index first.'));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (indexState === INDEX_STATE.RUNNING) {
|
|
||||||
toaster.warning(gettext('Indexing, please try again later.'));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let results = [];
|
let results = [];
|
||||||
let normalSearchQueryData = Object.assign({}, queryData, {'search_filename_only': true});
|
seafileAPI.aiSearchFiles(queryData, cancelToken).then(res => {
|
||||||
seafileAPI.searchFiles(normalSearchQueryData, cancelToken).then(res => {
|
console.log(res.data)
|
||||||
if (res.data.total > 0) {
|
|
||||||
results = [...results, ...this.formatResultItems(res.data.results)];
|
results = [...results, ...this.formatResultItems(res.data.results)];
|
||||||
}
|
|
||||||
seafileAPI.similaritySearchFiles(queryData, cancelToken).then(res => {
|
|
||||||
this.source = null;
|
|
||||||
if (res.data && res.data.children_list) {
|
|
||||||
results = [...results, ...this.formatSimilarityItems(res.data.children_list)];
|
|
||||||
}
|
|
||||||
|
|
||||||
let tempPathObj = {};
|
|
||||||
let searchResults = [];
|
|
||||||
results.forEach(item => {
|
|
||||||
if (!tempPathObj[item.path]) {
|
|
||||||
tempPathObj[item.path] = true;
|
|
||||||
searchResults.push(item);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
this.setState({
|
this.setState({
|
||||||
resultItems: searchResults,
|
resultItems: results,
|
||||||
isResultGetted: true,
|
isResultGetted: true,
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
page: page + 1,
|
page: page + 1,
|
||||||
hasMore: false,
|
hasMore: false,
|
||||||
});
|
});
|
||||||
}).catch(error => {
|
|
||||||
if (error && error.message === "prev request is cancelled") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let errMessage = Utils.getErrorMsg(error);
|
|
||||||
toaster.danger(errMessage);
|
|
||||||
this.setState({ isLoading: false });
|
|
||||||
});
|
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
console.log(error);
|
console.log(error);
|
||||||
@@ -377,29 +311,8 @@ export default class AISearch extends Component {
|
|||||||
items[i]['name'] = data[i].name;
|
items[i]['name'] = data[i].name;
|
||||||
items[i]['path'] = data[i].fullpath;
|
items[i]['path'] = data[i].fullpath;
|
||||||
items[i]['repo_id'] = data[i].repo_id;
|
items[i]['repo_id'] = data[i].repo_id;
|
||||||
items[i]['repo_name'] = data[i].repo_name;
|
|
||||||
items[i]['is_dir'] = data[i].is_dir;
|
items[i]['is_dir'] = data[i].is_dir;
|
||||||
items[i]['link_content'] = decodeURI(data[i].fullpath).substring(1);
|
items[i]['link_content'] = decodeURI(data[i].fullpath).substring(1);
|
||||||
items[i]['content'] = data[i].content_highlight;
|
|
||||||
items[i]['thumbnail_url'] = data[i].thumbnail_url;
|
|
||||||
}
|
|
||||||
return items;
|
|
||||||
}
|
|
||||||
|
|
||||||
formatSimilarityItems(data) {
|
|
||||||
let items = [];
|
|
||||||
let repo_id = this.props.repoID;
|
|
||||||
for (let i = 0; i < data.length; i++) {
|
|
||||||
items[i] = {};
|
|
||||||
items[i]['index'] = [i];
|
|
||||||
items[i]['name'] = data[i].path.substring(data[i].path.lastIndexOf('/')+1);
|
|
||||||
items[i]['path'] = data[i].path;
|
|
||||||
items[i]['repo_id'] = repo_id;
|
|
||||||
items[i]['repo_name'] = this.props.repoName;
|
|
||||||
items[i]['is_dir'] = false;
|
|
||||||
items[i]['link_content'] = decodeURI(data[i].path).substring(1);
|
|
||||||
items[i]['content'] = data[i].sentence;
|
|
||||||
items[i]['thumbnail_url'] = '';
|
|
||||||
}
|
}
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from rest_framework.authentication import SessionAuthentication
|
from rest_framework.authentication import SessionAuthentication
|
||||||
@@ -13,7 +14,7 @@ from seahub.api2.utils import api_error
|
|||||||
|
|
||||||
from seahub.views import check_folder_permission
|
from seahub.views import check_folder_permission
|
||||||
from seahub.utils.repo import parse_repo_perm
|
from seahub.utils.repo import parse_repo_perm
|
||||||
from seahub.ai.utils import create_library_sdoc_index, similarity_search_in_library, update_library_sdoc_index, \
|
from seahub.ai.utils import create_library_sdoc_index, search, update_library_sdoc_index, \
|
||||||
delete_library_index, query_task_status, query_library_index_state, question_answering_search_in_library,\
|
delete_library_index, query_task_status, query_library_index_state, question_answering_search_in_library,\
|
||||||
get_file_download_token
|
get_file_download_token
|
||||||
|
|
||||||
@@ -62,7 +63,7 @@ class LibrarySdocIndexes(APIView):
|
|||||||
return Response(resp_json, resp.status_code)
|
return Response(resp_json, resp.status_code)
|
||||||
|
|
||||||
|
|
||||||
class SimilaritySearchInLibrary(APIView):
|
class Search(APIView):
|
||||||
authentication_classes = (TokenAuthentication, SessionAuthentication)
|
authentication_classes = (TokenAuthentication, SessionAuthentication)
|
||||||
permission_classes = (IsAuthenticated, )
|
permission_classes = (IsAuthenticated, )
|
||||||
throttle_classes = (UserRateThrottle, )
|
throttle_classes = (UserRateThrottle, )
|
||||||
@@ -89,7 +90,7 @@ class SimilaritySearchInLibrary(APIView):
|
|||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resp = similarity_search_in_library(params)
|
resp = search(params)
|
||||||
if resp.status_code == 500:
|
if resp.status_code == 500:
|
||||||
logger.error('search in library error status: %s body: %s', resp.status_code, resp.text)
|
logger.error('search in library error status: %s body: %s', resp.status_code, resp.text)
|
||||||
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error')
|
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error')
|
||||||
@@ -266,21 +267,3 @@ class FileDownloadToken(APIView):
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Response(library_files_info, status.HTTP_200_OK)
|
return Response(library_files_info, status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
# class RepoCommit(APIView):
|
|
||||||
# authentication_classes = (SeafileAiAuthentication, )
|
|
||||||
# throttle_classes = (UserRateThrottle, )
|
|
||||||
#
|
|
||||||
# def get(self, request):
|
|
||||||
# repo_id = request.GET.get('repo_id')
|
|
||||||
# if not repo_id:
|
|
||||||
# return api_error(status.HTTP_400_BAD_REQUEST, 'repo_id invalid')
|
|
||||||
#
|
|
||||||
# commit_id = get_latest_commit_id(repo_id)
|
|
||||||
#
|
|
||||||
# repo_info = {
|
|
||||||
# 'commit_id': commit_id
|
|
||||||
# }
|
|
||||||
#
|
|
||||||
# return Response(repo_info, status.HTTP_200_OK)
|
|
||||||
|
@@ -25,9 +25,9 @@ def create_library_sdoc_index(params):
|
|||||||
return resp
|
return resp
|
||||||
|
|
||||||
|
|
||||||
def similarity_search_in_library(params):
|
def search(params):
|
||||||
headers = gen_headers()
|
headers = gen_headers()
|
||||||
url = urljoin(SEAFILE_AI_SERVER_URL, '/api/v1/similarity-search-in-library/')
|
url = urljoin(SEAFILE_AI_SERVER_URL, '/api/v1/search/')
|
||||||
resp = requests.post(url, json=params, headers=headers)
|
resp = requests.post(url, json=params, headers=headers)
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
|
@@ -202,8 +202,8 @@ from seahub.seadoc.views import sdoc_revision, sdoc_revisions
|
|||||||
|
|
||||||
from seahub.ocm.settings import OCM_ENDPOINT
|
from seahub.ocm.settings import OCM_ENDPOINT
|
||||||
|
|
||||||
from seahub.ai.apis import LibrarySdocIndexes, SimilaritySearchInLibrary, LibrarySdocIndex, TaskStatus, \
|
from seahub.ai.apis import LibrarySdocIndexes, Search, LibrarySdocIndex, TaskStatus, \
|
||||||
LibraryIndexState, QuestionAnsweringSearchInLibrary
|
LibraryIndexState, QuestionAnsweringSearchInLibrary, FileDownloadToken
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('accounts/', include('seahub.base.registration_urls')),
|
path('accounts/', include('seahub.base.registration_urls')),
|
||||||
@@ -965,9 +965,10 @@ if getattr(settings, 'ENABLE_SEADOC', False):
|
|||||||
if settings.ENABLE_SEAFILE_AI:
|
if settings.ENABLE_SEAFILE_AI:
|
||||||
urlpatterns += [
|
urlpatterns += [
|
||||||
re_path(r'^api/v2.1/ai/library-sdoc-indexes/$', LibrarySdocIndexes.as_view(), name='api-v2.1-ai-library-sdoc-indexes'),
|
re_path(r'^api/v2.1/ai/library-sdoc-indexes/$', LibrarySdocIndexes.as_view(), name='api-v2.1-ai-library-sdoc-indexes'),
|
||||||
re_path(r'^api/v2.1/ai/similarity-search-in-library/$', SimilaritySearchInLibrary.as_view(), name='api-v2.1-ai-similarity-search-in-library'),
|
re_path(r'^api/v2.1/ai/search/$', Search.as_view(), name='api-v2.1-ai-search'),
|
||||||
re_path(r'^api/v2.1/ai/question-answering-search-in-library/$', QuestionAnsweringSearchInLibrary.as_view(), name='api-v2.1-ai-question-answering-search-in-library'),
|
re_path(r'^api/v2.1/ai/question-answering-search-in-library/$', QuestionAnsweringSearchInLibrary.as_view(), name='api-v2.1-ai-question-answering-search-in-library'),
|
||||||
re_path(r'^api/v2.1/ai/library-sdoc-index/$', LibrarySdocIndex.as_view(), name='api-v2.1-ai-library-sdoc-index'),
|
re_path(r'^api/v2.1/ai/library-sdoc-index/$', LibrarySdocIndex.as_view(), name='api-v2.1-ai-library-sdoc-index'),
|
||||||
re_path(r'^api/v2.1/ai/task-status/$', TaskStatus.as_view(), name='api-v2.1-ai-task-status'),
|
re_path(r'^api/v2.1/ai/task-status/$', TaskStatus.as_view(), name='api-v2.1-ai-task-status'),
|
||||||
re_path(r'^api/v2.1/ai/library-index-state/$', LibraryIndexState.as_view(), name='api-v2.1-ai-library-index-state'),
|
re_path(r'^api/v2.1/ai/library-index-state/$', LibraryIndexState.as_view(), name='api-v2.1-ai-library-index-state'),
|
||||||
|
re_path(r'^api/v2.1/ai/repo/file-download-token/$', FileDownloadToken.as_view(), name='api-v2.1-ai-repo-file-download-token'),
|
||||||
]
|
]
|
||||||
|
Reference in New Issue
Block a user