generated from ztimson/template
Better searching
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
import fs from 'node:fs';
|
||||
import {decompress as lzmaDecompress} from 'lzma1';
|
||||
import {ZstdCodec} from 'zstd-codec';
|
||||
import {fuzzyMatch} from './utils.js';
|
||||
import {fuzzyMatch, titleFromUrl} from './utils.js';
|
||||
|
||||
const HEADER_SIZE = 80;
|
||||
const NS_CONTENT = 'C';
|
||||
@@ -219,7 +219,7 @@ export class ZimReader {
|
||||
const termList = String(terms).split(',').map(t => t.trim()).filter(Boolean);
|
||||
if (!termList.length) return [];
|
||||
|
||||
const candidates = this.#hasTitleListing
|
||||
const candidates = (this.#hasTitleListing && this.#header.articleCount > 5000)
|
||||
? await this.#titleIndexCandidates(termList[0])
|
||||
: await this.#allDirents();
|
||||
|
||||
@@ -227,9 +227,16 @@ export class ZimReader {
|
||||
for (const dirent of candidates) {
|
||||
if (dirent.namespace !== NS_CONTENT) continue;
|
||||
if (htmlOnly && !(this.#mimeTypes[dirent.mimetype] || '').startsWith('text/html')) continue;
|
||||
const {max} = fuzzyMatch(dirent.title, ...termList);
|
||||
scored.push({url: dirent.url, title: dirent.title, namespace: dirent.namespace, score: max});
|
||||
const urlTitle = titleFromUrl(dirent.url);
|
||||
const titleScore = fuzzyMatch(dirent.title, ...termList).max;
|
||||
const urlScore = fuzzyMatch(urlTitle, ...termList).max;
|
||||
scored.push({
|
||||
url: dirent.url,
|
||||
title: dirent.title.length > urlTitle.length ? dirent.title : urlTitle,
|
||||
namespace: dirent.namespace,
|
||||
score: Math.max(titleScore, urlScore)
|
||||
});
|
||||
}
|
||||
return scored.toSorted((a, b) => b.score - a.score).slice(0, limit);
|
||||
return scored.filter(a => a.score > 0).toSorted((a, b) => b.score - a.score).slice(0, limit);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user