mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
优化中文搜索
This commit is contained in:
1 parent
f2f0212947
commit
6eed519407
11 files changed
+91
-9
No files matched your search
@@ -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(' ');
|
||||
});
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
@@ -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 };
|
||||
@@ -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": []
|
||||
}
|
||||
Reference in new issue
Block a user