Skip to content

Commit

Permalink
sonic: search by id
Browse files Browse the repository at this point in the history
  • Loading branch information
undefined-moe committed Aug 24, 2024
1 parent afe8fbb commit ad726c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions packages/sonic/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
Context, DomainModel, iterateAllProblem, iterateAllProblemInDomain,
Logger, Schema, SystemModel,
Logger, ProblemModel, Schema, SystemModel,
} from 'hydrooj';
import { SonicService } from './service';

Expand All @@ -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,
};
};

Expand Down
2 changes: 1 addition & 1 deletion packages/sonic/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hydrooj/sonic",
"version": "1.3.1",
"version": "1.3.2",
"description": "Sonic search service",
"main": "index.ts",
"preferUnplugged": true,
Expand Down

0 comments on commit ad726c7

Please sign in to comment.