core: allow cross-domain problem search

This commit is contained in:
undefined committed 2021-12-01 22:28:22 +08:00
1 parent 71a5f6dd13
commit a0734a1030
11 files changed
+120 -53

No files matched your search

+35 -11
View File
@@ -1,26 +1,50 @@
import DomainModel from 'hydrooj/src/model/domain';
import * as system from 'hydrooj/src/model/system';
import * as bus from 'hydrooj/src/service/bus';
import sonic from './service';
bus.on('problem/add', async (doc, docId) => {
await sonic.push('problem', `${doc.domainId}@title`, docId.toString(), doc.title);
await sonic.push('problem', `${doc.domainId}@content`, docId.toString(), doc.content.toString());
const union = await DomainModel.searchUnion({ union: doc.domainId, problem: true });
const tasks = [];
for (const domainId of [doc.domainId, ...union.map((i) => i._id)]) {
tasks.push(
sonic.push('problem', `${domainId}@title`, `${doc.domainId}/${docId}`, doc.title),
sonic.push('problem', `${domainId}@content`, `${doc.domainId}/${docId}`, doc.content.toString()),
);
}
await Promise.all(tasks);
});
bus.on('problem/edit', async (pdoc) => {
await sonic.flusho('problem', `${pdoc.domainId}@title`, pdoc.docId.toString());
await sonic.flusho('problem', `${pdoc.domainId}@content`, pdoc.docId.toString());
await sonic.push('problem', `${pdoc.domainId}@title`, pdoc.docId.toString(), pdoc.title);
await sonic.push('problem', `${pdoc.domainId}@content`, pdoc.docId.toString(), pdoc.content.toString());
const union = await DomainModel.searchUnion({ union: pdoc.domainId, problem: true });
const tasks = [];
const id = `${pdoc.domainId}/${pdoc.docId}`;
for (const domainId of [pdoc.domainId, ...union.map((i) => i._id)]) {
tasks.push(
sonic.flusho('problem', `${domainId}@title`, id)
.then(() => sonic.push('problem', `${domainId}@title`, id, pdoc.title)),
sonic.flusho('problem', `${domainId}@content`, id)
.then(() => sonic.push('problem', `${domainId}@content`, id, pdoc.content.toString())),
);
}
await Promise.all(tasks);
});
bus.on('problem/del', async (domainId, docId) => {
await sonic.flusho('problem', `${domainId}@title`, docId.toString());
await sonic.flusho('problem', `${domainId}@content`, docId.toString());
const union = await DomainModel.searchUnion({ union: domainId, problem: true });
const tasks = [];
const id = `${domainId}/${docId}`;
for (const domain of [domainId, ...union.map((i) => i._id)]) {
tasks.push(
sonic.flusho('problem', `${domain}@title`, id),
sonic.flusho('problem', `${domain}@content`, id),
);
}
await Promise.all(tasks);
});
global.Hydro.lib.problemSearch = async (domainId: string, query: string, limit = system.get('pagination.problem')) => {
const docIds = await sonic.query('problem', `${domainId}@title`, query, { limit });
if (limit - docIds.length > 0) docIds.push(...await sonic.query('problem', `${domainId}@content`, query, { limit: limit - docIds.length }));
return docIds.map(Number);
const ids = await sonic.query('problem', `${domainId}@title`, query, { limit });
if (limit - ids.length > 0) ids.push(...await sonic.query('problem', `${domainId}@content`, query, { limit: limit - ids.length }));
return ids;
};
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@hydrooj/sonic",
"version": "1.1.1",
"version": "1.2.0",
"description": "Sonic search service",
"main": "service.js",
"typings": "service.d.ts",
+10 -4
View File
@@ -1,3 +1,4 @@
import DomainModel from 'hydrooj/src/model/domain';
import { iterateAllProblem, iterateAllProblemInDomain } from 'hydrooj/src/pipelineUtils';
import sonic from './service';
@@ -10,10 +11,15 @@ export async function run({ domainId }, report) {
const cb = async (pdoc) => {
i++;
if (!(i % 1000)) report({ message: `${i} problems indexed` });
await Promise.all([
sonic.push('problem', `${pdoc.domainId}@title`, pdoc.docId.toString(), pdoc.title),
sonic.push('problem', `${pdoc.domainId}@content`, pdoc.docId.toString(), pdoc.content.toString()),
]).catch((e) => console.log(`${pdoc.domainId}/${pdoc.docId}`, e));
const union = await DomainModel.searchUnion({ union: pdoc.domainId, problem: true });
const tasks = [];
for (const did of [domainId, ...union.map((j) => j._id)]) {
tasks.push(
pdoc.title && sonic.push('problem', `${did}@title`, `${pdoc.domainId}/${pdoc.docId}`, pdoc.title),
pdoc.content.toString() && sonic.push('problem', `${did}@content`, `${pdoc.domainId}/${pdoc.docId}`, pdoc.content.toString()),
);
}
await Promise.all(tasks).catch((e) => console.log(`${pdoc.domainId}/${pdoc.docId}`, e));
};
if (domainId) await iterateAllProblemInDomain(domainId, ['title', 'content'], cb);
else await iterateAllProblem(['title', 'content'], cb);