优化中文搜索

This commit is contained in:
undefined committed 2020-09-06 12:12:54 +08:00
1 parent f2f0212947
commit 6eed519407
11 files changed
+91 -9

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
import nodejieba from 'nodejieba';
import { Pdoc } from 'hydrooj';
import * as document from 'hydrooj/dist/model/document';
import * as bus from 'hydrooj/dist/service/bus';
declare module 'hydrooj' {
interface Pdoc {
search?: string
}
}
bus.on('document/add', async (doc) => {
if (doc.type !== document.TYPE_PROBLEM) return;
const pdoc = doc as Pdoc;
const segments = nodejieba.cut(pdoc.title);
pdoc.search = segments.join(' ');
});
bus.on('document/set', async (domainId, docType, docId, $set) => {
if (docType !== document.TYPE_PROBLEM || !$set.title) return;
const segments = nodejieba.cut($set.title);
$set.search = segments.join(' ');
});
+20
View File
@@ -0,0 +1,20 @@
{
"name": "@hydrooj/better-search",
"version": "1.0.1",
"description": "Enhance Problem Search",
"main": "package.json",
"repository": "https://github.com/hydro-dev/Hydro.git",
"author": "undefined <masnn0@outlook.com>",
"license": "AGPL-3.0-only",
"dependencies": {
"nodejieba": "^2.4.1"
},
"devDependencies": {},
"peerDependencies": {
"hydrooj": "^2.13.2"
},
"scripts": {
"build": "tsc",
"lint": "eslint . --ext ts --fix"
}
}
+16
View File
@@ -0,0 +1,16 @@
import nodejieba from 'nodejieba';
import { Pdoc } from 'hydrooj';
import * as document from 'hydrooj/dist/model/document';
export const description = 'Ensure index for peoblem search';
export async function run() {
await document.coll.find({ docType: document.TYPE_PROBLEM }).forEach(async (doc: Pdoc) => {
const segments = nodejieba.cut(doc.title);
await document.coll.updateOne({ _id: doc._id }, { $set: { search: segments.join(' ') } });
});
}
export const validate = {};
global.Hydro.script.ensureSearch = { run, description, validate };
+19
View File
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "es2019",
"module": "commonjs",
"esModuleInterop": true,
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"composite": true,
"strictBindCallApply": true,
"experimentalDecorators": true,
"outDir": ".",
"rootDir": "."
},
"include": [
"*.ts"
],
"exclude": []
}
+1 -1
View File
@@ -252,4 +252,4 @@ async function postInit() {
task.consume({ type: 'judge' }, (t) => (new JudgeTask(t)).handle().catch(console.error)); task.consume({ type: 'judge' }, (t) => (new JudgeTask(t)).handle().catch(console.error));
} }
global.Hydro.postInit.push(postInit); global.Hydro.service.bus.once('app/started', postInit);
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "2.13.1", "version": "2.13.2",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"main": "dist/loader.js", "main": "dist/loader.js",
"typings": "dist/loader.d.ts", "typings": "dist/loader.d.ts",
+2 -1
View File
@@ -1,3 +1,4 @@
import { inspect } from 'util';
import * as judge from './judge'; import * as judge from './judge';
import { ValidationError } from '../error'; import { ValidationError } from '../error';
import * as check from '../check'; import * as check from '../check';
@@ -108,7 +109,7 @@ class SystemScriptHandler extends SystemHandler {
domainId, domainId,
rid, rid,
status: STATUS.STATUS_ACCEPTED, status: STATUS.STATUS_ACCEPTED,
message: ret.toString(), message: inspect(ret, false, 10, true),
judger: 1, judger: 1,
time, time,
memory: 0, memory: 0,
+4 -3
View File
@@ -61,10 +61,11 @@ export function get(domainId: string, docType: number, docId: DocID) {
return coll.findOne({ domainId, docType, docId }); return coll.findOne({ domainId, docType, docId });
} }
export async function set(domainId: string, docType: number, docId: DocID, args: any) { export async function set(domainId: string, docType: number, docId: DocID, $set: any) {
await bus.parallel('document/set', domainId, docType, docId, $set);
const res = await coll.findOneAndUpdate( const res = await coll.findOneAndUpdate(
{ domainId, docType, docId }, { domainId, docType, docId },
{ $set: args }, { $set },
{ returnOriginal: false, upsert: true }, { returnOriginal: false, upsert: true },
); );
return res.value; return res.value;
@@ -339,7 +340,7 @@ async function ensureIndexes() {
await ic({ domainId: 1, docType: 1, docId: 1 }, u); await ic({ domainId: 1, docType: 1, docId: 1 }, u);
await ic({ domainId: 1, docType: 1, owner: 1, docId: -1 }); await ic({ domainId: 1, docType: 1, owner: 1, docId: -1 });
// For problem // For problem
await ic({ domainId: 1, docType: 1, title: 'text' }, s); await ic({ domainId: 1, docType: 1, search: 'text', title: 'text' }, s);
await ic({ domainId: 1, docType: 1, category: 1, docId: 1 }, s); await ic({ domainId: 1, docType: 1, category: 1, docId: 1 }, s);
await ic({ domainId: 1, docType: 1, hidden: 1, category: 1, docId: 1 }, s); await ic({ domainId: 1, docType: 1, hidden: 1, category: 1, docId: 1 }, s);
await ic({ domainId: 1, docType: 1, tag: 1, docId: 1 }, s); await ic({ domainId: 1, docType: 1, tag: 1, docId: 1 }, s);
+3 -2
View File
@@ -1,6 +1,6 @@
/* eslint-disable no-await-in-loop */ /* eslint-disable no-await-in-loop */
import cluster from 'cluster'; import cluster from 'cluster';
import { Db } from 'mongodb'; import { Db, ObjectID } from 'mongodb';
import { Mdoc, Rdoc, User } from '../interface'; import { Mdoc, Rdoc, User } from '../interface';
const _hooks: Record<keyof any, Array<(...args: any[]) => any>> = {}; const _hooks: Record<keyof any, Array<(...args: any[]) => any>> = {};
@@ -21,7 +21,8 @@ interface EventMap {
'user/message': (uid: number, mdoc: Mdoc, udoc: User) => void 'user/message': (uid: number, mdoc: Mdoc, udoc: User) => void
'document/add': (doc: any) => string | void 'document/add': (doc: any) => Promise<string | void> | string | void
'document/set': (domainId: string, docType: number, docId: ObjectID | string | number, args: any) => Promise<string | void> | string | void
'record/change': (rdoc: Rdoc, $set?: any, $push?: any) => void 'record/change': (rdoc: Rdoc, $set?: any, $push?: any) => void
} }
+1 -1
View File
@@ -33,4 +33,4 @@ export async function postInit() {
}); });
} }
bus.subscribe(['app/started'], postInit); bus.once('app/started', postInit);
+1
View File
@@ -15,6 +15,7 @@
{ "path": "packages/hydrojudge" }, { "path": "packages/hydrojudge" },
{ "path": "packages/geoip" }, { "path": "packages/geoip" },
{ "path": "packages/html2md" }, { "path": "packages/html2md" },
{ "path": "packages/better-search" },
{ "path": "packages/fps-importer" }, { "path": "packages/fps-importer" },
{ "path": "packages/login-with-github" }, { "path": "packages/login-with-github" },
{ "path": "packages/login-with-google" }, { "path": "packages/login-with-google" },