perf(search): предтокенизация документов при загрузке (поиск ~1.2с → ~6мс)
This commit is contained in:
@@ -145,7 +145,17 @@ function loadIndex(rawJson) {
|
|||||||
index = { docs: Array.isArray(index) ? index : [] };
|
index = { docs: Array.isArray(index) ? index : [] };
|
||||||
}
|
}
|
||||||
|
|
||||||
const docs = Array.isArray(index.docs) ? index.docs : [];
|
const docs = (Array.isArray(index.docs) ? index.docs : []).map((doc) => ({
|
||||||
|
location: typeof doc.location === 'string' ? doc.location : '',
|
||||||
|
title: typeof doc.title === 'string' ? doc.title : '',
|
||||||
|
text: typeof doc.text === 'string' ? doc.text : '',
|
||||||
|
// Предрасчёт: токены и сегмент считаются ОДИН раз при загрузке,
|
||||||
|
// а не на каждый поисковый запрос (экономия ~1 с на 823 доках).
|
||||||
|
_seg: (typeof doc.location === 'string' ? doc.location : '').split('/')[0].trim(),
|
||||||
|
_titleTokens: tokenize(doc.title),
|
||||||
|
_textTokens: tokenize(doc.text),
|
||||||
|
}));
|
||||||
|
|
||||||
// Предрасчёт: списки ресурсов считаются ОДИН раз при загрузке индекса,
|
// Предрасчёт: списки ресурсов считаются ОДИН раз при загрузке индекса,
|
||||||
// а не на каждый запрос (экономия CPU на regex по всем докам).
|
// а не на каждый запрос (экономия CPU на regex по всем докам).
|
||||||
const resourceTypes = extractResourceTypes({ docs });
|
const resourceTypes = extractResourceTypes({ docs });
|
||||||
@@ -230,11 +240,11 @@ function search(index, query, opts = {}) {
|
|||||||
for (const doc of docs) {
|
for (const doc of docs) {
|
||||||
if (!doc) continue;
|
if (!doc) continue;
|
||||||
const loc = typeof doc.location === "string" ? doc.location : "";
|
const loc = typeof doc.location === "string" ? doc.location : "";
|
||||||
const seg = loc.split("/")[0].trim();
|
const seg = doc._seg !== undefined ? doc._seg : loc.split("/")[0].trim();
|
||||||
const boost = KIND_BOOST[pageKind(seg)] || 1.0;
|
const boost = KIND_BOOST[pageKind(seg)] || 1.0;
|
||||||
|
|
||||||
const titleTokens = tokenize(doc.title);
|
const titleTokens = doc._titleTokens || tokenize(doc.title);
|
||||||
const textTokens = tokenize(doc.text);
|
const textTokens = doc._textTokens || tokenize(doc.text);
|
||||||
|
|
||||||
let score = 0;
|
let score = 0;
|
||||||
for (const t of qTokens) {
|
for (const t of qTokens) {
|
||||||
|
|||||||
Reference in New Issue
Block a user