sonic: search by id

This commit is contained in:
undefined committed 2024-08-24 14:29:19 +08:00
1 parent afe8fbbaa8
commit ad726c7ea4
2 files changed
+17 -7

No files matched your search

+16 -6
View File
@@ -1,6 +1,6 @@
import {
Context, DomainModel, iterateAllProblem, iterateAllProblemInDomain,
Logger, Schema, SystemModel,
Logger, ProblemModel, Schema, SystemModel,
} from 'hydrooj';
import { SonicService } from './service';
@@ -11,12 +11,22 @@ export function apply(ctx: Context) {
global.Hydro.lib.problemSearch = async (domainId, query, opts) => {
const limit = opts?.limit || SystemModel.get('pagination.problem');
const ids = await ctx.sonic.query('problem', `${domainId}@title`, query, { limit });
if (limit - ids.length > 0) ids.push(...await ctx.sonic.query('problem', `${domainId}@content`, query, { limit: limit - ids.length }));
let hits = await ctx.sonic.query('problem', `${domainId}@title`, query, { limit });
if (!opts.skip) {
let pdoc = await ProblemModel.get(domainId, +query || query, ProblemModel.PROJECTION_LIST);
if (pdoc) {
hits = hits.filter((i) => i !== `${pdoc.domainId}/${pdoc.docId}`);
hits.unshift(`${pdoc.domainId}/${pdoc.docId}`);
} else if (/^P\d+$/.test(query)) {
pdoc = await ProblemModel.get(domainId, +query.substring(1), ProblemModel.PROJECTION_LIST);
if (pdoc) hits.unshift(`${pdoc.domainId}/${pdoc.docId}`);
}
}
if (limit - hits.length > 0) hits.push(...await ctx.sonic.query('problem', `${domainId}@content`, query, { limit: limit - hits.length }));
return {
countRelation: ids.length >= limit ? 'gte' : 'eq',
total: ids.length,
hits: ids,
countRelation: hits.length >= limit ? 'gte' : 'eq',
total: hits.length,
hits,
};
};
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@hydrooj/sonic",
"version": "1.3.1",
"version": "1.3.2",
"description": "Sonic search service",
"main": "index.ts",
"preferUnplugged": true,