|
|
|
@@ -2189,11 +2189,18 @@ async function deprecateEntriesByLibrary(db, ownerId, library) {
|
|
|
|
|
var MAX_LIKE_Q_BYTES = 48;
|
|
|
|
|
var MAX_LIKE_TERMS = 6;
|
|
|
|
|
var utf8Len = (s) => new TextEncoder().encode(s).length;
|
|
|
|
|
var LIKE_ESCAPE = "\\";
|
|
|
|
|
var CONTENT_LIKE = `content LIKE ? ESCAPE '${LIKE_ESCAPE}'`;
|
|
|
|
|
function escapeLikeLiteral(s) {
|
|
|
|
|
return s.replace(/[\\%_]/g, (ch) => LIKE_ESCAPE + ch);
|
|
|
|
|
}
|
|
|
|
|
var likeBytes = (s) => utf8Len(escapeLikeLiteral(s));
|
|
|
|
|
var likePattern = (s) => `%${escapeLikeLiteral(s)}%`;
|
|
|
|
|
function chunkByBytes(s, maxBytes) {
|
|
|
|
|
const out = [];
|
|
|
|
|
let cur = "";
|
|
|
|
|
for (const ch of s) {
|
|
|
|
|
if (utf8Len(cur + ch) > maxBytes) {
|
|
|
|
|
if (likeBytes(cur + ch) > maxBytes) {
|
|
|
|
|
if (cur) out.push(cur);
|
|
|
|
|
cur = ch;
|
|
|
|
|
} else {
|
|
|
|
@@ -2204,8 +2211,8 @@ function chunkByBytes(s, maxBytes) {
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
function buildContentLike(q) {
|
|
|
|
|
if (utf8Len(q) <= MAX_LIKE_Q_BYTES) {
|
|
|
|
|
return { conds: ["content LIKE ?"], params: [`%${q}%`], split: false };
|
|
|
|
|
if (likeBytes(q) <= MAX_LIKE_Q_BYTES) {
|
|
|
|
|
return { conds: [CONTENT_LIKE], params: [likePattern(q)], split: false };
|
|
|
|
|
}
|
|
|
|
|
const terms = [];
|
|
|
|
|
for (const word of q.split(/\s+/).filter(Boolean)) {
|
|
|
|
@@ -2217,8 +2224,8 @@ function buildContentLike(q) {
|
|
|
|
|
}
|
|
|
|
|
if (terms.length === 0) terms.push(chunkByBytes(q, MAX_LIKE_Q_BYTES)[0] ?? "");
|
|
|
|
|
return {
|
|
|
|
|
conds: terms.map(() => "content LIKE ?"),
|
|
|
|
|
params: terms.map((t) => `%${t}%`),
|
|
|
|
|
conds: terms.map(() => CONTENT_LIKE),
|
|
|
|
|
params: terms.map(likePattern),
|
|
|
|
|
split: true
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
@@ -2335,7 +2342,7 @@ function buildSearchScore(q) {
|
|
|
|
|
if (terms.length === 0) {
|
|
|
|
|
const m = buildContentLike(trimmed);
|
|
|
|
|
return {
|
|
|
|
|
scoreExpr: m.conds.map(() => "CASE WHEN content LIKE ? THEN 1 ELSE 0 END").join(" + "),
|
|
|
|
|
scoreExpr: m.conds.map(() => `CASE WHEN ${CONTENT_LIKE} THEN 1 ELSE 0 END`).join(" + "),
|
|
|
|
|
scoreParams: m.params,
|
|
|
|
|
terms: [],
|
|
|
|
|
legacyShape: true
|
|
|
|
@@ -2344,14 +2351,14 @@ function buildSearchScore(q) {
|
|
|
|
|
const parts = [];
|
|
|
|
|
const params = [];
|
|
|
|
|
for (const { term, weight } of terms) {
|
|
|
|
|
parts.push(`CASE WHEN content LIKE ? THEN ${weight} ELSE 0 END`);
|
|
|
|
|
params.push(`%${term}%`);
|
|
|
|
|
parts.push(`CASE WHEN ${CONTENT_LIKE} THEN ${weight} ELSE 0 END`);
|
|
|
|
|
params.push(likePattern(term));
|
|
|
|
|
}
|
|
|
|
|
const single = terms.length === 1 && terms[0].term === trimmed;
|
|
|
|
|
if (!single && utf8Len(trimmed) <= MAX_LIKE_Q_BYTES) {
|
|
|
|
|
if (!single && likeBytes(trimmed) <= MAX_LIKE_Q_BYTES) {
|
|
|
|
|
const bonus = terms.reduce((s, t) => s + t.weight, 0);
|
|
|
|
|
parts.push(`CASE WHEN content LIKE ? THEN ${bonus} ELSE 0 END`);
|
|
|
|
|
params.push(`%${trimmed}%`);
|
|
|
|
|
parts.push(`CASE WHEN ${CONTENT_LIKE} THEN ${bonus} ELSE 0 END`);
|
|
|
|
|
params.push(likePattern(trimmed));
|
|
|
|
|
}
|
|
|
|
|
return { scoreExpr: parts.join(" + "), scoreParams: params, terms, legacyShape: single };
|
|
|
|
|
}
|
|
|
|
|