Skip to content

Commit

Permalink
feat/seasearch: add res highlight sup in wiki seasearch
Browse files Browse the repository at this point in the history
  • Loading branch information
cir9no committed Aug 21, 2024
1 parent 764ede1 commit 4f828d5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
3 changes: 2 additions & 1 deletion seafevent_server/request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ def search():

return {'results': results}, 200


@app.route('/wiki-search', methods=['POST'])
def search():
def search_wikis():
is_valid = check_auth_token(request)
if not is_valid:
return {'error_msg': 'Permission denied'}, 403
Expand Down
21 changes: 14 additions & 7 deletions seasearch/index_store/wiki_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class WikiIndex(object):
},
'content': {
'type': 'text',
'highlightable': True,
'fields': {
'ngram': {
'type': 'text',
Expand Down Expand Up @@ -132,7 +133,7 @@ def get_wiki_conf(self, wiki_id):
# Get wiki config dict
conf_path = posixpath.join(WIKI_CONFIG_PATH, WIKI_CONFIG_FILE_NAME)
conf_id = seafile_api.get_file_id_by_path(wiki_id, conf_path)

f = fs_mgr.load_seafile(wiki_id, 1, conf_id)
return json.loads(f.get_content().decode())

Expand All @@ -143,7 +144,7 @@ def extract_ids_from_navigation(navigation_items, navigation_ids):
navigation_ids.add(item['id'])
if 'children' in item and item['children']:
extract_ids_from_navigation(item['children'], navigation_ids)

navigation_ids = set()
extract_ids_from_navigation(config['navigation'], navigation_ids)
doc_uuids = [page['docUuid'] for page in config['pages'] if page['id'] in navigation_ids]
Expand All @@ -156,7 +157,7 @@ def extract_ids_from_navigation(navigation_items, navigation_ids):
navigation_ids.add(item['id'])
if 'children' in item and item['children']:
extract_ids_from_navigation(item['children'], navigation_ids)

navigation_ids = set()
extract_ids_from_navigation(config['navigation'], navigation_ids)
non_navigation_doc_uuids = [page['docUuid'] for page in config['pages'] if page['id'] not in navigation_ids]
Expand Down Expand Up @@ -252,13 +253,18 @@ def search_wikis(self, wikis, keyword, start=0, size=10):
'from': start,
'size': size,
'_source': ['wiki_id', 'doc_uuid'],
'sort': ['_score']
'sort': ['_score'],
"highlight": {
"pre_tags": ["<mark>"],
"post_tags": ["</mark>"],
"fields": {"content": {}},
},
}
index_name = WIKI_INDEX_PREFIX + wiki_id
index_info = {"index": index_name}
bulk_search_params.append(index_info)
bulk_search_params.append(data)

# Get wiki title
conf = self.get_wiki_conf(wiki_id)
doc_uuids = self.extract_doc_uuids(conf)
Expand All @@ -269,7 +275,6 @@ def search_wikis(self, wikis, keyword, start=0, size=10):
uuid_name_list.append((page_uuid, page["name"], wiki_id))

results = self.seasearch_api.m_search(bulk_search_params)

res_wikis = []
for result in results.get('responses'):
hits = result.get('hits', {}).get('hits', [])
Expand All @@ -288,6 +293,8 @@ def search_wikis(self, wikis, keyword, start=0, size=10):
'_id': _id,
'type': 'wiki_content'
}
if highlight_content := hit.get('highlight').get('content', [None])[0]:
r.update(matching_content=highlight_content)
res_wikis.append(r)
res_wikis = sorted(res_wikis, key=lambda row: row['score'], reverse=True)[:size]

Expand All @@ -302,6 +309,6 @@ def search_wikis(self, wikis, keyword, start=0, size=10):
'type': 'wiki_title'
}
query_match.append(r_t)

query_match.extend(res_wikis)
return query_match
2 changes: 1 addition & 1 deletion seasearch/index_task/wiki_index_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def run(self):
sched = GeventScheduler()
logging.info('Start to update wiki index...')
try:
sched.add_job(update_wiki_indexes, CronTrigger(minute='*/5'),
sched.add_job(update_wiki_indexes, CronTrigger(second='*/30'),
args=(self.wiki_status_index, self.wiki_index, self.index_manager, self.repo_data))
except Exception as e:
logging.exception('periodical update wiki index error: %s', e)
Expand Down

0 comments on commit 4f828d5

Please sign in to comment.