nodejs-hands-on/regexp-search.js
2024-02-06 14:33:31 +09:00

16 lines
407 B
JavaScript

"use strict";
// 正規表現検索を有効化
window.elasticlunr.Index.load = (index) => ({
search(keyword) {
const regexp = new RegExp(keyword, "gi");
return [...Object.entries(index.documentStore.docs)].flatMap(
([ref, doc]) => {
const match = `${doc.title} ${doc.body}`.match(regexp);
return match ? [{ ref, doc, score: match.length }] : [];
},
);
},
});