mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: update default search logic
This commit is contained in:
1 parent
52a0666b31
commit
9e3f650afc
1 file changed
+22
-5
@@ -1,7 +1,9 @@
|
|||||||
import AdmZip from 'adm-zip';
|
import AdmZip from 'adm-zip';
|
||||||
import { readFile, statSync } from 'fs-extra';
|
import { readFile, statSync } from 'fs-extra';
|
||||||
import { isBinaryFile } from 'isbinaryfile';
|
import { isBinaryFile } from 'isbinaryfile';
|
||||||
import { flattenDeep, intersection, isSafeInteger } from 'lodash';
|
import {
|
||||||
|
escapeRegExp, flattenDeep, intersection, isSafeInteger,
|
||||||
|
} from 'lodash';
|
||||||
import { FilterQuery, ObjectID } from 'mongodb';
|
import { FilterQuery, ObjectID } from 'mongodb';
|
||||||
import { nanoid } from 'nanoid';
|
import { nanoid } from 'nanoid';
|
||||||
import { sortFiles, streamToBuffer } from '@hydrooj/utils/lib/utils';
|
import { sortFiles, streamToBuffer } from '@hydrooj/utils/lib/utils';
|
||||||
@@ -13,7 +15,7 @@ import {
|
|||||||
ValidationError,
|
ValidationError,
|
||||||
} from '../error';
|
} from '../error';
|
||||||
import {
|
import {
|
||||||
ProblemDoc, ProblemStatusDoc, User,
|
ProblemDoc, ProblemSearchOptions, ProblemStatusDoc, User,
|
||||||
} from '../interface';
|
} from '../interface';
|
||||||
import paginate from '../lib/paginate';
|
import paginate from '../lib/paginate';
|
||||||
import { isPid, parsePid as convertPid } from '../lib/validator';
|
import { isPid, parsePid as convertPid } from '../lib/validator';
|
||||||
@@ -117,6 +119,23 @@ function buildQuery(udoc: User) {
|
|||||||
return q;
|
return q;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const defaultSearch = async (domainId: string, q: string, options?: ProblemSearchOptions) => {
|
||||||
|
const escaped = escapeRegExp(q.toLowerCase());
|
||||||
|
const $regex = new RegExp(q.length >= 3 ? escaped : `\\A${escaped}`, 'gmi');
|
||||||
|
const filter = { $or: [{ pid: { $regex } }, { title: { $regex } }] };
|
||||||
|
const pdocs = await problem.getMulti(domainId, filter, ['domainId', 'docId', 'pid'])
|
||||||
|
.skip(options.skip || 0).limit(options.limit || system.get('pagination.problem')).toArray();
|
||||||
|
if (!Number.isNaN(+q)) {
|
||||||
|
const pdoc = await problem.get(domainId, +q, ['domainId', 'docId', 'pid', 'title']);
|
||||||
|
if (pdoc) pdocs.unshift(pdoc);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
hits: pdocs.map((i) => `${i.domainId}/${i.docId}`),
|
||||||
|
total: await problem.count(domainId, filter),
|
||||||
|
countRelation: 'eq',
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export class ProblemMainHandler extends Handler {
|
export class ProblemMainHandler extends Handler {
|
||||||
@param('page', Types.PositiveInt, true)
|
@param('page', Types.PositiveInt, true)
|
||||||
@param('q', Types.Content, true)
|
@param('q', Types.Content, true)
|
||||||
@@ -125,7 +144,7 @@ export class ProblemMainHandler extends Handler {
|
|||||||
// eslint-disable-next-line @typescript-eslint/no-shadow
|
// eslint-disable-next-line @typescript-eslint/no-shadow
|
||||||
const query = buildQuery(this.user);
|
const query = buildQuery(this.user);
|
||||||
const psdict = {};
|
const psdict = {};
|
||||||
const search = global.Hydro.lib.problemSearch;
|
const search = global.Hydro.lib.problemSearch || defaultSearch;
|
||||||
let sort: string[];
|
let sort: string[];
|
||||||
let fail = false;
|
let fail = false;
|
||||||
let pcountRelation = 'eq';
|
let pcountRelation = 'eq';
|
||||||
@@ -138,7 +157,6 @@ export class ProblemMainHandler extends Handler {
|
|||||||
if (category.length) this.UiContext.extraTitleContent = category.join(',');
|
if (category.length) this.UiContext.extraTitleContent = category.join(',');
|
||||||
let total = 0;
|
let total = 0;
|
||||||
if (text) {
|
if (text) {
|
||||||
if (search) {
|
|
||||||
const result = await search(domainId, q, { skip: (page - 1) * system.get('pagination.problem') });
|
const result = await search(domainId, q, { skip: (page - 1) * system.get('pagination.problem') });
|
||||||
total = result.total;
|
total = result.total;
|
||||||
pcountRelation = result.countRelation;
|
pcountRelation = result.countRelation;
|
||||||
@@ -151,7 +169,6 @@ export class ProblemMainHandler extends Handler {
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
sort = result.hits;
|
sort = result.hits;
|
||||||
} else query.$text = { $search: text };
|
|
||||||
}
|
}
|
||||||
await bus.serial('problem/list', query, this);
|
await bus.serial('problem/list', query, this);
|
||||||
// eslint-disable-next-line prefer-const
|
// eslint-disable-next-line prefer-const
|
||||||
|
|||||||
Reference in new issue
Block a user